Skip to content

Commit c31d653

Browse files
authored
fix(RepositoryProxy::find()): allow not entity object in (#441)
1 parent 4ea400e commit c31d653

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/RepositoryProxy.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\ORM\EntityManagerInterface;
1717
use Doctrine\ORM\EntityRepository;
1818
use Doctrine\ORM\Mapping\ClassMetadata as ORMClassMetadata;
19+
use Doctrine\ORM\Mapping\MappingException as ORMMappingException;
1920
use Doctrine\Persistence\Mapping\MappingException;
2021
use Doctrine\Persistence\ObjectManager;
2122
use Doctrine\Persistence\ObjectRepository;
@@ -332,7 +333,7 @@ public function find($criteria)
332333

333334
try {
334335
$metadataForAttribute = $this->getObjectManager()->getClassMetadata($attributeValue::class);
335-
} catch (MappingException) {
336+
} catch (MappingException|ORMMappingException) {
336337
$normalizedCriteria[$attributeName] = $attributeValue;
337338

338339
continue;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Zenstruck\Foundry\Tests\Fixtures\Factories;
4+
5+
use Zenstruck\Foundry\ModelFactory;
6+
use Zenstruck\Foundry\Proxy;
7+
use Zenstruck\Foundry\Tests\Fixtures\PHP81\EntityWithEnum;
8+
use Zenstruck\Foundry\Tests\Fixtures\PHP81\SomeEnum;
9+
10+
final class EntityWithEnumFactory extends ModelFactory
11+
{
12+
protected function getDefaults(): array
13+
{
14+
return [
15+
'enum' => self::faker()->randomElement(SomeEnum::cases()),
16+
];
17+
}
18+
19+
protected static function getClass(): string
20+
{
21+
return EntityWithEnum::class;
22+
}
23+
}

tests/Functional/ORMModelFactoryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory;
1919
use Zenstruck\Foundry\Tests\Fixtures\Factories\CommentFactory;
2020
use Zenstruck\Foundry\Tests\Fixtures\Factories\ContactFactory;
21+
use Zenstruck\Foundry\Tests\Fixtures\Factories\EntityWithEnumFactory;
2122
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory;
2223
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithInvalidInitialize;
2324
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithNullInitialize;
2425
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithValidInitialize;
2526
use Zenstruck\Foundry\Tests\Fixtures\Factories\SpecificPostFactory;
2627
use Zenstruck\Foundry\Tests\Fixtures\Factories\TagFactory;
2728
use Zenstruck\Foundry\Tests\Fixtures\Factories\UserFactory;
29+
use Zenstruck\Foundry\Tests\Fixtures\PHP81\SomeEnum;
2830

2931
/**
3032
* @author Kevin Bond <[email protected]>
@@ -605,6 +607,21 @@ public function can_find_or_create_from_proxy_of_object(): void
605607
self::assertSame($comment->object(), $comment2->object());
606608
}
607609

610+
/**
611+
* @test
612+
* @requires PHP 8.1
613+
*/
614+
public function can_find_or_create_entity_with_enum(): void
615+
{
616+
$entityWithEnum = EntityWithEnumFactory::findOrCreate($attributes = ['enum' => SomeEnum::VALUE]);
617+
EntityWithEnumFactory::assert()->count(1);
618+
619+
$entityWithEnum2 = EntityWithEnumFactory::findOrCreate($attributes);
620+
EntityWithEnumFactory::assert()->count(1);
621+
622+
self::assertSame($entityWithEnum->object(), $entityWithEnum2->object());
623+
}
624+
608625
protected function categoryClass(): string
609626
{
610627
return Category::class;

0 commit comments

Comments
 (0)