Passed
Pull Request — master (#14)
by Pavel
03:16
created

testDiscriminatorMetadataLoading()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Tests;
4
5
use Bankiru\Api\Doctrine\Mapping\EntityMetadata;
6
use Bankiru\Api\Doctrine\Test\Entity\Discriminator\InheritorFirst;
7
use Bankiru\Api\Doctrine\Test\Entity\Discriminator\InheritorSecond;
8
use Bankiru\Api\Doctrine\Test\Entity\DiscriminatorBaseClass;
9
use ScayTrase\Api\Rpc\RpcRequestInterface;
10
11
final class DiscriminatorTest extends AbstractEntityManagerTest
12
{
13
    public function testDiscriminatorMetadataLoading()
14
    {
15
        $factory = $this->getManager()->getMetadataFactory();
16
        /** @var EntityMetadata $baseMetadata */
17
        $baseMetadata = $factory->getMetadataFor(DiscriminatorBaseClass::class);
18
        /** @var EntityMetadata $firstMetadata */
19
        $firstMetadata = $factory->getMetadataFor(InheritorFirst::class);
20
        /** @var EntityMetadata $secondMetadata */
21
        $secondMetadata = $factory->getMetadataFor(InheritorSecond::class);
22
23
        self::assertCount(3, $baseMetadata->discriminatorMap);
24
        self::assertEquals(strtolower('DiscriminatorBaseClass'), $baseMetadata->discriminatorValue);
25
        self::assertInternalType('array', $baseMetadata->discriminatorField);
26
        self::assertInternalType('string', $baseMetadata->discriminatorValue);
27
28
        self::assertCount(3, $firstMetadata->discriminatorMap);
29
        self::assertEquals(strtolower('InheritorFirst'), $firstMetadata->discriminatorValue);
30
        self::assertInternalType('array', $firstMetadata->discriminatorField);
31
        self::assertInternalType('string', $firstMetadata->discriminatorValue);
32
33
        self::assertCount(3, $secondMetadata->discriminatorMap);
34
        self::assertEquals(strtolower('InheritorSecond'), $secondMetadata->discriminatorValue);
35
        self::assertInternalType('array', $secondMetadata->discriminatorField);
36
        self::assertInternalType('string', $secondMetadata->discriminatorValue);
37
    }
38
39
    public function testDiscriminatorMapResolution()
40
    {
41
        $this->getClient()->push(
42
            $this->getResponseMock(
43
                true,
44
                [
45
                    (object)[
46
                        'type'     => strtolower('DiscriminatorBaseClass'),
47
                        'id_field' => 1,
48
                        'base'     => 1,
49
                        'first'    => null,
50
                        'second'   => null,
51
                    ],
52
                    (object)[
53
                        'type'     => strtolower('InheritorFirst'),
54
                        'id_field' => 2,
55
                        'base'     => 2,
56
                        'first'    => 2,
57
                        'second'   => null,
58
                    ],
59
                    (object)[
60
                        'type'     => strtolower('InheritorSecond'),
61
                        'id_field' => 3,
62
                        'base'     => 3,
63
                        'first'    => 3,
64
                        'second'   => 3,
65
                    ],
66
                ]
67
            ),
68 View Code Duplication
            function (RpcRequestInterface $request) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
                self::assertEquals('discriminator/search', $request->getMethod());
70
                self::assertEquals(
71
                    [
72
                        'criteria' => [
73
                            'type' => [
74
                                strtolower('DiscriminatorBaseClass'),
75
                                strtolower('InheritorFirst'),
76
                                strtolower('InheritorSecond'),
77
                            ],
78
                        ],
79
                        'order'    => [],
80
                        'limit'    => null,
81
                        'offset'   => null,
82
                    ],
83
                    $request->getParameters()
84
                );
85
86
                return true;
87
            }
88
        );
89
90
        $entities = $this->getManager()->getRepository(DiscriminatorBaseClass::class)->findAll();
91
92
        self::assertCount(3, $entities);
93
        /** @var DiscriminatorBaseClass $base */
94
        $base = array_shift($entities);
95
        self::assertInstanceOf(DiscriminatorBaseClass::class, $base);
96
        self::assertEquals(1, $base->getId());
97
        self::assertEquals(1, $base->getBase());
98
99
        /** @var InheritorFirst $first */
100
        $first = array_shift($entities);
101
        self::assertInstanceOf(InheritorFirst::class, $first);
102
        self::assertEquals(2, $first->getId());
103
        self::assertEquals(2, $first->getBase());
104
        self::assertEquals(2, $first->getFirst());
105
106
        /** @var InheritorSecond $second */
107
        $second = array_shift($entities);
108
        self::assertInstanceOf(InheritorSecond::class, $second);
109
        self::assertEquals(3, $second->getId());
110
        self::assertEquals(3, $second->getBase());
111
        self::assertEquals(3, $second->getFirst());
112
        self::assertEquals(3, $second->getSecond());
113
    }
114
115
    public function testSearchingDiscriminatedEntitiesAddsTypeFilter()
116
    {
117
        $this->getClient()->push(
118
            $this->getResponseMock(
119
                true,
120
                [
121
                    (object)[
122
                        'type'     => strtolower('InheritorFirst'),
123
                        'id_field' => 2,
124
                        'base'     => 2,
125
                        'first'    => 2,
126
                        'second'   => null,
127
                    ],
128
                    (object)[
129
                        'type'     => strtolower('InheritorSecond'),
130
                        'id_field' => 3,
131
                        'base'     => 3,
132
                        'first'    => 3,
133
                        'second'   => 3,
134
                    ],
135
                ]
136
            ),
137 View Code Duplication
            function (RpcRequestInterface $request) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
                self::assertEquals('discriminator/search', $request->getMethod());
139
                self::assertEquals(
140
                    [
141
                        'criteria' => [
142
                            'type' => [
143
                                strtolower('InheritorFirst'),
144
                                strtolower('InheritorSecond'),
145
                            ],
146
                        ],
147
                        'order'    => [],
148
                        'limit'    => null,
149
                        'offset'   => null,
150
                    ],
151
                    $request->getParameters()
152
                );
153
154
                return true;
155
            }
156
        );
157
158
        $entities = $this->getManager()->getRepository(InheritorFirst::class)->findAll();
159
160
        /** @var InheritorFirst $first */
161
        $first = array_shift($entities);
162
        self::assertInstanceOf(InheritorFirst::class, $first);
163
        self::assertEquals(2, $first->getId());
164
        self::assertEquals(2, $first->getBase());
165
        self::assertEquals(2, $first->getFirst());
166
167
        /** @var InheritorSecond $second */
168
        $second = array_shift($entities);
169
        self::assertInstanceOf(InheritorSecond::class, $second);
170
        self::assertEquals(3, $second->getId());
171
        self::assertEquals(3, $second->getBase());
172
        self::assertEquals(3, $second->getFirst());
173
        self::assertEquals(3, $second->getSecond());
174
    }
175
176
    public function testDiscriminatorEntitiesCommit()
177
    {
178
        $this->getClient()->push(
179
            $this->getResponseMock(true, ['id_field' => 241]),
180
            function (RpcRequestInterface $request) {
181
                self::assertEquals('discriminator/create', $request->getMethod());
182
                self::assertEquals(
183
                    [
184
                        'type'  => strtolower('InheritorFirst'),
185
                        'base'  => null,
186
                        'first' => null,
187
                    ],
188
                    $request->getParameters()
189
                );
190
191
                return true;
192
            }
193
        );
194
195
        $entity = new InheritorFirst();
196
197
        $this->getManager()->persist($entity);
198
        $this->getManager()->flush();
199
200
        $this->getClient()->push(
201
            $this->getResponseMock(true, ['id_field' => 241]),
202
            function (RpcRequestInterface $request) {
203
                self::assertEquals('discriminator/patch', $request->getMethod());
204
                self::assertEquals(
205
                    [
206
                        'identifier' => ['id_field' => 241],
207
                        'patch'       => ['first' => 'test'],
208
                    ],
209
                    $request->getParameters()
210
                );
211
212
                return true;
213
            }
214
        );
215
216
        $entity->setFirst('test');
217
        $this->getManager()->flush();
218
    }
219
}
220