|
12 | 12 | use Doctrine\DBAL\Schema\Name\UnqualifiedName; |
13 | 13 | use Doctrine\DBAL\Schema\PrimaryKeyConstraint; |
14 | 14 | use Doctrine\DBAL\Schema\PrimaryKeyConstraintEditor; |
| 15 | +use Doctrine\DBAL\Schema\Schema; |
15 | 16 | use Doctrine\DBAL\Schema\Table as DbalTable; |
16 | 17 | use Doctrine\DBAL\Types\EnumType; |
17 | 18 | use Doctrine\DBAL\Types\Types; |
|
53 | 54 | use Doctrine\Tests\Models\NullDefault\NullDefaultColumn; |
54 | 55 | use Doctrine\Tests\OrmTestCase; |
55 | 56 | use PHPUnit\Framework\Attributes\Group; |
| 57 | +use PHPUnit\Framework\Attributes\RequiresMethod; |
56 | 58 |
|
57 | 59 | use function array_map; |
58 | 60 | use function class_exists; |
@@ -527,6 +529,62 @@ public function testCompositeIdPositionRespectedInSchema(): void |
527 | 529 | self::assertSame(['first', 'second', 'third'], $pkColumns); |
528 | 530 | } |
529 | 531 |
|
| 532 | + #[RequiresMethod(Schema::class, 'edit')] |
| 533 | + public function testOverwritingSchemaInListener(): void |
| 534 | + { |
| 535 | + $em = $this->getTestEntityManager(); |
| 536 | + |
| 537 | + $em->getEventManager()->addEventListener( |
| 538 | + [ToolEvents::postGenerateSchemaTable, ToolEvents::postGenerateSchema], |
| 539 | + new class () { |
| 540 | + public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs): void |
| 541 | + { |
| 542 | + $eventArgs->setSchema(new Schema()); |
| 543 | + } |
| 544 | + |
| 545 | + public function postGenerateSchema(GenerateSchemaEventArgs $eventArgs): void |
| 546 | + { |
| 547 | + $eventArgs->setSchema(new Schema()); |
| 548 | + } |
| 549 | + }, |
| 550 | + ); |
| 551 | + $schemaTool = new SchemaTool($em); |
| 552 | + |
| 553 | + $schema = $schemaTool->getSchemaFromMetadata([$em->getClassMetadata(CmsAddress::class)]); |
| 554 | + |
| 555 | + self::assertFalse( |
| 556 | + $schema->hasTable('cms_address'), |
| 557 | + 'The original schema should have been overwritten by the listener, so cms_address table should not exist.', |
| 558 | + ); |
| 559 | + } |
| 560 | + |
| 561 | + #[RequiresMethod(Schema::class, 'edit')] |
| 562 | + public function testOverwritingTableInListener(): void |
| 563 | + { |
| 564 | + $em = $this->getTestEntityManager(); |
| 565 | + |
| 566 | + $em->getEventManager()->addEventListener( |
| 567 | + [ToolEvents::postGenerateSchemaTable], |
| 568 | + new class () { |
| 569 | + public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs): void |
| 570 | + { |
| 571 | + $eventArgs->setClassTable(new DbalTable('just_address')); |
| 572 | + } |
| 573 | + }, |
| 574 | + ); |
| 575 | + $schemaTool = new SchemaTool($em); |
| 576 | + |
| 577 | + $schema = $schemaTool->getSchemaFromMetadata([$em->getClassMetadata(CmsAddress::class)]); |
| 578 | + self::assertTrue( |
| 579 | + $schema->hasTable('just_address'), |
| 580 | + 'The original table should have been overwritten by the listener, so just_address table should exist.', |
| 581 | + ); |
| 582 | + self::assertFalse( |
| 583 | + $schema->hasTable('cms_address'), |
| 584 | + 'The original table should have been overwritten by the listener, so cms_address table should not exist.', |
| 585 | + ); |
| 586 | + } |
| 587 | + |
530 | 588 | /** @return string[] */ |
531 | 589 | private static function getIndexedColumns(DbalIndex $index): array |
532 | 590 | { |
|
0 commit comments