Skip to content

Commit 41cb96c

Browse files
committed
Issue #1487
1 parent fe0fcea commit 41cb96c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/Generator/DiffGeneratorTest.php

+57
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use PHPUnit\Framework\MockObject\MockObject;
1919
use PHPUnit\Framework\TestCase;
2020

21+
use function preg_match;
22+
2123
class DiffGeneratorTest extends TestCase
2224
{
2325
private DBALConfiguration&MockObject $dbalConfiguration;
@@ -180,6 +182,61 @@ public function testGenerateFromEmptySchema(): void
180182
self::assertSame('path2', $this->migrationDiffGenerator->generate('2345', null, false, 120, true, true));
181183
}
182184

185+
public function testGenerateCallsComparatorWithExpectedSchemasWhenDbalHasSchemaAssetsFilter(): void
186+
{
187+
// a standard Regex SchemaAssetsFilter already registered on the DBAL
188+
$dbalSchemaAssetsFilter = static function ($assetName): bool {
189+
return (bool) preg_match('~^some_schema\.~', $assetName);
190+
};
191+
192+
$fromSchema = new Schema();
193+
194+
$toTable1 = new Table('some_schema.table1');
195+
$toTable2 = new Table('some_schema.table2');
196+
$toSchema = new Schema([$toTable1, $toTable2]);
197+
198+
$this->schemaManager->expects(self::once())
199+
->method('introspectSchema')
200+
->willReturn($fromSchema);
201+
202+
$this->schemaProvider->expects(self::once())
203+
->method('createSchema')
204+
->willReturn($toSchema);
205+
206+
$this->dbalConfiguration->expects(self::once())
207+
->method('getSchemaAssetsFilter')
208+
->willReturn($dbalSchemaAssetsFilter);
209+
210+
$comparator = $this->createMock(Comparator::class);
211+
$matcher = self::exactly(2);
212+
$comparator->expects($matcher)
213+
->method('compareSchemas')
214+
->willReturnCallback(
215+
static function (Schema $oldSchema, Schema $newSchema) use ($matcher, $toTable1, $toTable2): SchemaDiff {
216+
// assert that comparator is called with the expected schema
217+
if ($matcher->numberOfInvocations() === 1) { // up
218+
self::assertEquals([$toTable1, $toTable2], $newSchema->getTables());
219+
}
220+
221+
if ($matcher->numberOfInvocations() === 2) { // down
222+
self::assertEquals([$toTable1, $toTable2], $oldSchema->getTables());
223+
}
224+
225+
return self::createStub(SchemaDiff::class);
226+
},
227+
);
228+
229+
$this->schemaManager->expects(self::once())
230+
->method('createComparator')
231+
->willReturn($comparator);
232+
233+
$this->migrationSqlGenerator->expects(self::exactly(2))
234+
->method('generate')
235+
->willReturnOnConsecutiveCalls('up', 'down');
236+
237+
$this->migrationDiffGenerator->generate('Version1234', null);
238+
}
239+
183240
protected function setUp(): void
184241
{
185242
$this->dbalConfiguration = $this->createMock(DBALConfiguration::class);

0 commit comments

Comments
 (0)