diff --git a/src/EventListener/SchemaFilterListener.php b/src/EventListener/SchemaFilterListener.php index 203739b..bba1622 100644 --- a/src/EventListener/SchemaFilterListener.php +++ b/src/EventListener/SchemaFilterListener.php @@ -5,7 +5,8 @@ namespace Doctrine\Bundle\MigrationsBundle\EventListener; use Doctrine\DBAL\Schema\AbstractAsset; -use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand; +use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand; +use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand; use Symfony\Component\Console\Event\ConsoleCommandEvent; /** @@ -24,7 +25,7 @@ public function __construct(string $configurationTableName) } /** @var bool */ - private $enabled = true; + private $enabled = false; /** @param AbstractAsset|string $asset */ public function __invoke($asset): bool @@ -40,19 +41,14 @@ public function __invoke($asset): bool return $asset !== $this->configurationTableName; } - private function disable(): void - { - $this->enabled = false; - } - public function onConsoleCommand(ConsoleCommandEvent $event): void { $command = $event->getCommand(); - if (! $command instanceof DoctrineCommand) { + if (! $command instanceof ValidateSchemaCommand && ! $command instanceof UpdateCommand) { return; } - $this->disable(); + $this->enabled = true; } } diff --git a/tests/Collector/EventListener/SchemaFilterListenerTest.php b/tests/Collector/EventListener/SchemaFilterListenerTest.php index 2d2e0a1..f1e47d9 100644 --- a/tests/Collector/EventListener/SchemaFilterListenerTest.php +++ b/tests/Collector/EventListener/SchemaFilterListenerTest.php @@ -7,6 +7,10 @@ use Doctrine\Bundle\MigrationsBundle\EventListener\SchemaFilterListener; use Doctrine\DBAL\Schema\Table; use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand; +use Doctrine\ORM\Tools\Console\Command\AbstractEntityManagerCommand; +use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand; +use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand; +use Doctrine\ORM\Tools\Console\EntityManagerProvider; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Event\ConsoleCommandEvent; use Symfony\Component\Console\Input\ArrayInput; @@ -14,18 +18,17 @@ class SchemaFilterListenerTest extends TestCase { - public function testItFiltersOutMigrationMetadataTableByDefault(): void + public function testItFiltersNothingByDefault(): void { $listener = new SchemaFilterListener('doctrine_migration_versions'); - - self::assertFalse($listener(new Table('doctrine_migration_versions'))); + self::assertTrue($listener(new Table('doctrine_migration_versions'))); self::assertTrue($listener(new Table('some_other_table'))); } - public function testItDisablesItselfWhenTheCurrentCommandIsAMigrationsCommand(): void + public function testItFiltersNothingWhenNotRunningSpecificCommands(): void { $listener = new SchemaFilterListener('doctrine_migration_versions'); - $migrationsCommand = new class extends DoctrineCommand { + $migrationsCommand = new class () extends DoctrineCommand { }; $listener->onConsoleCommand(new ConsoleCommandEvent( @@ -35,5 +38,33 @@ public function testItDisablesItselfWhenTheCurrentCommandIsAMigrationsCommand(): )); self::assertTrue($listener(new Table('doctrine_migration_versions'))); + self::assertTrue($listener(new Table('some_other_table'))); + } + + /** + * @param class-string $command + * + * @dataProvider getCommands + */ + public function testItFiltersOutMigrationMetadataTableWhenRunningSpecificCommands(string $command): void + { + $listener = new SchemaFilterListener('doctrine_migration_versions'); + $ormCommand = new $command($this->createStub(EntityManagerProvider::class)); + + $listener->onConsoleCommand(new ConsoleCommandEvent( + $ormCommand, + new ArrayInput([]), + new NullOutput() + )); + + self::assertFalse($listener(new Table('doctrine_migration_versions'))); + self::assertTrue($listener(new Table('some_other_table'))); + } + + /** @return iterable}> */ + public static function getCommands(): iterable + { + yield 'orm:validate-schema' => [ValidateSchemaCommand::class]; + yield 'orm:schema-tool:update' => [UpdateCommand::class]; } }