Skip to content

Commit

Permalink
Merge pull request #1381 from ergebnis/feature/instantiator
Browse files Browse the repository at this point in the history
Enhancement: Require and use `doctrine/instantiator` directly
  • Loading branch information
localheinz committed May 5, 2024
2 parents 59bed30 + 96fbe21 commit 8adc4a0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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].
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions phpstan-baseline.neon
Expand Up @@ -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
Expand All @@ -179,18 +184,18 @@ parameters:
path: src/FixtureFactory.php

-
message: "#^Parameter \\#1 \\$className of method Doctrine\\\\ORM\\\\EntityManagerInterface\\:\\:getClassMetadata\\(\\) expects class\\-string\\<object\\>, class\\-string\\<T\\> given\\.$#"
count: 1
message: "#^Parameter \\#1 \\$className of method Doctrine\\\\Instantiator\\\\Instantiator\\:\\:instantiate\\(\\) expects class\\-string\\<object\\>, class\\-string\\<T\\> 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\\<object\\>, class\\-string\\<T\\> 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

-
Expand Down
8 changes: 5 additions & 3 deletions src/FixtureFactory.php
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 8adc4a0

Please sign in to comment.