Completed
Pull Request — master (#14)
by Pavel
03:24
created

ReferenceLoadingTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 198
Duplicated Lines 3.54 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 7
loc 198
c 0
b 0
f 0
wmc 8
lcom 1
cbo 7
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B testAnyToOneLoading() 0 40 1
A testOneToManyLoading() 7 54 3
B testFindByReference() 0 45 1
A testSubEntityRelations() 0 48 2
A getClientNames() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
11
class ReferenceLoadingTest extends AbstractEntityManagerTest
12
{
13
    public function testAnyToOneLoading()
14
    {
15
16
        $repository = $this->getManager()->getRepository(TestEntity::class);
17
18
        $this->getClient('test-client')->push(
19
            $this->getResponseMock(
20
                true,
21
                (object)[
22
                    'id'         => '1',
23
                    'payload'    => 'test-payload',
24
                    'references' => [],
25
                    'parent'     => '2',
26
                ]
27
            )
28
        );
29
30
        $this->getClient('test-client')->push(
31
            $this->getResponseMock(
32
                true,
33
                (object)[
34
                    'id'         => '2',
35
                    'payload'    => 'parent-payload',
36
                    'references' => [],
37
                    'parent'     => 1,
38
                ]
39
            )
40
        );
41
42
        /** @var TestEntity|Proxy $entity */
43
        $entity = $repository->find(1);
44
        $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...
45
46
        self::assertInstanceOf(TestEntity::class, $parent);
47
        self::assertEquals('parent-payload', $parent->getPayload());
48
        self::assertEquals(2, $parent->getId());
49
        self::assertInternalType('int', $parent->getId());
50
51
        self::assertEquals($entity, $parent->getParent());
52
    }
53
54
    public function testOneToManyLoading()
55
    {
56
        $repository = $this->getManager()->getRepository(TestEntity::class);
57
58
        $this->getClient('test-client')->push(
59
            $this->getResponseMock(
60
                true,
61
                (object)[
62
                    'id'      => '1',
63
                    'payload' => 'test-payload',
64
                ]
65
            )
66
        );
67
68
        $this->getClient('test-reference-client')->push(
69
            $this->getResponseMock(
70
                true,
71
                [
72
                    (object)[
73
                        'id'                => '5',
74
                        'reference-payload' => 'test-payload-5',
75
                        'owner'             => '1',
76
                    ],
77
                    (object)[
78
                        'id'                => '7',
79
                        'reference-payload' => 'test-payload-7',
80
                        'owner'             => '1',
81
                    ],
82
                ]
83
            )
84
        );
85
86
        /** @var TestEntity $entity */
87
        $entity = $repository->find(1);
88
89
        self::assertInstanceOf(TestEntity::class, $entity);
90
        self::assertEquals('test-payload', $entity->getPayload());
91
        self::assertEquals(1, $entity->getId());
92
        self::assertInstanceOf(Collection::class, $entity->getReferences());
93
        self::assertEquals(1, $this->getClient('test-reference-client')->count());
94
        self::assertCount(2, $entity->getReferences());
95
        self::assertEquals(0, $this->getClient('test-reference-client')->count());
96
        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...
97
        }
98
        self::assertInstanceOf(Collection::class, $entity->getReferences());
99
100 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...
101
            self::assertInternalType('int', $reference->getId());
102
            self::assertInternalType('int', $reference->getOwner()->getId());
103
            self::assertInstanceOf(TestReference::class, $reference);
104
            self::assertEquals('test-payload-'.$reference->getId(), $reference->getReferencePayload());
105
            self::assertEquals($entity, $reference->getOwner());
106
        }
107
    }
108
109
    public function testFindByReference()
110
    {
111
        $repository = $this->getManager()->getRepository(TestEntity::class);
112
113
        $this->getClient('test-client')->push(
114
            $this->getResponseMock(
115
                true,
116
                (object)[
117
                    'id'         => 1,
118
                    'payload'    => 'test-payload',
119
                    'references' => [],
120
                ]
121
            )
122
        );
123
124
        /** @var TestEntity $parent */
125
        $parent = $repository->find(1);
126
        self::assertInstanceOf(TestEntity::class, $parent);
127
        self::assertEquals('test-payload', $parent->getPayload());
128
        self::assertEquals(1, $parent->getId());
129
130
        $this->getClient('test-client')->push(
131
            $this->getResponseMock(
132
                true,
133
                [
134
                    (object)[
135
                        'id'         => 2,
136
                        'payload'    => 'test-child',
137
                        'references' => [],
138
                        'parent'     => 1,
139
                    ],
140
                ]
141
            )
142
        );
143
144
        $children = $repository->findBy(['parent' => $parent]);
145
146
        self::assertCount(1, $children);
147
        /** @var TestEntity $child */
148
149
        $child = array_shift($children);
150
151
        self::assertEquals($parent, $child->getParent());
152
        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...
153
    }
154
155
    public function testSubEntityRelations()
156
    {
157
        $repository = $this->getManager()->getRepository(SubEntity::class);
158
159
        $this->getClient('test-client')->push(
160
            $this->getResponseMock(
161
                true,
162
                (object)[
163
                    'id'          => '1',
164
                    'payload'     => 'test-payload',
165
                    'references'  => ['5', '7'],
166
                    'sub-payload' => 'sub-payload',
167
                ]
168
            )
169
        );
170
171
        $this->getClient('test-reference-client')->push(
172
            $this->getResponseMock(
173
                true,
174
                [
175
                    (object)[
176
                        'id'                => '5',
177
                        'reference-payload' => 'test-payload-5',
178
                        'owner'             => '1',
179
                    ],
180
                    (object)[
181
                        'id'                => '7',
182
                        'reference-payload' => 'test-payload-7',
183
                        'owner'             => '1',
184
                    ],
185
                ]
186
            )
187
        );
188
189
        /** @var SubEntity $entity */
190
        $entity = $repository->find(1);
191
        self::assertSame(1, $entity->getId());
192
        self::assertSame('test-payload', $entity->getPayload());
193
        self::assertSame('sub-payload', $entity->getSubPayload());
194
        self::assertNull($entity->getStringPayload());
195
        /** @var TestReference[] $references */
196
        $references = $entity->getReferences()->toArray();
197
198
        self::assertCount(2, $references);
199
        foreach ($references as $reference) {
200
            self::assertSame('test-payload-'.$reference->getId(), $reference->getReferencePayload());
201
        }
202
    }
203
204
    protected function getClientNames()
205
    {
206
        return array_merge(parent::getClientNames(), ['test-reference-client']);
207
    }
208
}
209