From 24af534ac506fb7214f255659fd507d17330e028 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Wed, 20 Dec 2023 14:37:07 +0100 Subject: [PATCH] Allow to skip property type validation --- .../Console/Command/ValidateSchemaCommand.php | 3 ++- lib/Doctrine/ORM/Tools/SchemaValidator.php | 10 ++++--- .../Functional/Ticket/GH10661/GH10661Test.php | 27 ++++++++++++------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php index 7c216445786..3807a81d58d 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php @@ -31,6 +31,7 @@ protected function configure() ->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on') ->addOption('skip-mapping', null, InputOption::VALUE_NONE, 'Skip the mapping validation check') ->addOption('skip-sync', null, InputOption::VALUE_NONE, 'Skip checking if the mapping is in sync with the database') + ->addOption('skip-property-types', null, InputOption::VALUE_NONE, 'Skip checking if property types match the Doctrine types') ->setHelp('Validate that the mapping files are correct and in sync with the database.'); } @@ -39,7 +40,7 @@ private function doExecute(InputInterface $input, OutputInterface $output): int $ui = (new SymfonyStyle($input, $output))->getErrorStyle(); $em = $this->getEntityManager($input); - $validator = new SchemaValidator($em); + $validator = new SchemaValidator($em, ! $input->getOption('skip-property-types')); $exit = 0; $ui->section('Mapping'); diff --git a/lib/Doctrine/ORM/Tools/SchemaValidator.php b/lib/Doctrine/ORM/Tools/SchemaValidator.php index 3fd323c565a..eb8f94d6f39 100644 --- a/lib/Doctrine/ORM/Tools/SchemaValidator.php +++ b/lib/Doctrine/ORM/Tools/SchemaValidator.php @@ -56,6 +56,9 @@ class SchemaValidator /** @var EntityManagerInterface */ private $em; + /** @var bool */ + private $validatePropertyTypes; + /** * It maps built-in Doctrine types to PHP types */ @@ -74,9 +77,10 @@ class SchemaValidator TextType::class => 'string', ]; - public function __construct(EntityManagerInterface $em) + public function __construct(EntityManagerInterface $em, bool $validatePropertyTypes = true) { - $this->em = $em; + $this->em = $em; + $this->validatePropertyTypes = $validatePropertyTypes; } /** @@ -136,7 +140,7 @@ public function validateClass(ClassMetadataInfo $class) } // PHP 7.4 introduces the ability to type properties, so we can't validate them in previous versions - if (PHP_VERSION_ID >= 70400) { + if (PHP_VERSION_ID >= 70400 && $this->validatePropertyTypes) { array_push($ce, ...$this->validatePropertiesTypes($class)); } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH10661/GH10661Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH10661/GH10661Test.php index 6bc7f4a6124..04576aa2619 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH10661/GH10661Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH10661/GH10661Test.php @@ -16,32 +16,36 @@ final class GH10661Test extends OrmTestCase /** @var EntityManagerInterface */ private $em; - /** @var SchemaValidator */ - private $validator; - protected function setUp(): void { - $this->em = $this->getTestEntityManager(); - $this->validator = new SchemaValidator($this->em); + $this->em = $this->getTestEntityManager(); } public function testMetadataFieldTypeNotCoherentWithEntityPropertyType(): void { $class = $this->em->getClassMetadata(InvalidEntity::class); - $ce = $this->validator->validateClass($class); + $ce = $this->bootstrapValidator()->validateClass($class); - self::assertEquals( + self::assertSame( ["The field 'Doctrine\Tests\ORM\Functional\Ticket\GH10661\InvalidEntity#property1' has the property type 'float' that differs from the metadata field type 'string' returned by the 'decimal' DBAL type."], $ce ); } + public function testPropertyTypeErrorsCanBeSilenced(): void + { + $class = $this->em->getClassMetadata(InvalidEntity::class); + $ce = $this->bootstrapValidator(false)->validateClass($class); + + self::assertSame([], $ce); + } + public function testMetadataFieldTypeNotCoherentWithEntityPropertyTypeWithInheritance(): void { $class = $this->em->getClassMetadata(InvalidChildEntity::class); - $ce = $this->validator->validateClass($class); + $ce = $this->bootstrapValidator()->validateClass($class); - self::assertEquals( + self::assertSame( [ "The field 'Doctrine\Tests\ORM\Functional\Ticket\GH10661\InvalidChildEntity#property1' has the property type 'float' that differs from the metadata field type 'string' returned by the 'decimal' DBAL type.", "The field 'Doctrine\Tests\ORM\Functional\Ticket\GH10661\InvalidChildEntity#property2' has the property type 'int' that differs from the metadata field type 'string' returned by the 'string' DBAL type.", @@ -50,4 +54,9 @@ public function testMetadataFieldTypeNotCoherentWithEntityPropertyTypeWithInheri $ce ); } + + private function bootstrapValidator(bool $validatePropertyTypes = true): SchemaValidator + { + return new SchemaValidator($this->em, $validatePropertyTypes); + } }