From 96fbe2139b97c9fb1caf0b19fb1503c770b439f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 5 May 2024 15:24:03 +0200 Subject: [PATCH] Enhancement: Require and use doctrine/instantiator directly --- CHANGELOG.md | 5 +++++ composer.json | 1 + composer.lock | 2 +- phpstan-baseline.neon | 15 ++++++++++----- src/FixtureFactory.php | 8 +++++--- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 163b56fb..53c57437 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), For a full diff see [`1.7.0...main`][1.7.0...main]. +### Changed + +- Required and used `doctrine/instantiator` directly to create new entities ([#1381]), by [@localheinz] + ## [`1.7.0`][1.7.0] For a full diff see [`1.6.0...1.7.0`][1.6.0...1.7.0]. @@ -370,6 +374,7 @@ For a full diff see [`fa9c564...0.1.0`][fa9c564...0.1.0]. [#1293]: https://github.com/ergebnis/factory-bot/pull/1293 [#1298]: https://github.com/ergebnis/factory-bot/pull/1298 [#1299]: https://github.com/ergebnis/factory-bot/pull/1299 +[#1381]: https://github.com/ergebnis/factory-bot/pull/1381 [@abenerd]: https://github.com/abenerd [@localheinz]: https://github.com/localheinz diff --git a/composer.json b/composer.json index 1cbd8c08..8bbdd534 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "php": "~8.1.0 || ~8.2.0 || ~8.3.0", "doctrine/collections": "^1.6.5 || ^2.0.0", "doctrine/dbal": "^2.12.0 || ^3.0.0 || ^4.0.0", + "doctrine/instantiator": "^1.0.0 || ^2.0.0", "doctrine/orm": "^2.14.0 || ^3.0.0", "doctrine/persistence": "^2.1.0 || ^3.0.0", "ergebnis/classy": "^1.6.0", diff --git a/composer.lock b/composer.lock index e9fd0b0c..1a92e112 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a832aebd6f2729f86aecc0684c69a85a", + "content-hash": "775da74ec9a71a74e619904cf5adaad6", "packages": [ { "name": "doctrine/cache", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 556706dc..3b481925 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -163,6 +163,11 @@ parameters: count: 1 path: src/FixtureFactory.php + - + message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:createOne\\(\\) should return T but returns object\\.$#" + count: 1 + path: src/FixtureFactory.php + - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:define\\(\\) has parameter \\$afterCreate with a nullable type declaration\\.$#" count: 1 @@ -179,18 +184,18 @@ parameters: path: src/FixtureFactory.php - - message: "#^Parameter \\#1 \\$className of method Doctrine\\\\ORM\\\\EntityManagerInterface\\:\\:getClassMetadata\\(\\) expects class\\-string\\, class\\-string\\ given\\.$#" - count: 1 + message: "#^Parameter \\#1 \\$className of method Doctrine\\\\Instantiator\\\\Instantiator\\:\\:instantiate\\(\\) expects class\\-string\\, class\\-string\\ given\\.$#" + count: 2 path: src/FixtureFactory.php - - message: "#^Parameter \\#1 \\$entity of method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:setField\\(\\) expects object, T given\\.$#" + message: "#^Parameter \\#1 \\$className of method Doctrine\\\\ORM\\\\EntityManagerInterface\\:\\:getClassMetadata\\(\\) expects class\\-string\\, class\\-string\\ given\\.$#" count: 1 path: src/FixtureFactory.php - - message: "#^Parameter \\#1 \\$object of method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:persist\\(\\) expects object, T given\\.$#" - count: 1 + message: "#^Unable to resolve the template type T in call to method Doctrine\\\\Instantiator\\\\Instantiator\\:\\:instantiate\\(\\)$#" + count: 2 path: src/FixtureFactory.php - diff --git a/src/FixtureFactory.php b/src/FixtureFactory.php index cf341994..28f02b77 100644 --- a/src/FixtureFactory.php +++ b/src/FixtureFactory.php @@ -14,12 +14,14 @@ namespace Ergebnis\FactoryBot; use Doctrine\Common; +use Doctrine\Instantiator; use Doctrine\ORM; use Ergebnis\Classy; use Faker\Generator; final class FixtureFactory { + private Instantiator\Instantiator $instantiator; private FieldResolution\FieldValueResolution\FieldValueResolutionStrategy $fieldValueResolutionStrategy; private FieldResolution\CountResolution\CountResolutionStrategy $countResolutionStrategy; private bool $persistAfterCreate = false; @@ -33,6 +35,7 @@ public function __construct( private ORM\EntityManagerInterface $entityManager, private Generator $faker, ) { + $this->instantiator = new Instantiator\Instantiator(); $this->fieldValueResolutionStrategy = new FieldResolution\FieldValueResolution\WithOrWithoutOptionalFieldValue(); $this->countResolutionStrategy = new FieldResolution\CountResolution\BetweenMinimumAndMaximumCount(); } @@ -100,7 +103,7 @@ public function define( $fieldDefinitions = self::normalizeFieldDefinitions($fieldDefinitions); - $defaultEntity = $classMetadata->newInstance(); + $defaultEntity = $this->instantiator->instantiate($className); foreach ($fieldNames as $fieldName) { if (\array_key_exists($fieldName, $fieldDefinitions)) { @@ -232,8 +235,7 @@ public function createOne( /** @var ORM\Mapping\ClassMetadata $classMetadata */ $classMetadata = $entityDefinition->classMetadata(); - /** @var T $entity */ - $entity = $classMetadata->newInstance(); + $entity = $this->instantiator->instantiate($className); $fieldDefinitions = \array_merge( $entityDefinition->fieldDefinitions(),