Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>\) of array_values is already a list, call has no effect\.$~'
path: tests/Generator/DiffGeneratorTest.php

symfony:
consoleApplicationLoader: tests/doctrine-migrations-phpstan-app.php
Expand Down
17 changes: 1 addition & 16 deletions src/Generator/DiffGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
}
49 changes: 48 additions & 1 deletion tests/Generator/DiffGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down