Failed Conditions
Pull Request — develop (#6719)
by Marco
65:26
created

assertSameIdentifierStructure()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Utility;
6
7
use Doctrine\ORM\Annotation as ORM;
8
use Doctrine\ORM\Utility\NormalizeIdentifier;
9
use Doctrine\Tests\OrmFunctionalTestCase;
10
11
/**
12
 * @covers \Doctrine\ORM\Utility\NormalizeIdentifier
13
 */
14
class NormalizeIdentifierTest extends OrmFunctionalTestCase
15
{
16
    /**
17
     * Identifier flattener
18
     *
19
     * @var \Doctrine\ORM\Utility\NormalizeIdentifier
20
     */
21
    private $normalizeIdentifier;
22
23
    protected function setUp() : void
24
    {
25
        parent::setUp();
26
27
        $this->normalizeIdentifier = new NormalizeIdentifier();
28
    }
29
30
    /**
31
     * @dataProvider expectedIdentifiersProvider
32
     */
33
    public function testIdentifierNormalization(
34
        string $targetClass,
35
        array $id,
36
        array $expectedIdentifierStructure
37
    ) : void {
38
        $this->assertSameIdentifierStructure(
39
            $expectedIdentifierStructure,
40
            ($this->normalizeIdentifier)(
41
                $this->em,
42
                $this->em->getClassMetadata($targetClass),
43
                $id
44
            )
45
        );
46
    }
47
48
    /**
49
     * Recursively analyzes a given identifier.
50
     * If objects are found, then recursively analyizes object structures
51
     */
52
    private function assertSameIdentifierStructure(array $expectedId, array $id) : void
53
    {
54
        self::assertSame(\array_keys($expectedId), \array_keys($id));
55
56
        foreach ($expectedId as $field => $value) {
57
            if (! \is_object($value)) {
58
                self::assertSame($id[$field], $value);
59
60
                continue;
61
            }
62
63
            self::assertInstanceOf(\get_class($value), $id[$field]);
64
65
            $nestedIdProperties       = [];
66
            $nestedExpectedProperties = [];
67
68
            foreach ((new \ReflectionClass($value))->getProperties() as $property) {
69
                $propertyName = $property->getName();
70
71
                $nestedExpectedProperties[$propertyName] = $property->getValue($value);
72
                $nestedIdProperties[$propertyName]       = $property->getValue($id[$field]);
73
            }
74
75
            $this->assertSameIdentifierStructure($nestedExpectedProperties, $nestedIdProperties);
76
        }
77
    }
78
79
    public function expectedIdentifiersProvider() : array
80
    {
81
        $simpleIdA                    = new SimpleId();
82
        $simpleIdB                    = new SimpleId();
83
        $toOneAssociationIdToSimpleId = new ToOneAssociationIdToSimpleId();
84
85
        $simpleIdA->id                          = 123;
86
        $simpleIdB->id                          = 456;
87
        $toOneAssociationIdToSimpleId->simpleId = $simpleIdA;
88
89
        return [
90
            'simple single-field id fetch' => [
91
                SimpleId::class,
92
                ['id' => 123],
93
                ['id' => 123],
94
            ],
95
            'simple multi-field id fetch' => [
96
                CompositeId::class,
97
                ['idA' => 123, 'idB' => 123],
98
                ['idA' => 123, 'idB' => 123],
99
            ],
100
            'simple multi-field id fetch, order reversed' => [
101
                CompositeId::class,
102
                ['idB' => 123, 'idA' => 123],
103
                ['idA' => 123, 'idB' => 123],
104
            ],
105
            ToOneAssociationIdToSimpleId::class => [
106
                ToOneAssociationIdToSimpleId::class,
107
                ['simpleId' => 123],
108
                ['simpleId' => $simpleIdA],
109
            ],
110
            ToOneCompositeAssociationToMultipleSimpleId::class => [
111
                ToOneCompositeAssociationToMultipleSimpleId::class,
112
                ['simpleIdA' => 123, 'simpleIdB' => 456],
113
                ['simpleIdA' => $simpleIdA, 'simpleIdB' => $simpleIdB],
114
            ],
115
            NestedAssociationToToOneAssociationIdToSimpleId::class => [
116
                NestedAssociationToToOneAssociationIdToSimpleId::class,
117
                ['nested' => 123],
118
                ['nested' => $toOneAssociationIdToSimpleId],
119
            ],
120
        ];
121
    }
122
}
123
124
// @TODO move following entities to their own namespace
125
/** @ORM\Entity */
126
class SimpleId
127
{
128
    /** @ORM\Id @ORM\Column(name="id", type="integer") */
129
    public $id;
130
}
131
132
/** @ORM\Entity */
133
class CompositeId
134
{
135
    /** @ORM\Id @ORM\Column(name="id_a", type="integer") */
136
    public $idA;
137
138
    /** @ORM\Id @ORM\Column(name="id_b", type="integer") */
139
    public $idB;
140
}
141
142
/** @ORM\Entity */
143
class ToOneAssociationIdToSimpleId
144
{
145
    /**
146
     * @ORM\Id
147
     * @ORM\ManyToOne(targetEntity=SimpleId::class)
148
     * @ORM\JoinColumn(name="simple_id", referencedColumnName="id")
149
     */
150
    public $simpleId;
151
}
152
153
/** @ORM\Entity */
154
class ToOneCompositeAssociationToMultipleSimpleId
155
{
156
    /**
157
     * @ORM\Id
158
     * @ORM\ManyToOne(targetEntity=SimpleId::class)
159
     * @ORM\JoinColumn(name="simple_id_a", referencedColumnName="id")
160
     */
161
    public $simpleIdA;
162
163
    /**
164
     * @ORM\Id
165
     * @ORM\ManyToOne(targetEntity=SimpleId::class)
166
     * @ORM\JoinColumn(name="simple_id_b", referencedColumnName="id")
167
     */
168
    public $simpleIdB;
169
}
170
171
/** @ORM\Entity */
172
class NestedAssociationToToOneAssociationIdToSimpleId
173
{
174
    /**
175
     * @ORM\Id
176
     * @ORM\ManyToOne(targetEntity=ToOneAssociationIdToSimpleId::class)
177
     * @ORM\JoinColumn(name="nested_id", referencedColumnName="simple_id")
178
     */
179
    public $nested;
180
}
181