From c31d65346f2196995f2ab48d9136c457807e9c32 Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Wed, 22 Mar 2023 18:08:37 +0100 Subject: [PATCH 1/2] fix(RepositoryProxy::find()): allow not entity object in (#441) --- src/RepositoryProxy.php | 3 ++- .../Factories/EntityWithEnumFactory.php | 23 +++++++++++++++++++ tests/Functional/ORMModelFactoryTest.php | 17 ++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/Factories/EntityWithEnumFactory.php diff --git a/src/RepositoryProxy.php b/src/RepositoryProxy.php index ab246c620..a714f6aa2 100644 --- a/src/RepositoryProxy.php +++ b/src/RepositoryProxy.php @@ -16,6 +16,7 @@ use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Mapping\ClassMetadata as ORMClassMetadata; +use Doctrine\ORM\Mapping\MappingException as ORMMappingException; use Doctrine\Persistence\Mapping\MappingException; use Doctrine\Persistence\ObjectManager; use Doctrine\Persistence\ObjectRepository; @@ -332,7 +333,7 @@ public function find($criteria) try { $metadataForAttribute = $this->getObjectManager()->getClassMetadata($attributeValue::class); - } catch (MappingException) { + } catch (MappingException|ORMMappingException) { $normalizedCriteria[$attributeName] = $attributeValue; continue; diff --git a/tests/Fixtures/Factories/EntityWithEnumFactory.php b/tests/Fixtures/Factories/EntityWithEnumFactory.php new file mode 100644 index 000000000..8091156fe --- /dev/null +++ b/tests/Fixtures/Factories/EntityWithEnumFactory.php @@ -0,0 +1,23 @@ + self::faker()->randomElement(SomeEnum::cases()), + ]; + } + + protected static function getClass(): string + { + return EntityWithEnum::class; + } +} diff --git a/tests/Functional/ORMModelFactoryTest.php b/tests/Functional/ORMModelFactoryTest.php index 0271cbea2..5b0080b11 100644 --- a/tests/Functional/ORMModelFactoryTest.php +++ b/tests/Functional/ORMModelFactoryTest.php @@ -18,6 +18,7 @@ use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory; use Zenstruck\Foundry\Tests\Fixtures\Factories\CommentFactory; use Zenstruck\Foundry\Tests\Fixtures\Factories\ContactFactory; +use Zenstruck\Foundry\Tests\Fixtures\Factories\EntityWithEnumFactory; use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory; use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithInvalidInitialize; use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithNullInitialize; @@ -25,6 +26,7 @@ use Zenstruck\Foundry\Tests\Fixtures\Factories\SpecificPostFactory; use Zenstruck\Foundry\Tests\Fixtures\Factories\TagFactory; use Zenstruck\Foundry\Tests\Fixtures\Factories\UserFactory; +use Zenstruck\Foundry\Tests\Fixtures\PHP81\SomeEnum; /** * @author Kevin Bond @@ -605,6 +607,21 @@ public function can_find_or_create_from_proxy_of_object(): void self::assertSame($comment->object(), $comment2->object()); } + /** + * @test + * @requires PHP 8.1 + */ + public function can_find_or_create_entity_with_enum(): void + { + $entityWithEnum = EntityWithEnumFactory::findOrCreate($attributes = ['enum' => SomeEnum::VALUE]); + EntityWithEnumFactory::assert()->count(1); + + $entityWithEnum2 = EntityWithEnumFactory::findOrCreate($attributes); + EntityWithEnumFactory::assert()->count(1); + + self::assertSame($entityWithEnum->object(), $entityWithEnum2->object()); + } + protected function categoryClass(): string { return Category::class; From 3f7a6e7393889ad2ffa4b3810e3ab142b7b9bccf Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Wed, 22 Mar 2023 18:11:29 +0100 Subject: [PATCH 2/2] changelog: update [skip ci] --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ce47d147..7ca0022b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## [v1.30.3](https://github.com/zenstruck/foundry/releases/tag/v1.30.3) + +March 22nd, 2023 - [v1.30.2...v1.30.3](https://github.com/zenstruck/foundry/compare/v1.30.2...v1.30.3) + +* c31d653 fix(RepositoryProxy::find()): allow not entity object in (#441) by @nikophil + ## [v1.30.2](https://github.com/zenstruck/foundry/releases/tag/v1.30.2) March 22nd, 2023 - [v1.30.1...v1.30.2](https://github.com/zenstruck/foundry/compare/v1.30.1...v1.30.2)