Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Allow installation of doctrine/orm:^3.0.0 #1293

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ For a full diff see [`1.6.0...main`][1.6.0...main].

### Changed

- Allowed installation of `doctrine/orm:^3.0.0` ([#1293]), by [@localheinz]
- Required `doctrine/orm:^2.14.0` ([#1298]), by [@localheinz]

## [`1.6.0`][1.6.0]
Expand Down Expand Up @@ -359,6 +360,7 @@ For a full diff see [`fa9c564...0.1.0`][fa9c564...0.1.0].
[#1213]: https://github.com/ergebnis/factory-bot/pull/1213
[#1233]: https://github.com/ergebnis/factory-bot/pull/1233
[#1234]: https://github.com/ergebnis/factory-bot/pull/1234
[#1293]: https://github.com/ergebnis/factory-bot/pull/1293
[#1298]: https://github.com/ergebnis/factory-bot/pull/1298

[@abenerd]: https://github.com/abenerd
Expand Down
4 changes: 3 additions & 1 deletion composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"symbol-whitelist": []
"symbol-whitelist": [
"Doctrine\\ORM\\Mapping\\AssociationMapping"
]
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +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",
"doctrine/orm": "^2.14.0",
"doctrine/orm": "^2.14.0 || ^3.0.0",
"doctrine/persistence": "^2.1.0 || ^3.0.0",
"ergebnis/classy": "^1.6.0",
"fakerphp/faker": "^1.20.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.

12 changes: 11 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ parameters:
path: src/FieldDefinition/References.php

-
message: "#^Call to function array_key_exists\\(\\) with 'inversedBy' and array\\{cache\\?\\: array, cascade\\: array\\<string\\>, declared\\?\\: class\\-string, fetch\\: mixed, fieldName\\: string, id\\?\\: bool, inherited\\?\\: class\\-string, indexBy\\?\\: string, \\.\\.\\.\\} will always evaluate to true\\.$#"
message: "#^Call to function is_array\\(\\) with array\\{cache\\?\\: array, cascade\\: array\\<string\\>, declared\\?\\: class\\-string, fetch\\: mixed, fieldName\\: string, id\\?\\: bool, inherited\\?\\: class\\-string, indexBy\\?\\: string, \\.\\.\\.\\} will always evaluate to true\\.$#"
count: 1
path: src/FixtureFactory.php

Expand All @@ -173,6 +173,11 @@ parameters:
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:resolveInversedBy\\(\\) has a nullable return type declaration\\.$#"
count: 1
path: src/FixtureFactory.php

-
message: "#^Parameter \\#1 \\$className of method Doctrine\\\\ORM\\\\EntityManagerInterface\\:\\:getClassMetadata\\(\\) expects class\\-string\\<object\\>, class\\-string\\<T\\> given\\.$#"
count: 1
Expand All @@ -193,6 +198,11 @@ parameters:
count: 1
path: src/FixtureFactory.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\Test\\\\DataProvider\\\\IntProvider\\:\\:faker\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
Expand Down
9 changes: 9 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
<MixedAssignment>
<code>$fieldValue</code>
</MixedAssignment>
<MixedInferredReturnType>
<code>?string</code>
</MixedInferredReturnType>
<RedundantConditionGivenDocblockType>
<code>\is_array($association)</code>
</RedundantConditionGivenDocblockType>
<TooManyArguments>
<code><![CDATA[function () use ($className, $fieldDefinitionOverrides) {
return $this->createOne(
Expand All @@ -95,6 +101,9 @@
);
}]]></code>
</TooManyArguments>
<UndefinedClass>
<code>ORM\Mapping\AssociationMapping</code>
</UndefinedClass>
<UnnecessaryVarAnnotation>
<code>EntityDefinition</code>
<code>EntityDefinitionProvider</code>
Expand Down
32 changes: 21 additions & 11 deletions src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,22 +434,15 @@
string $fieldName,
object $fieldValue,
): void {
$association = $classMetadata->getAssociationMapping($fieldName);

if (!\array_key_exists('inversedBy', $association)) {
return;
}

$inversedBy = $association['inversedBy'];
$inversedBy = $this->resolveInversedBy(
$classMetadata,
$fieldName,
);

if (!\is_string($inversedBy)) {
return;
}

if ('' === $inversedBy) {
return;
}

$classMetadataOfFieldValue = $this->entityManager->getClassMetadata($fieldValue::class);

$collection = $classMetadataOfFieldValue->getFieldValue(
Expand All @@ -457,10 +450,27 @@
$inversedBy,
);

if (!$collection instanceof Common\Collections\Collection) {

Check warning on line 453 in src/FixtureFactory.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (8.1, locked)

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ } $classMetadataOfFieldValue = $this->entityManager->getClassMetadata($fieldValue::class); $collection = $classMetadataOfFieldValue->getFieldValue($fieldValue, $inversedBy); - if (!$collection instanceof Common\Collections\Collection) { + if (!true) { return; } $collection->add($entity);
return;
}

$collection->add($entity);
}

private function resolveInversedBy(
ORM\Mapping\ClassMetadata $classMetadata,
string $fieldName,
): ?string {
$association = $classMetadata->getAssociationMapping($fieldName);

if (\is_array($association)) {
return $association['inversedBy'];
}

if ($association instanceof ORM\Mapping\AssociationMapping) {
return $association->inversedBy;
}

throw new \RuntimeException('This should not happen');
}
}
Loading