Skip to content

Commit 61ae401

Browse files
committed
test: add functional test for union type
1 parent 8be8b67 commit 61ae401

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\OneToManyWithUnionType;
15+
16+
use Doctrine\Common\Collections\Collection;
17+
use Doctrine\ORM\Mapping as ORM;
18+
use Zenstruck\Foundry\Tests\Fixture\Model\Base;
19+
20+
#[ORM\Entity]
21+
class HasOneToManyWithUnionType extends Base
22+
{
23+
public function __construct(
24+
/** @var Collection<int,OwningSideEntity>|list<OwningSideEntity> */
25+
#[ORM\OneToMany(targetEntity: OwningSideEntity::class, mappedBy: 'item')] // @phpstan-ignore doctrine.associationType, doctrine.associationType
26+
public Collection|array $collection
27+
) {
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\OneToManyWithUnionType;
15+
16+
use Doctrine\ORM\Mapping as ORM;
17+
use Zenstruck\Foundry\Tests\Fixture\Model\Base;
18+
19+
#[ORM\Entity]
20+
#[ORM\Table('union_type_owning_side_entity')]
21+
class OwningSideEntity extends Base
22+
{
23+
public function __construct(
24+
#[ORM\ManyToOne(inversedBy: 'collections')]
25+
#[ORM\JoinColumn(nullable: false)]
26+
public HasOneToManyWithUnionType $item,
27+
) {
28+
}
29+
}

Diff for: tests/Integration/ORM/EdgeCasesRelationshipTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace Zenstruck\Foundry\Tests\Integration\ORM;
1515

16+
use Doctrine\Common\Collections\ArrayCollection;
17+
use Doctrine\Common\Collections\Collection;
1618
use PHPUnit\Framework\Attributes\DataProvider;
1719
use PHPUnit\Framework\Attributes\RequiresPhpunit;
1820
use PHPUnit\Framework\Attributes\Test;
@@ -29,6 +31,7 @@
2931
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\RichDomainMandatoryRelationship;
3032
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\EdgeCases\MultipleMandatoryRelationshipToSameEntity;
3133
use Zenstruck\Foundry\Tests\Integration\RequiresORM;
34+
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\OneToManyWithUnionType;
3235

3336
use function Zenstruck\Foundry\Persistence\persistent_factory;
3437

@@ -158,6 +161,26 @@ public function indexed_one_to_many(): void
158161
self::assertNotNull($parent->getItems()->get('en')); // @phpstan-ignore argument.type
159162
}
160163

164+
/** @test */
165+
#[Test]
166+
public function object_with_union_type(): void
167+
{
168+
$owningSideFactory = persistent_factory(OneToManyWithUnionType\OwningSideEntity::class);
169+
$hasOneToManyWithUnionTypeFactory = persistent_factory(OneToManyWithUnionType\HasOneToManyWithUnionType::class);
170+
171+
$object = $hasOneToManyWithUnionTypeFactory->create(
172+
[
173+
'collection' => $owningSideFactory->many(2),
174+
]
175+
);
176+
177+
$owningSideFactory::assert()->count(2);
178+
$hasOneToManyWithUnionTypeFactory::assert()->count(1);
179+
180+
self::assertCount(2, $object->collection);
181+
self::assertInstanceOf(Collection::class, $object->collection);
182+
}
183+
161184
/**
162185
* @test
163186
*/

Diff for: tests/Unit/Object/HydratorTest.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Common\Collections\ArrayCollection;
1515
use Doctrine\Common\Collections\Collection;
1616
use Doctrine\Common\Collections\Selectable;
17+
use PHPUnit\Framework\Attributes\Test;
1718
use PHPUnit\Framework\TestCase;
1819
use Zenstruck\Foundry\Object\Hydrator;
1920
use Zenstruck\Foundry\Tests\Fixture\Object1;
@@ -26,6 +27,7 @@ class HydratorTest extends TestCase
2627
/**
2728
* @test
2829
*/
30+
#[Test]
2931
public function can_hydrate_scalar(): void
3032
{
3133
$value = 'Hello world';
@@ -42,6 +44,7 @@ public function can_hydrate_scalar(): void
4244
/**
4345
* @test
4446
*/
47+
#[Test]
4548
public function can_hydrate_scalar_array(): void
4649
{
4750
$value = ['foo', 'bar'];
@@ -58,6 +61,7 @@ public function can_hydrate_scalar_array(): void
5861
/**
5962
* @test
6063
*/
64+
#[Test]
6165
public function can_hydrate_object(): void
6266
{
6367
$object = new class {
@@ -79,6 +83,7 @@ public function __construct()
7983
/**
8084
* @test
8185
*/
86+
#[Test]
8287
public function can_hydrate_object_array(): void
8388
{
8489
$object = new class {
@@ -99,6 +104,7 @@ public function can_hydrate_object_array(): void
99104
/**
100105
* @test
101106
*/
107+
#[Test]
102108
public function can_hydrate_doctrine_collection(): void
103109
{
104110
$object = new class {
@@ -125,7 +131,8 @@ public function __construct()
125131
/**
126132
* @test
127133
*/
128-
public function can_hydrate_doctrine_collection_intersection(): void
134+
#[Test]
135+
public function can_hydrate_doctrine_collection_union(): void
129136
{
130137
$object = new class {
131138
/** @var Collection<array-key, Object1>|Selectable<array-key, Object1> */
@@ -151,7 +158,8 @@ public function __construct()
151158
/**
152159
* @test
153160
*/
154-
public function can_hydrate_doctrine_collection_union(): void
161+
#[Test]
162+
public function can_hydrate_doctrine_collection_intersection(): void
155163
{
156164
$object = new class {
157165
/** @var Collection<array-key, Object1>&Selectable<array-key, Object1> */

0 commit comments

Comments
 (0)