Passed
Pull Request — master (#14)
by Pavel
05:12
created

ReferenceLoadingTest::getClientNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Tests;
4
5
use Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity;
6
use Bankiru\Api\Doctrine\Test\Entity\TestEntity;
7
use Bankiru\Api\Doctrine\Test\Entity\TestReference;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\Common\Proxy\Proxy;
10
use ScayTrase\Api\Rpc\RpcRequestInterface;
11
12
final class ReferenceLoadingTest extends AbstractEntityManagerTest
13
{
14
    public function testAnyToOneLoading()
15
    {
16
17
        $repository = $this->getManager()->getRepository(TestEntity::class);
18
19
        $this->getClient('test-client')->push(
20
            $this->getResponseMock(
21
                true,
22
                (object)[
23
                    'id'         => '1',
24
                    'payload'    => 'test-payload',
25
                    'references' => [],
26
                    'parent'     => '2',
27
                ]
28
            )
29
        );
30
31
        $this->getClient('test-client')->push(
32
            $this->getResponseMock(
33
                true,
34
                (object)[
35
                    'id'         => '2',
36
                    'payload'    => 'parent-payload',
37
                    'references' => [],
38
                    'parent'     => 1,
39
                ]
40
            )
41
        );
42
43
        /** @var TestEntity|Proxy $entity */
44
        $entity = $repository->find(1);
45
        $parent = $entity->getParent();
0 ignored issues
show
Bug introduced by
The method getParent does only exist in Bankiru\Api\Doctrine\Test\Entity\TestEntity, but not in Doctrine\Common\Proxy\Proxy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
46
47
        self::assertInstanceOf(TestEntity::class, $parent);
48
        self::assertEquals('parent-payload', $parent->getPayload());
49
        self::assertEquals(2, $parent->getId());
50
        self::assertInternalType('int', $parent->getId());
51
52
        self::assertEquals($entity, $parent->getParent());
53
    }
54
55
    public function testOneToManyLoading()
56
    {
57
        $repository = $this->getManager()->getRepository(TestEntity::class);
58
59
        $this->getClient('test-client')->push(
60
            $this->getResponseMock(
61
                true,
62
                (object)[
63
                    'id'      => '1',
64
                    'payload' => 'test-payload',
65
                ]
66
            )
67
        );
68
69
        $this->getClient('test-reference-client')->push(
70
            $this->getResponseMock(
71
                true,
72
                [
73
                    (object)[
74
                        'id'                => '5',
75
                        'reference-payload' => 'test-payload-5',
76
                        'owner'             => '1',
77
                    ],
78
                    (object)[
79
                        'id'                => '7',
80
                        'reference-payload' => 'test-payload-7',
81
                        'owner'             => '1',
82
                    ],
83
                ]
84
            )
85
        );
86
87
        /** @var TestEntity $entity */
88
        $entity = $repository->find(1);
89
90
        self::assertInstanceOf(TestEntity::class, $entity);
91
        self::assertEquals('test-payload', $entity->getPayload());
92
        self::assertEquals(1, $entity->getId());
93
        self::assertInstanceOf(Collection::class, $entity->getReferences());
94
        self::assertEquals(1, $this->getClient('test-reference-client')->count());
95
        self::assertCount(2, $entity->getReferences());
96
        self::assertEquals(0, $this->getClient('test-reference-client')->count());
97
        foreach ($entity->getReferences() as $reference) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
98
        }
99
        self::assertInstanceOf(Collection::class, $entity->getReferences());
100
101 View Code Duplication
        foreach ($entity->getReferences() as $reference) {
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...
102
            self::assertInternalType('int', $reference->getId());
103
            self::assertInternalType('int', $reference->getOwner()->getId());
104
            self::assertInstanceOf(TestReference::class, $reference);
105
            self::assertEquals('test-payload-' . $reference->getId(), $reference->getReferencePayload());
106
            self::assertEquals($entity, $reference->getOwner());
107
        }
108
    }
109
110
    public function testFindByReference()
111
    {
112
        $repository = $this->getManager()->getRepository(TestEntity::class);
113
114
        $this->getClient('test-client')->push(
115
            $this->getResponseMock(
116
                true,
117
                (object)[
118
                    'id'         => 1,
119
                    'payload'    => 'test-payload',
120
                    'references' => [],
121
                ]
122
            )
123
        );
124
125
        /** @var TestEntity $parent */
126
        $parent = $repository->find(1);
127
        self::assertInstanceOf(TestEntity::class, $parent);
128
        self::assertEquals('test-payload', $parent->getPayload());
129
        self::assertEquals(1, $parent->getId());
130
131
        $this->getClient('test-client')->push(
132
            $this->getResponseMock(
133
                true,
134
                [
135
                    (object)[
136
                        'id'         => 2,
137
                        'payload'    => 'test-child',
138
                        'references' => [],
139
                        'parent'     => 1,
140
                    ],
141
                ]
142
            )
143
        );
144
145
        $children = $repository->findBy(['parent' => $parent]);
146
147
        self::assertCount(1, $children);
148
        /** @var TestEntity $child */
149
150
        $child = array_shift($children);
151
152
        self::assertEquals($parent, $child->getParent());
153
        self::assertEquals($parent->getPayload(), $child->getParent()->getPayload());
0 ignored issues
show
Bug introduced by
The method getPayload() does not seem to exist on object<Bankiru\Api\Doctr...t\Entity\TestReference>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
    }
155
156
    public function testFindByNullValue()
157
    {
158
        $repository = $this->getManager()->getRepository(TestEntity::class);
159
        /** @var TestEntity $parent */
160
        $parent = $this->getManager()->getReference(TestEntity::class, 1);
161
162
        $this->getClient('test-client')->push(
163
            $this->getResponseMock(
164
                true,
165
                [
166
                    (object)[
167
                        'id'         => 2,
168
                        'payload'    => 'test-child',
169
                        'references' => [],
170
                        'parent'     => 1,
171
                    ],
172
                ]
173
            )
174
        );
175
176
        $children = $repository->findBy(['parent' => $parent]);
0 ignored issues
show
Unused Code introduced by
$children is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
177
178
        $this->getClient('test-client')->push(
179
            $this->getResponseMock(
180
                true,
181
                [
182
                    (object)[
183
                        'id'         => 3,
184
                        'payload'    => 'test-child-3',
185
                        'references' => [],
186
                        'parent'     => 1,
187
                    ],
188
                ]
189
            ),
190
            function (RpcRequestInterface $request) {
191
                self::assertNull($request->getParameters()['criteria']['parent']);
192
193
                return true;
194
            }
195
        );
196
197
        $children = $repository->findBy(['parent' => null]);
0 ignored issues
show
Unused Code introduced by
$children is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
198
199
        $this->getClient('test-client')->push(
200
            $this->getResponseMock(
201
                true,
202
                [
203
                    (object)[
204
                        'id'         => 4,
205
                        'payload'    => 'test-child-4',
206
                        'references' => [],
207
                        'parent'     => 1,
208
                    ],
209
                ]
210
            ),
211
            function (RpcRequestInterface $request) {
212
                self::assertNull($request->getParameters()['criteria']['payload']);
213
214
                return true;
215
            }
216
        );
217
218
        $children = $repository->findBy(['payload' => null]);
0 ignored issues
show
Unused Code introduced by
$children is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
219
    }
220
221
    public function testSubEntityRelations()
222
    {
223
        $repository = $this->getManager()->getRepository(SubEntity::class);
224
225
        $this->getClient('test-client')->push(
226
            $this->getResponseMock(
227
                true,
228
                (object)[
229
                    'id'          => '1',
230
                    'payload'     => 'test-payload',
231
                    'references'  => ['5', '7'],
232
                    'sub-payload' => 'sub-payload',
233
                ]
234
            ),
235
            function (RpcRequestInterface $request) {
236
                self::assertEquals('test-entity/find', $request->getMethod());
237
                self::assertEquals(['id' => 1], $request->getParameters());
238
239
                return true;
240
            }
241
        );
242
243
        $this->getClient('test-reference-client')->push(
244
            $this->getResponseMock(
245
                true,
246
                [
247
                    (object)[
248
                        'id'                => '5',
249
                        'reference-payload' => 'test-payload-5',
250
                        'owner'             => '1',
251
                    ],
252
                    (object)[
253
                        'id'                => '7',
254
                        'reference-payload' => 'test-payload-7',
255
                        'owner'             => '1',
256
                    ],
257
                ]
258
            ),
259 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...
260
                self::assertEquals('test-reference/search', $request->getMethod());
261
                self::assertEquals(
262
                    [
263
                        'criteria' => ['owner' => 1],
264
                        'order'    => [],
265
                        'limit'    => null,
266
                        'offset'   => null,
267
                    ],
268
                    $request->getParameters()
269
                );
270
271
                return true;
272
            }
273
        );
274
275
        /** @var SubEntity $entity */
276
        $entity = $repository->find(1);
277
        self::assertSame(1, $entity->getId());
278
        self::assertSame('test-payload', $entity->getPayload());
279
        self::assertSame('sub-payload', $entity->getSubPayload());
280
        self::assertNull($entity->getStringPayload());
281
        /** @var TestReference[] $references */
282
        $references = $entity->getReferences()->toArray();
283
284
        self::assertCount(2, $references);
285
        foreach ($references as $reference) {
286
            self::assertSame('test-payload-' . $reference->getId(), $reference->getReferencePayload());
287
        }
288
    }
289
290
    protected function getClientNames()
291
    {
292
        return array_merge(parent::getClientNames(), ['test-reference-client']);
293
    }
294
}
295