Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6c81a0

Browse files
authored
fix: can use reuse with inheritance (#914)
1 parent 05386ab commit f6c81a0

5 files changed

Lines changed: 73 additions & 1 deletion

File tree

src/ObjectFactory.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff 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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\RelationshipOnInterface;
6+
7+
interface EntityInterface
8+
{
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

tests/Unit/ReuseEntityTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
use PHPUnit\Framework\Attributes\Test;
1717
use PHPUnit\Framework\TestCase;
1818
use Zenstruck\Foundry\Test\Factories;
19+
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\RelationshipOnInterface;
1920
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Address\AddressFactory;
2021
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Category\CategoryFactory;
2122
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ContactFactory;
2223

24+
use function Zenstruck\Foundry\factory;
25+
use function Zenstruck\Foundry\object;
26+
2327
final 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
}

0 commit comments

Comments
 (0)