| 1 |
<?php |
| 2 |
|
| 3 |
use PHPUnit\Framework\TestCase; |
| 4 |
use ResponsiveMenu\Database\Migration; |
| 5 |
use ResponsiveMenu\Collections\OptionsCollection; |
| 6 |
use ResponsiveMenu\Management\OptionManager; |
| 7 |
|
| 8 |
class MigrationTest extends TestCase { |
| 9 |
|
| 10 |
private $manager; |
| 11 |
private $defaults = [ |
| 12 |
'foo' => 'bar', |
| 13 |
'baz' => 'qux' |
| 14 |
]; |
| 15 |
|
| 16 |
public function setUp() { |
| 17 |
$db = $this->createMock('ResponsiveMenu\Database\Database'); |
| 18 |
$db->method('all')->willReturn(['moon' => 'rise']); |
| 19 |
$this->manager = new OptionManager($db, ['foo' => 'bar', 'river' => 'run']); |
| 20 |
} |
| 21 |
|
| 22 |
public function tableVersions() { |
| 23 |
return [ |
| 24 |
|
| 25 |
// These don't require a table |
| 26 |
['3.0.1', false], |
| 27 |
['3.1.2', false], |
| 28 |
['4.5.1', false], |
| 29 |
['3.0.0', false], |
| 30 |
|
| 31 |
// These do require a table |
| 32 |
['2.0.0', true], |
| 33 |
['2.0.1', true], |
| 34 |
['2.8.9', true], |
| 35 |
['1.2.0', true], |
| 36 |
['2.9.5', true], |
| 37 |
|
| 38 |
]; |
| 39 |
} |
| 40 |
/** |
| 41 |
* @dataProvider tableVersions |
| 42 |
*/ |
| 43 |
public function testNeedsTable($version, $expected) { |
| 44 |
$migration = new Migration($this->manager, $version, '3.0.1', $this->defaults); |
| 45 |
$this->assertEquals($expected, $migration->needsTable()); |
| 46 |
} |
| 47 |
|
| 48 |
public function updateVersions() { |
| 49 |
return [ |
| 50 |
['3.0.1', '3.1.0', true], |
| 51 |
['2.8.9', '3.0.0', true], |
| 52 |
['1.6.4', '4.1.0', true], |
| 53 |
['3.1.1', '4.5.3', true], |
| 54 |
['3.0.1', '3.0.2', true], |
| 55 |
['3.1.1', '3.1.2', true], |
| 56 |
|
| 57 |
['3.3.1', '3.1.2', false], |
| 58 |
['2.8.1', '1.3.2', false], |
| 59 |
['1.4.1', '1.1.2', false], |
| 60 |
['4.1.2', '3.1.2', false] |
| 61 |
]; |
| 62 |
} |
| 63 |
/** |
| 64 |
* @dataProvider updateVersions |
| 65 |
*/ |
| 66 |
public function testNeedsUpdate($old_version, $new_version, $expected) { |
| 67 |
$migration = new Migration($this->manager, $old_version, $new_version, $this->defaults); |
| 68 |
$this->assertEquals($expected, $migration->needsUpdate()); |
| 69 |
} |
| 70 |
|
| 71 |
public function testAddingNewOptions() { |
| 72 |
$migration = new Migration($this->manager, '3.0.0', '3.0.0', $this->defaults); |
| 73 |
$collection = new OptionsCollection(['foo' => 'bar', 'river' => 'run', 'baz' => 'qux']); |
| 74 |
$this->assertEquals($collection, $migration->addNewOptions()); |
| 75 |
} |
| 76 |
|
| 77 |
public function testRemoveOldOptions() { |
| 78 |
$migration = new Migration($this->manager, '3.0.0', '3.0.0', $this->defaults); |
| 79 |
$collection = new OptionsCollection(['foo' => 'bar', 'river' => 'run']); |
| 80 |
$this->assertEquals($collection, $migration->tidyUpOptions()); |
| 81 |
} |
| 82 |
|
| 83 |
public function testMinorPointUpgradeMigrationScriptsAreReturned() { |
| 84 |
$migration = new Migration($this->manager, '0.0.1', '0.0.2', $this->defaults); |
| 85 |
$classes = $migration->getMigrationClasses(); |
| 86 |
|
| 87 |
$this->assertCount(1, $classes); |
| 88 |
$this->assertArrayHasKey('0.0.1', $classes); |
| 89 |
} |
| 90 |
|
| 91 |
public function testMajorPointUpgradeMigrationScriptsAreReturned() { |
| 92 |
$migration = new Migration($this->manager, '0.8.9', '1.1.1', $this->defaults); |
| 93 |
$classes = $migration->getMigrationClasses(); |
| 94 |
|
| 95 |
$this->assertCount(1, $classes); |
| 96 |
$this->assertArrayHasKey('1.1.0', $classes); |
| 97 |
} |
| 98 |
|
| 99 |
public function testNotUpgradeMigrationScriptsAreReturnedIfNewInstall() { |
| 100 |
$migration = new Migration($this->manager, '', '1.1.1', $this->defaults); |
| 101 |
$classes = $migration->getMigrationClasses(); |
| 102 |
|
| 103 |
$this->assertCount(0, $classes); |
| 104 |
$this->assertArrayNotHasKey('0.0.1', $classes); |
| 105 |
} |
| 106 |
|
| 107 |
public function testMultipleUpgradeMigrationScriptsAreReturned() { |
| 108 |
$migration = new Migration($this->manager, '0.0.1', '1.1.1', $this->defaults); |
| 109 |
$classes = $migration->getMigrationClasses(); |
| 110 |
|
| 111 |
$this->assertCount(4, $classes); |
| 112 |
$this->assertArrayHasKey('0.0.1', $classes); |
| 113 |
$this->assertArrayHasKey('0.0.2', $classes); |
| 114 |
$this->assertArrayHasKey('0.0.5', $classes); |
| 115 |
$this->assertArrayHasKey('1.1.0', $classes); |
| 116 |
} |
| 117 |
|
| 118 |
public function testMigrationScriptUpdatesOptions() { |
| 119 |
$migration = new Migration($this->manager, '0.0.1', '0.0.2', $this->defaults); |
| 120 |
|
| 121 |
$options = new OptionsCollection([ |
| 122 |
'foo' => 'bar', |
| 123 |
'baz' => 'qux', |
| 124 |
'moon' => 'rise' |
| 125 |
]); |
| 126 |
|
| 127 |
foreach($migration->getMigrationClasses() as $migration) |
| 128 |
$migration->migrate($options); |
| 129 |
|
| 130 |
$this->assertEquals('qux', $options['foo']); |
| 131 |
$this->assertEquals('qux', $options['baz']); |
| 132 |
$this->assertEquals('rise', $options['moon']); |
| 133 |
} |
| 134 |
|
| 135 |
public function testMigrationScriptChainsUpdatesOptions() { |
| 136 |
$migration = new Migration($this->manager, '0.0.1', '1.1.1', $this->defaults); |
| 137 |
|
| 138 |
$options = new OptionsCollection([ |
| 139 |
'foo' => 'bar', |
| 140 |
'baz' => 'qux', |
| 141 |
'moon' => 'rise' |
| 142 |
]); |
| 143 |
|
| 144 |
foreach($migration->getMigrationClasses() as $migration) |
| 145 |
$migration->migrate($options); |
| 146 |
|
| 147 |
$this->assertEquals('qux', $options['foo']); |
| 148 |
$this->assertEquals('qux', $options['baz']); |
| 149 |
$this->assertEquals('qux', $options['moon']); |
| 150 |
} |
| 151 |
|
| 152 |
public function testMigrationScriptFunctionsAreCalled() { |
| 153 |
$migration = new Migration($this->manager, '0.0.1', '0.0.2', $this->defaults); |
| 154 |
|
| 155 |
$options = new OptionsCollection([ |
| 156 |
'foo' => 'bar', |
| 157 |
'baz' => 'qux', |
| 158 |
'moon' => 'rise', |
| 159 |
'sun' => [ |
| 160 |
[5, 'foo', 'bar'], |
| 161 |
[6, 'baz', 'qux'], |
| 162 |
[7, 'moon', 'rise'] |
| 163 |
] |
| 164 |
]); |
| 165 |
|
| 166 |
foreach($migration->getMigrationClasses() as $migration) |
| 167 |
$migration->migrate($options); |
| 168 |
|
| 169 |
$this->assertEquals('qux', $options['foo']); |
| 170 |
$this->assertEquals('qux', $options['baz']); |
| 171 |
$this->assertEquals('rise', $options['moon']); |
| 172 |
|
| 173 |
$expected_sun = [ |
| 174 |
[5, 'foo'], |
| 175 |
[6, 'baz'], |
| 176 |
[7, 'moon'] |
| 177 |
]; |
| 178 |
$this->assertEquals(json_encode($expected_sun), $options['sun']); |
| 179 |
} |
| 180 |
|
| 181 |
public function testMigrationScriptFunctionsAreChained() { |
| 182 |
$migration = new Migration($this->manager, '0.0.1', '1.1.1', $this->defaults); |
| 183 |
|
| 184 |
$options = new OptionsCollection([ |
| 185 |
'foo' => 'bar', |
| 186 |
'baz' => 'qux', |
| 187 |
'moon' => 'rise', |
| 188 |
'sun' => [ |
| 189 |
[5, 'foo', 'bar'], |
| 190 |
[6, 'baz', 'qux'], |
| 191 |
[7, 'moon', 'rise'] |
| 192 |
] |
| 193 |
]); |
| 194 |
|
| 195 |
foreach($migration->getMigrationClasses() as $migration) |
| 196 |
$migration->migrate($options); |
| 197 |
|
| 198 |
$this->assertEquals('qux', $options['foo']); |
| 199 |
$this->assertEquals('qux', $options['baz']); |
| 200 |
$this->assertEquals('qux', $options['moon']); |
| 201 |
|
| 202 |
$expected_sun = [ |
| 203 |
[5], |
| 204 |
[6], |
| 205 |
[7] |
| 206 |
]; |
| 207 |
$this->assertEquals(json_encode($expected_sun), $options['sun']); |
| 208 |
} |
| 209 |
|
| 210 |
public function testNoMigrationScriptsAreNotRunIfNewInstall() { |
| 211 |
$migration = new Migration($this->manager, '', '1.1.1', $this->defaults); |
| 212 |
|
| 213 |
$options = new OptionsCollection([ |
| 214 |
'foo' => 'bar', |
| 215 |
'baz' => 'qux', |
| 216 |
'moon' => 'rise', |
| 217 |
]); |
| 218 |
|
| 219 |
foreach($migration->getMigrationClasses() as $migration) |
| 220 |
$migration->migrate($options); |
| 221 |
|
| 222 |
$this->assertEquals('bar', $options['foo']); |
| 223 |
$this->assertEquals('qux', $options['baz']); |
| 224 |
$this->assertEquals('rise', $options['moon']); |
| 225 |
} |
| 226 |
|
| 227 |
} |
| 228 |
|