1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Utility; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Utility\NormalizeIdentifier; |
8
|
|
|
use Doctrine\Tests\Models\IdentityIsAssociation\CompositeId; |
9
|
|
|
use Doctrine\Tests\Models\IdentityIsAssociation\NestedAssociationToToOneAssociationIdToSimpleId; |
10
|
|
|
use Doctrine\Tests\Models\IdentityIsAssociation\SimpleId; |
11
|
|
|
use Doctrine\Tests\Models\IdentityIsAssociation\ToOneAssociationIdToSimpleId; |
12
|
|
|
use Doctrine\Tests\Models\IdentityIsAssociation\ToOneCompositeAssociationToMultipleSimpleId; |
13
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @covers \Doctrine\ORM\Utility\NormalizeIdentifier |
17
|
|
|
*/ |
18
|
|
|
class NormalizeIdentifierTest extends OrmFunctionalTestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Identifier flattener |
22
|
|
|
* |
23
|
|
|
* @var \Doctrine\ORM\Utility\NormalizeIdentifier |
24
|
|
|
*/ |
25
|
|
|
private $normalizeIdentifier; |
26
|
|
|
|
27
|
|
|
protected function setUp() : void |
28
|
|
|
{ |
29
|
|
|
parent::setUp(); |
30
|
|
|
|
31
|
|
|
$this->normalizeIdentifier = new NormalizeIdentifier(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @dataProvider expectedIdentifiersProvider |
36
|
|
|
*/ |
37
|
|
|
public function testIdentifierNormalization( |
38
|
|
|
string $targetClass, |
39
|
|
|
array $id, |
40
|
|
|
array $expectedIdentifierStructure |
41
|
|
|
) : void { |
42
|
|
|
$this->assertSameIdentifierStructure( |
43
|
|
|
$expectedIdentifierStructure, |
44
|
|
|
($this->normalizeIdentifier)( |
45
|
|
|
$this->em, |
46
|
|
|
$this->em->getClassMetadata($targetClass), |
47
|
|
|
$id |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Recursively analyzes a given identifier. |
54
|
|
|
* If objects are found, then recursively analyizes object structures |
55
|
|
|
*/ |
56
|
|
|
private function assertSameIdentifierStructure(array $expectedId, array $id) : void |
57
|
|
|
{ |
58
|
|
|
self::assertSame(\array_keys($expectedId), \array_keys($id)); |
59
|
|
|
|
60
|
|
|
foreach ($expectedId as $field => $value) { |
61
|
|
|
if (! \is_object($value)) { |
62
|
|
|
self::assertSame($id[$field], $value); |
63
|
|
|
|
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
self::assertInstanceOf(\get_class($value), $id[$field]); |
68
|
|
|
|
69
|
|
|
$nestedIdProperties = []; |
70
|
|
|
$nestedExpectedProperties = []; |
71
|
|
|
|
72
|
|
|
foreach ((new \ReflectionClass($value))->getProperties() as $property) { |
73
|
|
|
$propertyName = $property->getName(); |
74
|
|
|
|
75
|
|
|
$nestedExpectedProperties[$propertyName] = $property->getValue($value); |
76
|
|
|
$nestedIdProperties[$propertyName] = $property->getValue($id[$field]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->assertSameIdentifierStructure($nestedExpectedProperties, $nestedIdProperties); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function expectedIdentifiersProvider() : array |
84
|
|
|
{ |
85
|
|
|
$simpleIdA = new SimpleId(); |
86
|
|
|
$simpleIdB = new SimpleId(); |
87
|
|
|
$toOneAssociationIdToSimpleId = new ToOneAssociationIdToSimpleId(); |
88
|
|
|
|
89
|
|
|
$simpleIdA->id = 123; |
90
|
|
|
$simpleIdB->id = 456; |
91
|
|
|
$toOneAssociationIdToSimpleId->simpleId = $simpleIdA; |
92
|
|
|
|
93
|
|
|
return [ |
94
|
|
|
'simple single-field id fetch' => [ |
95
|
|
|
SimpleId::class, |
96
|
|
|
['id' => 123], |
97
|
|
|
['id' => 123], |
98
|
|
|
], |
99
|
|
|
'simple multi-field id fetch' => [ |
100
|
|
|
CompositeId::class, |
101
|
|
|
['idA' => 123, 'idB' => 123], |
102
|
|
|
['idA' => 123, 'idB' => 123], |
103
|
|
|
], |
104
|
|
|
'simple multi-field id fetch, order reversed' => [ |
105
|
|
|
CompositeId::class, |
106
|
|
|
['idB' => 123, 'idA' => 123], |
107
|
|
|
['idA' => 123, 'idB' => 123], |
108
|
|
|
], |
109
|
|
|
ToOneAssociationIdToSimpleId::class => [ |
110
|
|
|
ToOneAssociationIdToSimpleId::class, |
111
|
|
|
['simpleId' => 123], |
112
|
|
|
['simpleId' => $simpleIdA], |
113
|
|
|
], |
114
|
|
|
ToOneCompositeAssociationToMultipleSimpleId::class => [ |
115
|
|
|
ToOneCompositeAssociationToMultipleSimpleId::class, |
116
|
|
|
['simpleIdA' => 123, 'simpleIdB' => 456], |
117
|
|
|
['simpleIdA' => $simpleIdA, 'simpleIdB' => $simpleIdB], |
118
|
|
|
], |
119
|
|
|
NestedAssociationToToOneAssociationIdToSimpleId::class => [ |
120
|
|
|
NestedAssociationToToOneAssociationIdToSimpleId::class, |
121
|
|
|
['nested' => 123], |
122
|
|
|
['nested' => $toOneAssociationIdToSimpleId], |
123
|
|
|
], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|