diff --git a/src/bundle/RepositoryInstaller/Event/Subscriber/OrmEntitiesSchemaSubscriber.php b/src/bundle/RepositoryInstaller/Event/Subscriber/OrmEntitiesSchemaSubscriber.php index bd420da371..8ddf981eba 100644 --- a/src/bundle/RepositoryInstaller/Event/Subscriber/OrmEntitiesSchemaSubscriber.php +++ b/src/bundle/RepositoryInstaller/Event/Subscriber/OrmEntitiesSchemaSubscriber.php @@ -35,20 +35,15 @@ * * Tables already present in the schema are left as they are. */ -final class OrmEntitiesSchemaSubscriber implements EventSubscriberInterface +final readonly class OrmEntitiesSchemaSubscriber implements EventSubscriberInterface { - private EntityManagerInterface $entityManager; - - /** @var list */ - private array $entityClasses; - /** * @param list $entityClasses */ - public function __construct(EntityManagerInterface $entityManager, array $entityClasses) - { - $this->entityManager = $entityManager; - $this->entityClasses = $entityClasses; + public function __construct( + private EntityManagerInterface $entityManager, + private array $entityClasses + ) { } public static function getSubscribedEvents(): array @@ -62,9 +57,7 @@ public function onBuildSchema(SchemaBuilderEvent $event): void { $metadataFactory = $this->entityManager->getMetadataFactory(); $classMetadata = array_map( - static function (string $class) use ($metadataFactory): ClassMetadata { - return $metadataFactory->getMetadataFor($class); - }, + static fn (string $class): ClassMetadata => $metadataFactory->getMetadataFor($class), $this->entityClasses, ); @@ -77,9 +70,7 @@ static function (string $class) use ($metadataFactory): ClassMetadata { // own table, regardless of which classes were actually requested here. Only transplant the // tables that genuinely belong to the requested entities. $ownTableNames = array_map( - static function (ClassMetadata $metadata): string { - return $metadata->getTableName(); - }, + static fn (ClassMetadata $metadata): string => $metadata->getTableName(), $classMetadata, ); @@ -97,11 +88,7 @@ static function (ClassMetadata $metadata): string { } $ownSequenceNames = array_filter(array_map( - static function (ClassMetadata $metadata): ?string { - return isset($metadata->sequenceGeneratorDefinition['sequenceName']) - ? $metadata->sequenceGeneratorDefinition['sequenceName'] - : null; - }, + static fn (ClassMetadata $metadata): ?string => $metadata->sequenceGeneratorDefinition['sequenceName'] ?? null, $classMetadata, )); @@ -129,8 +116,10 @@ static function (ClassMetadata $metadata): ?string { * SchemaImporter uses when building a table from a parsed Yaml array, just reading from an * already-built Table here instead. */ - private function copyTable(Table $source, Table $target): void - { + private function copyTable( + Table $source, + Table $target + ): void { foreach ($source->getColumns() as $column) { $options = [ 'length' => $column->getLength(), @@ -170,8 +159,8 @@ private function copyTable(Table $source, Table $target): void foreach ($source->getForeignKeys() as $foreignKey) { $target->addForeignKeyConstraint( $foreignKey->getForeignTableName(), - $foreignKey->getUnquotedLocalColumns(), - $foreignKey->getUnquotedForeignColumns(), + array_values($foreignKey->getUnquotedLocalColumns()), + array_values($foreignKey->getUnquotedForeignColumns()), $foreignKey->getOptions(), $foreignKey->getName(), ); diff --git a/tests/bundle/RepositoryInstaller/Event/Subscriber/OrmEntitiesSchemaSubscriberTest.php b/tests/bundle/RepositoryInstaller/Event/Subscriber/OrmEntitiesSchemaSubscriberTest.php index 55951b01cb..cdfd5fe3c5 100644 --- a/tests/bundle/RepositoryInstaller/Event/Subscriber/OrmEntitiesSchemaSubscriberTest.php +++ b/tests/bundle/RepositoryInstaller/Event/Subscriber/OrmEntitiesSchemaSubscriberTest.php @@ -11,13 +11,11 @@ use Doctrine\Common\EventManager; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Platforms\AbstractPlatform; -use Doctrine\DBAL\Platforms\MySQL57Platform; -use Doctrine\DBAL\Platforms\PostgreSQL94Platform; -use Doctrine\DBAL\Platforms\SqlitePlatform; +use Doctrine\DBAL\Platforms\MySQLPlatform; +use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Sequence; -use Doctrine\DBAL\Schema\Table; use Doctrine\ORM\Configuration; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; @@ -49,9 +47,10 @@ final class OrmEntitiesSchemaSubscriberTest extends TestCase protected function setUp(): void { $configuration = new Configuration(); - $configuration->setMetadataDriverImpl(new SimplifiedXmlDriver([ - __DIR__ . '/Fixtures/orm' => 'Ibexa\Tests\Bundle\RepositoryInstaller\Event\Subscriber\Fixtures\Entity', - ])); + $configuration->setMetadataDriverImpl(new SimplifiedXmlDriver( + [__DIR__ . '/Fixtures/orm' => 'Ibexa\Tests\Bundle\RepositoryInstaller\Event\Subscriber\Fixtures\Entity'], + isXsdValidationEnabled: true, + )); $configuration->setProxyDir(sys_get_temp_dir()); $configuration->setProxyNamespace('Ibexa\Tests\Bundle\RepositoryInstaller\Event\Subscriber\Proxies'); @@ -77,13 +76,14 @@ public function testAddsTablesOfListedEntitiesAsSchemaToolGeneratesThem(Abstract } /** - * @return iterable + * @return iterable */ public static function providePlatforms(): iterable { - yield 'MySQL' => [new MySQL57Platform()]; - yield 'PostgreSQL' => [new PostgreSQL94Platform()]; - yield 'SQLite' => [new SqlitePlatform()]; + yield 'MySQL' => [new MySQLPlatform()]; + yield 'PostgreSQL' => [new PostgreSQLPlatform()]; + // The SQLite platform class was renamed between DBAL 3 and 4 - let DBAL pick it. + yield 'SQLite' => [DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true])->getDatabasePlatform()]; } public function testAddsSequencesOfListedEntitiesAsSchemaToolGeneratesThem(): void @@ -123,9 +123,7 @@ public function testLeavesTablesAlreadyInTheSchemaUntouched(): void $schema = $this->dispatch($schema); $columnNames = array_map( - static function (Column $column): string { - return $column->getName(); - }, + static fn (Column $column): string => $column->getName(), array_values($schema->getTable('test_orm_category')->getColumns()), ); @@ -138,27 +136,19 @@ private function getSchemaToolSchema(): Schema $metadataFactory = $this->entityManager->getMetadataFactory(); return (new SchemaTool($this->entityManager))->getSchemaFromMetadata(array_map( - static function (string $class) use ($metadataFactory) { - return $metadataFactory->getMetadataFor($class); - }, + static fn (string $class) => $metadataFactory->getMetadataFor($class), self::ENTITY_CLASSES, )); } /** - * @return array> + * @return list */ - private function getCreateTablesSql(Schema $schema, AbstractPlatform $platform): array - { - return array_map( - static function (Table $table) use ($platform): array { - return $platform->getCreateTableSQL( - $table, - AbstractPlatform::CREATE_INDEXES | AbstractPlatform::CREATE_FOREIGNKEYS, - ); - }, - $schema->getTables(), - ); + private function getCreateTablesSql( + Schema $schema, + AbstractPlatform $platform + ): array { + return $platform->getCreateTablesSQL($schema->getTables()); } /** @@ -168,12 +158,10 @@ static function (Table $table) use ($platform): array { */ private function getCreateSequencesSql(Schema $schema): array { - $platform = new PostgreSQL94Platform(); + $platform = new PostgreSQLPlatform(); return array_map( - static function (Sequence $sequence) use ($platform): string { - return $platform->getCreateSequenceSQL($sequence); - }, + static fn (Sequence $sequence): string => $platform->getCreateSequenceSQL($sequence), $schema->getSequences(), ); }