| @@ -1,227 +1,114 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | use PHPUnit\Framework\TestCase; |
| 4 | +use ResponsiveMenu\Collections\OptionsCollection; | |
| 4 | 5 | use ResponsiveMenu\Database\Migration; |
| 5 | -use ResponsiveMenu\Collections\OptionsCollection; | |
| 6 | -use ResponsiveMenu\Management\OptionManager; | |
| 6 | +use ResponsiveMenu\Models\Option; | |
| 7 | 7 | |
| 8 | 8 | class MigrationTest extends TestCase { |
| 9 | 9 | |
| 10 | - private $manager; | |
| 11 | - private $defaults = [ | |
| 12 | - 'foo' => 'bar', | |
| 13 | - 'baz' => 'qux' | |
| 14 | - ]; | |
| 15 | - | |
| 16 | 10 | 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 | - } | |
| 11 | + $this->database = $this->createMock('ResponsiveMenu\Database\WpDatabase'); | |
| 12 | + $this->service = $this->createMock('ResponsiveMenu\Services\OptionService'); | |
| 13 | + $this->defaults = ['default_one' => 1, 'default_two' => 'string', 'default_three' => 4.5, 'default_four' => 'new']; | |
| 14 | + $this->current_version = '3.0.8'; | |
| 15 | + $this->old_version = '3.0.7'; | |
| 16 | + $this->old_options = ['default_one' => 4, 'default_two' => 'old string', 'default_three' => 4.5, 'RM' => 'old RM value', 'RMDepth' => 'old RMDepth value']; | |
| 21 | 17 | |
| 22 | - public function tableVersions() { | |
| 23 | - return [ | |
| 18 | + $this->base_migration = new Migration($this->database, $this->service, $this->defaults, $this->current_version, $this->old_version, $this->old_options); | |
| 24 | 19 | |
| 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], | |
| 20 | + $this->options_collection = new OptionsCollection; | |
| 21 | + $this->options_collection->add(new Option('default_one', 5)); | |
| 22 | + $this->options_collection->add(new Option('default_two', 'string')); | |
| 23 | + $this->options_collection->add(new Option('default_three', 7.5)); | |
| 30 | 24 | |
| 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], | |
| 25 | + /* | |
| 26 | + * Mock the repository all() function to return controlled options collection | |
| 27 | + */ | |
| 28 | + $service_options = new OptionsCollection; | |
| 29 | + $service_options->add(new Option('default_one', 5)); | |
| 30 | + $service_options->add(new Option('default_two', 'new option')); | |
| 31 | + $service_options->add(new Option('to_delete', 'delete me!')); | |
| 32 | + $service_options->add(new Option('to_delete_also', 'delete me too!')); | |
| 33 | + $this->service->method('all')->willReturn($service_options); | |
| 37 | 34 | |
| 38 | - ]; | |
| 39 | 35 | } |
| 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()); | |
| 36 | + | |
| 37 | + public function testVersionCompareNeedsUpdate() { | |
| 38 | + $this->assertTrue($this->base_migration->needsUpdate()); | |
| 46 | 39 | } |
| 47 | 40 | |
| 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], | |
| 41 | + public function testVersionCompareDoesntNeedUpdate() { | |
| 42 | + $migration = new Migration($this->database, $this->service, $this->defaults, $this->current_version, '3.0.9', $this->old_options); | |
| 43 | + $this->assertFalse($migration->needsUpdate()); | |
| 44 | + } | |
| 56 | 45 | |
| 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 | - ]; | |
| 46 | + public function testVersionCompareNeedUpdateWithDoubleEndPoint() { | |
| 47 | + $migration = new Migration($this->database, $this->service, $this->defaults, '3.0.10', $this->old_version, $this->old_options); | |
| 48 | + $this->assertTrue($migration->needsUpdate()); | |
| 62 | 49 | } |
| 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()); | |
| 50 | + | |
| 51 | + public function testVersionCompareDoesntNeedUpdateWithDoubleEndPoint() { | |
| 52 | + $migration = new Migration($this->database, $this->service, $this->defaults, '3.0.10', '3.1.0', $this->old_options); | |
| 53 | + $this->assertFalse($migration->needsUpdate()); | |
| 69 | 54 | } |
| 70 | 55 | |
| 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()); | |
| 56 | + public function testVersionCompareDoesNeedUpdateWithTenComparedToOne() { | |
| 57 | + $migration = new Migration($this->database, $this->service, $this->defaults, '3.0.10', '3.0.1', $this->old_options); | |
| 58 | + $this->assertTrue($migration->needsUpdate()); | |
| 75 | 59 | } |
| 76 | 60 | |
| 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()); | |
| 61 | + public function testVersionCompareDoesntNeedUpdateWithOneComparedToTen() { | |
| 62 | + $migration = new Migration($this->database, $this->service, $this->defaults, '3.0.1', '3.0.10', $this->old_options); | |
| 63 | + $this->assertFalse($migration->needsUpdate()); | |
| 81 | 64 | } |
| 82 | 65 | |
| 83 | - public function testMinorPointUpgradeMigrationScriptsAreReturned() { | |
| 84 | - $migration = new Migration($this->manager, '0.0.1', '0.0.2', $this->defaults); | |
| 85 | - $classes = $migration->getMigrationClasses(); | |
| 66 | + public function testVersionCompareDoesntNeedUpdateWithDoubleEndPointWithVersionHigher() { | |
| 67 | + $migration = new Migration($this->database, $this->service, $this->defaults, '3.0.10', '3.2.0', $this->old_options); | |
| 68 | + $this->assertFalse($migration->needsUpdate()); | |
| 69 | + } | |
| 86 | 70 | |
| 87 | - $this->assertCount(1, $classes); | |
| 88 | - $this->assertArrayHasKey('0.0.1', $classes); | |
| 71 | + public function testVersionCompareDoesNeedUpdateWithDoubleEndPointWithVersionHigher() { | |
| 72 | + $migration = new Migration($this->database, $this->service, $this->defaults, '3.2.0', '3.0.10', $this->old_options); | |
| 73 | + $this->assertTrue($migration->needsUpdate()); | |
| 89 | 74 | } |
| 90 | 75 | |
| 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); | |
| 76 | + public function testNewOptionsReturnedAreCorrect() { | |
| 77 | + $this->assertSame(['default_four' => 'new'], $this->base_migration->getNewOptions($this->options_collection)); | |
| 97 | 78 | } |
| 98 | 79 | |
| 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); | |
| 80 | + public function testIsVersion3CheckReturnsFalse() { | |
| 81 | + $migration = new Migration($this->database, $this->service, $this->defaults, $this->current_version, '2.8.0', $this->old_options); | |
| 82 | + $this->assertFalse($migration->isVersion3()); | |
| 105 | 83 | } |
| 106 | 84 | |
| 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); | |
| 85 | + public function testIsVersion3CheckReturnsTrue() { | |
| 86 | + $this->assertTrue($this->base_migration->isVersion3()); | |
| 116 | 87 | } |
| 117 | 88 | |
| 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']); | |
| 89 | + public function testDeletableOptions() { | |
| 90 | + $this->assertSame(['to_delete' => 'to_delete', 'to_delete_also' => 'to_delete_also'], $this->base_migration->getOptionsToDelete()); | |
| 133 | 91 | } |
| 134 | 92 | |
| 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']); | |
| 93 | + public function testOptionsToMigrate() { | |
| 94 | + $this->assertSame(['menu_to_use' => 'old RM value', 'menu_depth' => 'old RMDepth value'], $this->base_migration->getMigratedOptions()); | |
| 150 | 95 | } |
| 151 | 96 | |
| 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']); | |
| 97 | + public function testSetup() { | |
| 98 | + $this->database->method('createTable')->willReturn(true); | |
| 99 | + $migration = new Migration($this->database, $this->service, $this->defaults, '3.0.10', '2.8.9', $this->old_options); | |
| 100 | + $this->assertEquals(null, $migration->setUp()); | |
| 179 | 101 | } |
| 180 | 102 | |
| 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']); | |
| 103 | + public function testSynchronise() { | |
| 104 | + $this->assertEquals(null, $this->base_migration->synchronise()); | |
| 208 | 105 | } |
| 209 | 106 | |
| 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']); | |
| 107 | + public function testAddNewOptionsEmpty() { | |
| 108 | + $service = $this->createMock('ResponsiveMenu\Services\OptionService'); | |
| 109 | + $service->method('all')->willReturn(new ResponsiveMenu\Collections\OptionsCollection); | |
| 110 | + $migration = new Migration($this->database, $service, $this->defaults, $this->current_version, $this->old_version, $this->old_options); | |
| 111 | + $this->assertEquals(null, $migration->addNewOptions()); | |
| 225 | 112 | } |
| 226 | 113 | |
| 227 | 114 | } |