File tree Expand file tree Collapse file tree
Fixture/Entity/EdgeCases/RelationshipOnInterface Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -162,12 +162,23 @@ final protected function reusedAttributes(): array
162162 continue ;
163163 }
164164
165- if (!$ type instanceof \ReflectionNamedType) {
165+ if (!$ type instanceof \ReflectionNamedType || $ type -> isBuiltin () ) {
166166 continue ;
167167 }
168168
169169 if (isset ($ this ->reusedObjects [$ type ->getName ()])) {
170170 $ attributes [$ property ->getName ()] = $ this ->reusedObjects [$ type ->getName ()];
171+
172+ continue ;
173+ }
174+
175+ // test if reused object is a subclass of the property's type
176+ foreach ($ this ->reusedObjects as $ reusedObject ) {
177+ if (is_a ($ reusedObject , $ type ->getName ())) {
178+ $ attributes [$ property ->getName ()] = $ reusedObject ;
179+
180+ break ;
181+ }
171182 }
172183 }
173184
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Zenstruck \Foundry \Tests \Fixture \Entity \EdgeCases \RelationshipOnInterface ;
6+
7+ use Doctrine \ORM \Mapping as ORM ;
8+ use Zenstruck \Foundry \Tests \Fixture \Model \Base ;
9+
10+ #[ORM \Entity]
11+ #[ORM \Table('relationship_on_interface_entity ' )]
12+ class Entity extends Base implements EntityInterface
13+ {
14+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Zenstruck \Foundry \Tests \Fixture \Entity \EdgeCases \RelationshipOnInterface ;
6+
7+ interface EntityInterface
8+ {
9+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Zenstruck \Foundry \Tests \Fixture \Entity \EdgeCases \RelationshipOnInterface ;
6+
7+ use Doctrine \ORM \Mapping as ORM ;
8+ use Zenstruck \Foundry \Tests \Fixture \Model \Base ;
9+
10+ #[ORM \Entity]
11+ #[ORM \Table('relationship_on_interface_other_entity ' )]
12+ class OtherEntity extends Base
13+ {
14+ public function __construct (
15+ #[ORM \ManyToOne(targetEntity: Entity::class)] // @phpstan-ignore doctrine.associationType
16+ #[ORM \JoinColumn(nullable: false )]
17+ public EntityInterface $ entity ,
18+ ) {
19+ }
20+ }
Original file line number Diff line number Diff line change 1616use PHPUnit \Framework \Attributes \Test ;
1717use PHPUnit \Framework \TestCase ;
1818use Zenstruck \Foundry \Test \Factories ;
19+ use Zenstruck \Foundry \Tests \Fixture \Entity \EdgeCases \RelationshipOnInterface ;
1920use Zenstruck \Foundry \Tests \Fixture \Factories \Entity \Address \AddressFactory ;
2021use Zenstruck \Foundry \Tests \Fixture \Factories \Entity \Category \CategoryFactory ;
2122use Zenstruck \Foundry \Tests \Fixture \Factories \Entity \Contact \ContactFactory ;
2223
24+ use function Zenstruck \Foundry \factory ;
25+ use function Zenstruck \Foundry \object ;
26+
2327final class ReuseEntityTest extends TestCase
2428{
2529 use Factories;
@@ -161,4 +165,18 @@ public function reused_object_dont_have_priority_over_states(): void
161165
162166 self ::assertNotSame ($ address , $ contact ->getAddress ());
163167 }
168+
169+ /**
170+ * @test
171+ */
172+ #[Test]
173+ public function reused_object_on_interface_property (): void
174+ {
175+ $ otherEntity = factory (RelationshipOnInterface \OtherEntity::class)
176+ ->reuse ($ entity = object (RelationshipOnInterface \Entity::class))
177+ ->create ()
178+ ;
179+
180+ self ::assertSame ($ entity , $ otherEntity ->entity );
181+ }
164182}
You can’t perform that action at this time.
0 commit comments