|
| 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\Migrations\Tools\Console\Command\DiffCommand; |
| 9 | +use Doctrine\Migrations\Tools\Console\Command\DumpSchemaCommand; |
| 10 | +use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand; |
| 11 | +use Doctrine\Migrations\Tools\Console\Command\GenerateCommand; |
| 12 | +use Doctrine\Migrations\Tools\Console\Command\LatestCommand; |
| 13 | +use Doctrine\Migrations\Tools\Console\Command\ListCommand; |
| 14 | +use Doctrine\Migrations\Tools\Console\Command\MigrateCommand; |
| 15 | +use Doctrine\Migrations\Tools\Console\Command\RollupCommand; |
| 16 | +use Doctrine\Migrations\Tools\Console\Command\StatusCommand; |
| 17 | +use Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand; |
| 18 | +use Doctrine\Migrations\Tools\Console\Command\UpToDateCommand; |
| 19 | +use Doctrine\Migrations\Tools\Console\Command\VersionCommand; |
| 20 | +use Doctrine\ORM\EntityManager; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 24 | +use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass; |
| 25 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 26 | +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
| 27 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 28 | +use function sys_get_temp_dir; |
| 29 | + |
| 30 | +class DoctrineCommandsTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @dataProvider getCommands |
| 34 | + */ |
| 35 | + public function testCommandRegistered(string $name, string $instance) : void |
| 36 | + { |
| 37 | + $application = $this->getApplication(); |
| 38 | + |
| 39 | + self::assertInstanceOf($instance, $application->find($name)); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @return string[][] |
| 44 | + */ |
| 45 | + public function getCommands() : array |
| 46 | + { |
| 47 | + return [ |
| 48 | + ['doctrine:migrations:diff', DiffCommand::class], |
| 49 | + ['doctrine:migrations:dump-schema', DumpSchemaCommand::class], |
| 50 | + ['doctrine:migrations:execute', ExecuteCommand::class], |
| 51 | + ['doctrine:migrations:generate', GenerateCommand::class], |
| 52 | + ['doctrine:migrations:latest', LatestCommand::class], |
| 53 | + ['doctrine:migrations:list', ListCommand::class], |
| 54 | + ['doctrine:migrations:migrate', MigrateCommand::class], |
| 55 | + ['doctrine:migrations:rollup', RollupCommand::class], |
| 56 | + ['doctrine:migrations:status', StatusCommand::class], |
| 57 | + ['doctrine:migrations:sync-metadata-storage', SyncMetadataCommand::class], |
| 58 | + ['doctrine:migrations:up-to-date', UpToDateCommand::class], |
| 59 | + ['doctrine:migrations:version', VersionCommand::class], |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return MockObject|KernelInterface |
| 65 | + */ |
| 66 | + private function getKernel(ContainerBuilder $container) |
| 67 | + { |
| 68 | + $kernel = $this |
| 69 | + ->getMockBuilder(KernelInterface::class) |
| 70 | + ->getMock(); |
| 71 | + |
| 72 | + $kernel |
| 73 | + ->expects(self::any()) |
| 74 | + ->method('getContainer') |
| 75 | + ->willReturn($container); |
| 76 | + |
| 77 | + $kernel |
| 78 | + ->expects(self::once()) |
| 79 | + ->method('getBundles') |
| 80 | + ->willReturn([]); |
| 81 | + |
| 82 | + return $kernel; |
| 83 | + } |
| 84 | + |
| 85 | + private function getApplication() : Application |
| 86 | + { |
| 87 | + $container = new ContainerBuilder(new ParameterBag([ |
| 88 | + 'kernel.debug' => false, |
| 89 | + 'kernel.bundles' => [], |
| 90 | + 'kernel.cache_dir' => sys_get_temp_dir(), |
| 91 | + 'kernel.environment' => 'test', |
| 92 | + 'kernel.project_dir' => __DIR__ . '/../', |
| 93 | + ])); |
| 94 | + |
| 95 | + $kernel = $this->getKernel($container); |
| 96 | + $application = new Application($kernel); |
| 97 | + $container->set('application', $application); |
| 98 | + |
| 99 | + $em = $this->createMock(EntityManager::class); |
| 100 | + $container->set('doctrine.orm.default_entity_manager', $em); |
| 101 | + |
| 102 | + $container->addCompilerPass(new AddConsoleCommandPass()); |
| 103 | + |
| 104 | + $extension = new DoctrineMigrationsExtension(); |
| 105 | + $extension->load([ |
| 106 | + 'doctrine_migrations' => [ |
| 107 | + 'migrations_paths' => ['DoctrineMigrationsTest' => 'a'], |
| 108 | + ], |
| 109 | + ], $container); |
| 110 | + |
| 111 | + $container->compile(); |
| 112 | + |
| 113 | + return $application; |
| 114 | + } |
| 115 | +} |
0 commit comments