diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 093bfbac4..a90fb3f1c 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -56,6 +56,9 @@ parameters: - message: '#^Call to function method_exists\(\) with Doctrine\\DBAL\\Schema\\Table and ''getObjectName'' will always evaluate to true\.$#' path: src/SchemaDumper.php + - + message: '~^Parameter #1 \$array \(list\) of array_values is already a list, call has no effect\.$~' + path: tests/Generator/DiffGeneratorTest.php symfony: consoleApplicationLoader: tests/doctrine-migrations-phpstan-app.php diff --git a/src/Generator/DiffGenerator.php b/src/Generator/DiffGenerator.php index 320784451..2d5525903 100644 --- a/src/Generator/DiffGenerator.php +++ b/src/Generator/DiffGenerator.php @@ -16,8 +16,6 @@ use function class_exists; use function method_exists; use function preg_match; -use function strpos; -use function substr; /** * The DiffGenerator class is responsible for comparing two Doctrine\DBAL\Schema\Schema instances and generating a @@ -136,7 +134,7 @@ private function createToSchema(): Schema foreach ($toSchema->getTables() as $table) { $tableName = $table->getName(); - if ($schemaAssetsFilter($this->resolveTableName($tableName))) { + if ($schemaAssetsFilter($tableName)) { continue; } @@ -146,17 +144,4 @@ private function createToSchema(): Schema return $toSchema; } - - /** - * Resolve a table name from its fully qualified name. The `$name` argument - * comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return - * a namespaced name with the form `{namespace}.{tableName}`. This extracts - * the table name from that. - */ - private function resolveTableName(string $name): string - { - $pos = strpos($name, '.'); - - return $pos === false ? $name : substr($name, $pos + 1); - } } diff --git a/tests/Generator/DiffGeneratorTest.php b/tests/Generator/DiffGeneratorTest.php index f9095734b..e32496712 100644 --- a/tests/Generator/DiffGeneratorTest.php +++ b/tests/Generator/DiffGeneratorTest.php @@ -18,6 +18,10 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use function array_map; +use function array_values; +use function preg_match; + class DiffGeneratorTest extends TestCase { private DBALConfiguration&MockObject $dbalConfiguration; @@ -43,7 +47,7 @@ public function testGenerate(): void $this->dbalConfiguration->expects(self::once()) ->method('getSchemaAssetsFilter') ->willReturn( - static fn ($name): bool => $name === 'table_name1', + static fn ($name): bool => $name === 'schema.table_name1', ); $table1 = $this->createMock(Table::class); @@ -181,6 +185,49 @@ public function testGenerateFromEmptySchema(): void self::assertSame('path2', $this->migrationDiffGenerator->generate('2345', null, false, false, 120, true, true)); } + public function testGenerateAppliesFilterOnMappedSchema(): void + { + // a standard Regex SchemaAssetsFilter already registered on the DBAL + $dbalSchemaAssetsFilter = static function ($assetName): bool { + return (bool) preg_match('~^some_schema~', $assetName); + }; + + $fromSchema = new Schema(); + + $toTable1 = new Table('some_schema.table1'); + $toTable2 = new Table('some_schema.table2'); + $toSchema = new Schema([$toTable1, $toTable2]); + + $this->schemaManager->expects(self::once()) + ->method('introspectSchema') + ->willReturn($fromSchema); + + $this->schemaProvider->expects(self::once()) + ->method('createSchema') + ->willReturn($toSchema); + + $this->dbalConfiguration->expects(self::once()) + ->method('getSchemaAssetsFilter') + ->willReturn($dbalSchemaAssetsFilter); + + $schemaDiff = self::createStub(SchemaDiff::class); + $comparator = $this->mockComparator($schemaDiff); + + $this->schemaManager->expects(self::once()) + ->method('createComparator') + ->willReturn($comparator); + + $this->migrationSqlGenerator->expects(self::exactly(2)) + ->method('generate') + ->willReturnOnConsecutiveCalls('up', 'down'); + + $this->migrationDiffGenerator->generate('Version1234', null); + + $filteredTableNames = array_map(static fn (Table $table) => $table->getName(), $toSchema->getTables()); + + self::assertSame(['some_schema.table1', 'some_schema.table2'], array_values($filteredTableNames)); + } + protected function setUp(): void { $this->dbalConfiguration = $this->createMock(DBALConfiguration::class);