From 9c5889ba697b1ce43d8bd1eff66721eaed88b29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Fr=C3=B6mer?= Date: Wed, 2 Feb 2022 05:28:30 +0100 Subject: [PATCH 1/3] Only load doctrine/persistence class alias if classes exist --- src/Faker/ORM/Doctrine/backward-compatibility.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Faker/ORM/Doctrine/backward-compatibility.php b/src/Faker/ORM/Doctrine/backward-compatibility.php index 6f545f87b8..86867057ec 100644 --- a/src/Faker/ORM/Doctrine/backward-compatibility.php +++ b/src/Faker/ORM/Doctrine/backward-compatibility.php @@ -2,10 +2,10 @@ declare(strict_types=1); -if (!class_exists('Doctrine\Common\Persistence\Mapping\ClassMetadata')) { +if (class_exists('\Doctrine\Persistence\Mapping\ClassMetadata') && !class_exists('Doctrine\Common\Persistence\Mapping\ClassMetadata')) { class_alias(\Doctrine\Persistence\Mapping\ClassMetadata::class, 'Doctrine\Common\Persistence\Mapping\ClassMetadata'); } -if (!class_exists('Doctrine\Common\Persistence\ObjectManager')) { +if (class_exists('\Doctrine\Persistence\ObjectManager') && !class_exists('Doctrine\Common\Persistence\ObjectManager')) { class_alias(\Doctrine\Persistence\ObjectManager::class, 'Doctrine\Common\Persistence\ObjectManager'); } From edefb8d62ac967f10ad2d0076cbef39adb0b2764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Fr=C3=B6mer?= Date: Wed, 2 Feb 2022 06:01:05 +0100 Subject: [PATCH 2/3] Only load class alias when using Faker\ORM\Doctrine classes. This will also add autoload for phpstan to register the alias. --- composer.json | 5 +---- phpstan.neon.dist | 2 ++ src/Faker/ORM/Doctrine/ColumnTypeGuesser.php | 2 ++ src/Faker/ORM/Doctrine/EntityPopulator.php | 2 ++ src/Faker/ORM/Doctrine/Populator.php | 2 ++ src/Faker/ORM/Doctrine/backward-compatibility.php | 4 ++-- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 3de6aa55ce..7303a15589 100644 --- a/composer.json +++ b/composer.json @@ -27,10 +27,7 @@ "autoload": { "psr-4": { "Faker\\": "src/Faker/" - }, - "files": [ - "src/Faker/ORM/Doctrine/backward-compatibility.php" - ] + } }, "autoload-dev": { "psr-4": { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index d302ac8fcd..d23694b6cb 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -3,6 +3,8 @@ includes: parameters: level: 5 + bootstrapFiles: + - src/Faker/ORM/Doctrine/backward-compatibility.php paths: - src tmpDir: .build/phpstan/ diff --git a/src/Faker/ORM/Doctrine/ColumnTypeGuesser.php b/src/Faker/ORM/Doctrine/ColumnTypeGuesser.php index 0672d1d7d3..3267fe469b 100644 --- a/src/Faker/ORM/Doctrine/ColumnTypeGuesser.php +++ b/src/Faker/ORM/Doctrine/ColumnTypeGuesser.php @@ -5,6 +5,8 @@ use Doctrine\Common\Persistence\Mapping\ClassMetadata; use Faker\Generator; +require_once 'backward-compatibility.php'; + class ColumnTypeGuesser { protected $generator; diff --git a/src/Faker/ORM/Doctrine/EntityPopulator.php b/src/Faker/ORM/Doctrine/EntityPopulator.php index 8bd8bede66..79d3796b8a 100644 --- a/src/Faker/ORM/Doctrine/EntityPopulator.php +++ b/src/Faker/ORM/Doctrine/EntityPopulator.php @@ -5,6 +5,8 @@ use Doctrine\Common\Persistence\Mapping\ClassMetadata; use Doctrine\Common\Persistence\ObjectManager; +require_once 'backward-compatibility.php'; + /** * Service class for populating a table through a Doctrine Entity class. */ diff --git a/src/Faker/ORM/Doctrine/Populator.php b/src/Faker/ORM/Doctrine/Populator.php index a1284edac3..893d856627 100644 --- a/src/Faker/ORM/Doctrine/Populator.php +++ b/src/Faker/ORM/Doctrine/Populator.php @@ -5,6 +5,8 @@ use Doctrine\Common\Persistence\ObjectManager; use Faker\Generator; +require_once 'backward-compatibility.php'; + /** * Service class for populating a database using the Doctrine ORM or ODM. * A Populator can populate several tables using ActiveRecord classes. diff --git a/src/Faker/ORM/Doctrine/backward-compatibility.php b/src/Faker/ORM/Doctrine/backward-compatibility.php index 86867057ec..6f545f87b8 100644 --- a/src/Faker/ORM/Doctrine/backward-compatibility.php +++ b/src/Faker/ORM/Doctrine/backward-compatibility.php @@ -2,10 +2,10 @@ declare(strict_types=1); -if (class_exists('\Doctrine\Persistence\Mapping\ClassMetadata') && !class_exists('Doctrine\Common\Persistence\Mapping\ClassMetadata')) { +if (!class_exists('Doctrine\Common\Persistence\Mapping\ClassMetadata')) { class_alias(\Doctrine\Persistence\Mapping\ClassMetadata::class, 'Doctrine\Common\Persistence\Mapping\ClassMetadata'); } -if (class_exists('\Doctrine\Persistence\ObjectManager') && !class_exists('Doctrine\Common\Persistence\ObjectManager')) { +if (!class_exists('Doctrine\Common\Persistence\ObjectManager')) { class_alias(\Doctrine\Persistence\ObjectManager::class, 'Doctrine\Common\Persistence\ObjectManager'); } From 405e7de6a1f455dd856b7ed59e0e5f274cffce8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Fr=C3=B6mer?= Date: Wed, 2 Feb 2022 06:30:10 +0100 Subject: [PATCH 3/3] Add tests to check if backward compatibility is working Each test will run in a separate process to make sure the autoload is done in every single one of them. --- .../ORM/Doctrine/ColumnTypeGuesserTest.php | 25 ++++++++++++++++++ .../ORM/Doctrine/EntityPopulatorTest.php | 26 +++++++++++++++++++ test/Faker/ORM/Doctrine/PopulatorTest.php | 23 ++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 test/Faker/ORM/Doctrine/ColumnTypeGuesserTest.php create mode 100644 test/Faker/ORM/Doctrine/EntityPopulatorTest.php create mode 100644 test/Faker/ORM/Doctrine/PopulatorTest.php diff --git a/test/Faker/ORM/Doctrine/ColumnTypeGuesserTest.php b/test/Faker/ORM/Doctrine/ColumnTypeGuesserTest.php new file mode 100644 index 0000000000..5b105793b2 --- /dev/null +++ b/test/Faker/ORM/Doctrine/ColumnTypeGuesserTest.php @@ -0,0 +1,25 @@ +faker); + // Mock ClassMetadata after autoload to test class alias + $classMetaData = $this->createMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); + $classMetaData->method('getTypeOfField')->with(self::anything())->willReturn('integer'); + + $fakerClosure = $columnTypeGuesser->guessFormat('test', $classMetaData); + self::assertIsNumeric($fakerClosure()); + } +} diff --git a/test/Faker/ORM/Doctrine/EntityPopulatorTest.php b/test/Faker/ORM/Doctrine/EntityPopulatorTest.php new file mode 100644 index 0000000000..a538785f59 --- /dev/null +++ b/test/Faker/ORM/Doctrine/EntityPopulatorTest.php @@ -0,0 +1,26 @@ +createMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); + $classMetaData->method('getName')->willReturn('test'); + $entityPopulator = new EntityPopulator($classMetaData); + + self::assertSame('test', $entityPopulator->getClass()); + } +} diff --git a/test/Faker/ORM/Doctrine/PopulatorTest.php b/test/Faker/ORM/Doctrine/PopulatorTest.php new file mode 100644 index 0000000000..34bfb4d8fa --- /dev/null +++ b/test/Faker/ORM/Doctrine/PopulatorTest.php @@ -0,0 +1,23 @@ +faker); + // Mock ObjectManager after autoload to test class alias + $objectManager = $this->createMock('Doctrine\Common\Persistence\ObjectManager'); + + self::assertEmpty($populator->execute($objectManager)); + } +}