|
18 | 18 | use PHPUnit\Framework\MockObject\MockObject;
|
19 | 19 | use PHPUnit\Framework\TestCase;
|
20 | 20 |
|
| 21 | +use function preg_match; |
| 22 | + |
21 | 23 | class DiffGeneratorTest extends TestCase
|
22 | 24 | {
|
23 | 25 | private DBALConfiguration&MockObject $dbalConfiguration;
|
@@ -180,6 +182,61 @@ public function testGenerateFromEmptySchema(): void
|
180 | 182 | self::assertSame('path2', $this->migrationDiffGenerator->generate('2345', null, false, 120, true, true));
|
181 | 183 | }
|
182 | 184 |
|
| 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 | + |
183 | 240 | protected function setUp(): void
|
184 | 241 | {
|
185 | 242 | $this->dbalConfiguration = $this->createMock(DBALConfiguration::class);
|
|
0 commit comments