|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ApplicationTest\Integration\DBAL\Migration; |
| 4 | + |
| 5 | +use ApplicationTest\Integration\Util\Bootstrap; |
| 6 | +use Doctrine\DBAL; |
| 7 | + |
| 8 | +class MigrationTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var DBAL\Migrations\Configuration\Configuration |
| 12 | + */ |
| 13 | + private $configuration; |
| 14 | + |
| 15 | + protected function tearDown() |
| 16 | + { |
| 17 | + $this->migrateUpTo(); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @dataProvider providerMigration |
| 22 | + * |
| 23 | + * @param string $identifier |
| 24 | + * @param string $previousIdentifier |
| 25 | + */ |
| 26 | + public function testMigration($identifier, $previousIdentifier) |
| 27 | + { |
| 28 | + $this->migrateUpTo($previousIdentifier); |
| 29 | + |
| 30 | + $this->assertCanMigrateUpTo($identifier); |
| 31 | + $this->assertCanMigrateDownFrom($identifier, $previousIdentifier); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @return \Generator |
| 36 | + */ |
| 37 | + public function providerMigration() |
| 38 | + { |
| 39 | + $previousIdentifier = '0'; |
| 40 | + |
| 41 | + foreach ($this->getMigrationConfiguration()->getAvailableVersions() as $identifier) { |
| 42 | + yield [ |
| 43 | + $identifier, |
| 44 | + $previousIdentifier, |
| 45 | + ]; |
| 46 | + |
| 47 | + $previousIdentifier = $identifier; |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Asserts that the migration with the specified version can be rolled back. |
| 53 | + * |
| 54 | + * @param string $identifier |
| 55 | + * @param string $previousIdentifier |
| 56 | + * |
| 57 | + * @throws \Exception |
| 58 | + */ |
| 59 | + private function assertCanMigrateDownFrom($identifier, $previousIdentifier) |
| 60 | + { |
| 61 | + $version = $this->getMigrationConfiguration()->getVersion($identifier); |
| 62 | + |
| 63 | + try { |
| 64 | + $version->execute('down'); |
| 65 | + } catch (\Exception $exception) { |
| 66 | + $this->fail(sprintf( |
| 67 | + 'Failed asserting that the version "%s" can be rolled back from - %s', |
| 68 | + $identifier, |
| 69 | + $exception->getMessage() |
| 70 | + )); |
| 71 | + |
| 72 | + throw $exception; |
| 73 | + } |
| 74 | + |
| 75 | + $this->assertSame($previousIdentifier, $this->getMigrationConfiguration()->getCurrentVersion()); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Asserts that the migration with the specified version can be rolled to. |
| 80 | + * |
| 81 | + * @param string $identifier |
| 82 | + * |
| 83 | + * @throws DBAL\Migrations\MigrationException |
| 84 | + * @throws \Exception |
| 85 | + */ |
| 86 | + private function assertCanMigrateUpTo($identifier) |
| 87 | + { |
| 88 | + $version = $this->getMigrationConfiguration()->getVersion($identifier); |
| 89 | + |
| 90 | + try { |
| 91 | + $version->execute('up'); |
| 92 | + } catch (\Exception $exception) { |
| 93 | + $this->fail(sprintf( |
| 94 | + 'Failed asserting that the version "%s" can be rolled up to - %s', |
| 95 | + $identifier, |
| 96 | + $exception->getMessage() |
| 97 | + )); |
| 98 | + |
| 99 | + throw $exception; |
| 100 | + } |
| 101 | + |
| 102 | + $this->assertSame($identifier, $this->getMigrationConfiguration()->getCurrentVersion()); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Ensure that the migration with the specified version has been migrated up to. |
| 107 | + * |
| 108 | + * @param string $identifier |
| 109 | + */ |
| 110 | + private function migrateUpTo($identifier = null) |
| 111 | + { |
| 112 | + $migration = new DBAL\Migrations\Migration($this->getMigrationConfiguration()); |
| 113 | + $migration->migrate($identifier); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @return DBAL\Migrations\Configuration\Configuration |
| 118 | + */ |
| 119 | + private function getMigrationConfiguration() |
| 120 | + { |
| 121 | + if ($this->configuration === null) { |
| 122 | + $serviceManager = Bootstrap::getServiceManager(); |
| 123 | + |
| 124 | + /* @var DBAL\Migrations\Configuration\Configuration $migrationConfiguration */ |
| 125 | + $this->configuration = $serviceManager->get('doctrine.migrations_configuration.orm_default'); |
| 126 | + } |
| 127 | + |
| 128 | + return $this->configuration; |
| 129 | + } |
| 130 | +} |
0 commit comments