Completed
Pull Request — master (#14)
by Pavel
04:00
created

ReferenceLoadingTest::testFindByReference()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 26
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
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 testSubEntityRelations()
157
    {
158
        $repository = $this->getManager()->getRepository(SubEntity::class);
159
160
        $this->getClient('test-client')->push(
161
            $this->getResponseMock(
162
                true,
163
                (object)[
164
                    'id'          => '1',
165
                    'payload'     => 'test-payload',
166
                    'references'  => ['5', '7'],
167
                    'sub-payload' => 'sub-payload',
168
                ]
169
            ),
170
            function (RpcRequestInterface $request) {
171
                self::assertEquals('test-entity/find', $request->getMethod());
172
                self::assertEquals(['id' => 1], $request->getParameters());
173
174
                return true;
175
            }
176
        );
177
178
        $this->getClient('test-reference-client')->push(
179
            $this->getResponseMock(
180
                true,
181
                [
182
                    (object)[
183
                        'id'                => '5',
184
                        'reference-payload' => 'test-payload-5',
185
                        'owner'             => '1',
186
                    ],
187
                    (object)[
188
                        'id'                => '7',
189
                        'reference-payload' => 'test-payload-7',
190
                        'owner'             => '1',
191
                    ],
192
                ]
193
            ),
194 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...
195
                self::assertEquals('test-reference/search', $request->getMethod());
196
                self::assertEquals(
197
                    [
198
                        'criteria' => ['owner' => 1],
199
                        'order'    => [],
200
                        'limit'    => null,
201
                        'offset'   => null,
202
                    ],
203
                    $request->getParameters()
204
                );
205
206
                return true;
207
            }
208
        );
209
210
        /** @var SubEntity $entity */
211
        $entity = $repository->find(1);
212
        self::assertSame(1, $entity->getId());
213
        self::assertSame('test-payload', $entity->getPayload());
214
        self::assertSame('sub-payload', $entity->getSubPayload());
215
        self::assertNull($entity->getStringPayload());
216
        /** @var TestReference[] $references */
217
        $references = $entity->getReferences()->toArray();
218
219
        self::assertCount(2, $references);
220
        foreach ($references as $reference) {
221
            self::assertSame('test-payload-' . $reference->getId(), $reference->getReferencePayload());
222
        }
223
    }
224
225
    protected function getClientNames()
226
    {
227
        return array_merge(parent::getClientNames(), ['test-reference-client']);
228
    }
229
}
230