doctrine /
DoctrineMigrationsBundle
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Doctrine\Bundle\MigrationsBundle\Tests\DependencyInjection; |
||
| 6 | |||
| 7 | use Doctrine\Bundle\MigrationsBundle\DependencyInjection\DoctrineMigrationsExtension; |
||
| 8 | use Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle; |
||
| 9 | use Doctrine\Bundle\MigrationsBundle\Tests\Fixtures\CustomEntityManager; |
||
| 10 | use Doctrine\DBAL\Connection; |
||
| 11 | use Doctrine\Migrations\Configuration\Configuration; |
||
| 12 | use Doctrine\Migrations\DependencyFactory; |
||
| 13 | use Doctrine\Migrations\Metadata\Storage\MetadataStorage; |
||
| 14 | use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration; |
||
| 15 | use Doctrine\Migrations\Version\Comparator; |
||
| 16 | use Doctrine\Migrations\Version\Version; |
||
| 17 | use Doctrine\ORM\EntityManager; |
||
| 18 | use InvalidArgumentException; |
||
| 19 | use PHPUnit\Framework\TestCase; |
||
| 20 | use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
||
| 21 | use Symfony\Component\Config\FileLocator; |
||
| 22 | use Symfony\Component\DependencyInjection\Alias; |
||
| 23 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
| 24 | use Symfony\Component\DependencyInjection\Definition; |
||
| 25 | use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
||
| 26 | use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
||
| 27 | use function assert; |
||
| 28 | use function method_exists; |
||
| 29 | use function sys_get_temp_dir; |
||
| 30 | |||
| 31 | class DoctrineMigrationsExtensionTest extends TestCase |
||
| 32 | { |
||
| 33 | public function testXmlConfigs() : void |
||
| 34 | { |
||
| 35 | $container = $this->getContainerBuilder(); |
||
| 36 | |||
| 37 | $conn = $this->createMock(Connection::class); |
||
| 38 | $container->set('doctrine.dbal.default_connection', $conn); |
||
| 39 | |||
| 40 | $container->registerExtension(new DoctrineMigrationsExtension()); |
||
| 41 | |||
| 42 | $container->setAlias('doctrine.migrations.configuration.test', new Alias('doctrine.migrations.configuration', true)); |
||
| 43 | |||
| 44 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Fixtures')); |
||
| 45 | $loader->load('conf.xml'); |
||
| 46 | |||
| 47 | $container->compile(); |
||
| 48 | |||
| 49 | $config = $container->get('doctrine.migrations.configuration.test'); |
||
| 50 | $this->assertConfigs($config); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function testFullConfig() : void |
||
| 54 | { |
||
| 55 | $config = [ |
||
| 56 | 'storage' => [ |
||
| 57 | 'table_storage' => [ |
||
| 58 | 'table_name' => 'doctrine_migration_versions_test', |
||
| 59 | 'version_column_name' => 'doctrine_migration_column_test', |
||
| 60 | 'version_column_length' => 2000, |
||
| 61 | 'executed_at_column_name' => 'doctrine_migration_executed_at_column_test', |
||
| 62 | 'execution_time_column_name' => 'doctrine_migration_execution_time_column_test', |
||
| 63 | ], |
||
| 64 | ], |
||
| 65 | |||
| 66 | 'migrations_paths' => [ |
||
| 67 | 'DoctrineMigrationsTest' => 'a', |
||
| 68 | 'DoctrineMigrationsTest2' => 'b', |
||
| 69 | ], |
||
| 70 | |||
| 71 | 'migrations' => ['Foo', 'Bar'], |
||
| 72 | |||
| 73 | 'organize_migrations' => 'BY_YEAR_AND_MONTH', |
||
| 74 | |||
| 75 | 'all_or_nothing' => true, |
||
| 76 | 'check_database_platform' => true, |
||
| 77 | ]; |
||
| 78 | $container = $this->getContainer($config); |
||
| 79 | |||
| 80 | $conn = $this->createMock(Connection::class); |
||
| 81 | $container->set('doctrine.dbal.default_connection', $conn); |
||
| 82 | |||
| 83 | $container->compile(); |
||
| 84 | |||
| 85 | $config = $container->get('doctrine.migrations.configuration'); |
||
| 86 | |||
| 87 | $this->assertConfigs($config); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function testNoConfig() : void |
||
| 91 | { |
||
| 92 | $this->expectException(InvalidConfigurationException::class); |
||
| 93 | $this->expectExceptionMessage('The child node "migrations_paths" at path "doctrine_migrations" must be configured.'); |
||
| 94 | |||
| 95 | $container = $this->getContainer([]); |
||
| 96 | |||
| 97 | $conn = $this->createMock(Connection::class); |
||
| 98 | $container->set('doctrine.dbal.default_connection', $conn); |
||
| 99 | $container->compile(); |
||
| 100 | |||
| 101 | $container->get('doctrine.migrations.configuration'); |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | private function assertConfigs(?object $config) : void |
||
| 106 | { |
||
| 107 | self::assertInstanceOf(Configuration::class, $config); |
||
| 108 | self::assertSame([ |
||
| 109 | 'DoctrineMigrationsTest' => 'a', |
||
| 110 | 'DoctrineMigrationsTest2' => 'b', |
||
| 111 | |||
| 112 | ], $config->getMigrationDirectories()); |
||
|
0 ignored issues
–
show
|
|||
| 113 | |||
| 114 | self::assertSame(['Foo', 'Bar'], $config->getMigrationClasses()); |
||
| 115 | self::assertTrue($config->isAllOrNothing()); |
||
| 116 | self::assertTrue($config->isDatabasePlatformChecked()); |
||
| 117 | self::assertTrue($config->areMigrationsOrganizedByYearAndMonth()); |
||
| 118 | |||
| 119 | $storage = $config->getMetadataStorageConfiguration(); |
||
| 120 | self::assertInstanceOf(TableMetadataStorageConfiguration::class, $storage); |
||
| 121 | |||
| 122 | self::assertSame('doctrine_migration_versions_test', $storage->getTableName()); |
||
| 123 | self::assertSame('doctrine_migration_column_test', $storage->getVersionColumnName()); |
||
| 124 | self::assertSame(2000, $storage->getVersionColumnLength()); |
||
| 125 | self::assertSame('doctrine_migration_execution_time_column_test', $storage->getExecutionTimeColumnName()); |
||
| 126 | self::assertSame('doctrine_migration_executed_at_column_test', $storage->getExecutedAtColumnName()); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function testCustomSorter() : void |
||
| 130 | { |
||
| 131 | $config = [ |
||
| 132 | 'migrations_paths' => ['DoctrineMigrationsTest' => 'a'], |
||
| 133 | 'services' => [Comparator::class => 'my_sorter'], |
||
| 134 | ]; |
||
| 135 | $container = $this->getContainer($config); |
||
| 136 | |||
| 137 | $conn = $this->createMock(Connection::class); |
||
| 138 | $container->set('doctrine.dbal.default_connection', $conn); |
||
| 139 | |||
| 140 | $sorter = new class() implements Comparator{ |
||
| 141 | public function compare(Version $a, Version $b) : int |
||
| 142 | { |
||
| 143 | } |
||
| 144 | }; |
||
| 145 | $container->set('my_sorter', $sorter); |
||
| 146 | |||
| 147 | $container->compile(); |
||
| 148 | |||
| 149 | $di = $container->get('doctrine.migrations.dependency_factory'); |
||
| 150 | self::assertInstanceOf(DependencyFactory::class, $di); |
||
| 151 | self::assertSame($sorter, $di->getVersionComparator()); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function testCustomConnection() : void |
||
| 155 | { |
||
| 156 | $config = [ |
||
| 157 | 'migrations_paths' => ['DoctrineMigrationsTest' => 'a'], |
||
| 158 | 'connection' => 'custom', |
||
| 159 | ]; |
||
| 160 | $container = $this->getContainer($config); |
||
| 161 | |||
| 162 | $conn = $this->createMock(Connection::class); |
||
| 163 | $container->set('doctrine.dbal.custom_connection', $conn); |
||
| 164 | |||
| 165 | $container->compile(); |
||
| 166 | |||
| 167 | $di = $container->get('doctrine.migrations.dependency_factory'); |
||
| 168 | self::assertInstanceOf(DependencyFactory::class, $di); |
||
| 169 | self::assertSame($conn, $di->getConnection()); |
||
| 170 | } |
||
| 171 | |||
| 172 | |||
| 173 | public function testPrefersEntityManagerOverConnection() : void |
||
| 174 | { |
||
| 175 | $config = [ |
||
| 176 | 'migrations_paths' => ['DoctrineMigrationsTest' => 'a'], |
||
| 177 | ]; |
||
| 178 | $container = $this->getContainer($config); |
||
| 179 | |||
| 180 | $em = $this->createMock(EntityManager::class); |
||
| 181 | $container->set('doctrine.orm.default_entity_manager', $em); |
||
| 182 | |||
| 183 | $container->compile(); |
||
| 184 | |||
| 185 | $di = $container->get('doctrine.migrations.dependency_factory'); |
||
| 186 | |||
| 187 | self::assertInstanceOf(DependencyFactory::class, $di); |
||
| 188 | self::assertSame($em, $di->getEntityManager()); |
||
| 189 | } |
||
| 190 | |||
| 191 | public function testCustomEntityManager() : void |
||
| 192 | { |
||
| 193 | $config = [ |
||
| 194 | 'em' => 'custom', |
||
| 195 | 'migrations_paths' => ['DoctrineMigrationsTest' => 'a'], |
||
| 196 | ]; |
||
| 197 | $container = $this->getContainer($config); |
||
| 198 | |||
| 199 | $em = new Definition(CustomEntityManager::class); |
||
| 200 | $container->setDefinition('doctrine.orm.custom_entity_manager', $em); |
||
| 201 | |||
| 202 | $container->compile(); |
||
| 203 | |||
| 204 | $di = $container->get('doctrine.migrations.dependency_factory'); |
||
| 205 | self::assertInstanceOf(DependencyFactory::class, $di); |
||
| 206 | |||
| 207 | $em = $di->getEntityManager(); |
||
| 208 | self::assertInstanceOf(CustomEntityManager::class, $em); |
||
| 209 | |||
| 210 | assert(method_exists($di->getConnection(), 'getEm')); |
||
| 211 | self::assertInstanceOf(CustomEntityManager::class, $di->getConnection()->getEm()); |
||
| 212 | self::assertSame($em, $di->getConnection()->getEm()); |
||
| 213 | } |
||
| 214 | |||
| 215 | public function testCustomMetadataStorage() : void |
||
| 216 | { |
||
| 217 | $config = [ |
||
| 218 | 'migrations_paths' => ['DoctrineMigrationsTest' => 'a'], |
||
| 219 | 'services' => [MetadataStorage::class => 'mock_storage_service'], |
||
| 220 | ]; |
||
| 221 | |||
| 222 | $container = $this->getContainer($config); |
||
| 223 | |||
| 224 | $mockStorage = $this->createMock(MetadataStorage::class); |
||
| 225 | $container->set('mock_storage_service', $mockStorage); |
||
| 226 | |||
| 227 | $conn = $this->createMock(Connection::class); |
||
| 228 | $container->set('doctrine.dbal.default_connection', $conn); |
||
| 229 | |||
| 230 | $container->compile(); |
||
| 231 | |||
| 232 | $di = $container->get('doctrine.migrations.dependency_factory'); |
||
| 233 | self::assertInstanceOf(DependencyFactory::class, $di); |
||
| 234 | self::assertSame($mockStorage, $di->getMetadataStorage()); |
||
| 235 | } |
||
| 236 | |||
| 237 | public function testInvalidService() : void |
||
| 238 | { |
||
| 239 | $this->expectException(InvalidConfigurationException::class); |
||
| 240 | $this->expectExceptionMessage('Invalid configuration for path "doctrine_migrations.services": Valid services for the DoctrineMigrationsBundle must be in the "Doctrine\Migrations" namespace.'); |
||
| 241 | |||
| 242 | $config = [ |
||
| 243 | 'migrations_paths' => ['DoctrineMigrationsTest' => 'a'], |
||
| 244 | 'services' => ['foo' => 'mock_storage_service'], |
||
| 245 | ]; |
||
| 246 | $container = $this->getContainer($config); |
||
| 247 | |||
| 248 | $conn = $this->createMock(Connection::class); |
||
| 249 | $container->set('doctrine.dbal.default_connection', $conn); |
||
| 250 | |||
| 251 | $container->compile(); |
||
| 252 | } |
||
| 253 | |||
| 254 | public function testCanNotSpecifyBothEmAndConnection() : void |
||
| 255 | { |
||
| 256 | $this->expectExceptionMessage('You cannot specify both "connection" and "em" in the DoctrineMigrationsBundle configurations'); |
||
| 257 | $this->expectException(InvalidArgumentException::class); |
||
| 258 | |||
| 259 | $config = [ |
||
| 260 | 'migrations_paths' => ['DoctrineMigrationsTest' => 'a'], |
||
| 261 | 'em' => 'custom', |
||
| 262 | 'connection' => 'custom', |
||
| 263 | ]; |
||
| 264 | |||
| 265 | $container = $this->getContainer($config); |
||
| 266 | |||
| 267 | $container->compile(); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param mixed[] $config |
||
| 272 | */ |
||
| 273 | private function getContainer(array $config) : ContainerBuilder |
||
| 274 | { |
||
| 275 | $container = $this->getContainerBuilder(); |
||
| 276 | |||
| 277 | $bundle = new DoctrineMigrationsBundle(); |
||
| 278 | $bundle->build($container); |
||
| 279 | |||
| 280 | $extension = new DoctrineMigrationsExtension(); |
||
| 281 | |||
| 282 | $extension->load(['doctrine_migrations' => $config], $container); |
||
| 283 | |||
| 284 | $container->getDefinition('doctrine.migrations.dependency_factory')->setPublic(true); |
||
| 285 | $container->getDefinition('doctrine.migrations.configuration')->setPublic(true); |
||
| 286 | |||
| 287 | return $container; |
||
| 288 | } |
||
| 289 | |||
| 290 | private function getContainerBuilder() : ContainerBuilder |
||
| 291 | { |
||
| 292 | return new ContainerBuilder(new ParameterBag([ |
||
| 293 | 'kernel.debug' => false, |
||
| 294 | 'kernel.bundles' => [], |
||
| 295 | 'kernel.cache_dir' => sys_get_temp_dir(), |
||
| 296 | 'kernel.environment' => 'test', |
||
| 297 | 'kernel.project_dir' => __DIR__ . '/../', |
||
| 298 | ])); |
||
| 299 | } |
||
| 300 | } |
||
| 301 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.