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 |
||
12 | class ReferenceLoadingTest extends AbstractEntityManagerTest |
||
13 | { |
||
14 | 1 | public function testAnyToOneLoading() |
|
15 | { |
||
16 | |||
17 | 1 | $repository = $this->getManager()->getRepository(TestEntity::class); |
|
18 | |||
19 | 1 | $this->getResponseMock('test-client')->append( |
|
20 | 1 | new Response( |
|
21 | 1 | 200, |
|
22 | 1 | [], |
|
23 | 1 | json_encode( |
|
24 | [ |
||
25 | 1 | 'jsonrpc' => '2.0', |
|
26 | 1 | 'id' => 'test', |
|
27 | 'result' => [ |
||
28 | 1 | 'id' => '1', |
|
29 | 1 | 'payload' => 'test-payload', |
|
30 | 1 | 'references' => [], |
|
31 | 1 | 'parent' => '2', |
|
32 | 1 | ], |
|
33 | ] |
||
34 | 1 | ) |
|
35 | 1 | ) |
|
36 | 1 | ); |
|
37 | |||
38 | 1 | $this->getResponseMock('test-client')->append( |
|
39 | 1 | new Response( |
|
40 | 1 | 200, |
|
41 | 1 | [], |
|
42 | 1 | json_encode( |
|
43 | [ |
||
44 | 1 | 'jsonrpc' => '2.0', |
|
45 | 1 | 'id' => 'test', |
|
46 | 'result' => [ |
||
47 | 1 | 'id' => '2', |
|
48 | 1 | 'payload' => 'parent-payload', |
|
49 | 1 | 'references' => [], |
|
50 | 1 | 'parent' => 1, |
|
51 | 1 | ], |
|
52 | ] |
||
53 | 1 | ) |
|
54 | 1 | ) |
|
55 | 1 | ); |
|
56 | |||
57 | |||
58 | /** @var TestEntity|Proxy $entity */ |
||
59 | 1 | $entity = $repository->find(1); |
|
60 | 1 | $parent = $entity->getParent(); |
|
61 | |||
62 | 1 | self::assertInstanceOf(TestEntity::class, $parent); |
|
63 | 1 | self::assertEquals('parent-payload', $parent->getPayload()); |
|
64 | 1 | self::assertEquals(2, $parent->getId()); |
|
65 | 1 | self::assertInternalType('int', $parent->getId()); |
|
66 | |||
67 | 1 | self::assertEquals($entity, $parent->getParent()); |
|
68 | 1 | } |
|
69 | |||
70 | 1 | public function testOneToManyLoading() |
|
71 | { |
||
72 | 1 | $repository = $this->getManager()->getRepository(TestEntity::class); |
|
73 | |||
74 | 1 | $this->getResponseMock('test-client')->append( |
|
75 | 1 | new Response( |
|
76 | 1 | 200, |
|
77 | 1 | [], |
|
78 | 1 | json_encode( |
|
79 | [ |
||
80 | 1 | 'jsonrpc' => '2.0', |
|
81 | 1 | 'id' => 'test', |
|
82 | 'result' => [ |
||
83 | 1 | 'id' => '1', |
|
84 | 1 | 'payload' => 'test-payload', |
|
85 | 1 | 'references' => ['5', '7'], |
|
86 | 1 | ], |
|
87 | ] |
||
88 | 1 | ) |
|
89 | 1 | ) |
|
90 | 1 | ); |
|
91 | |||
92 | 1 | $this->getResponseMock('test-reference-client')->append( |
|
93 | 1 | new Response( |
|
94 | 1 | 200, |
|
95 | 1 | [], |
|
96 | 1 | json_encode( |
|
97 | [ |
||
98 | 1 | 'jsonrpc' => '2.0', |
|
99 | 1 | 'id' => 'test', |
|
100 | 'result' => [ |
||
101 | [ |
||
102 | 1 | 'id' => '5', |
|
103 | 1 | 'reference-payload' => 'test-payload-5', |
|
104 | 1 | 'owner' => '1', |
|
105 | 1 | ], |
|
106 | [ |
||
107 | 1 | 'id' => '7', |
|
108 | 1 | 'reference-payload' => 'test-payload-7', |
|
109 | 1 | 'owner' => '1', |
|
110 | 1 | ], |
|
111 | 1 | ], |
|
112 | ] |
||
113 | 1 | ) |
|
114 | 1 | ) |
|
115 | 1 | ); |
|
116 | |||
117 | |||
118 | /** @var TestEntity $entity */ |
||
119 | 1 | $entity = $repository->find(1); |
|
120 | |||
121 | 1 | self::assertInstanceOf(TestEntity::class, $entity); |
|
122 | 1 | self::assertEquals('test-payload', $entity->getPayload()); |
|
123 | 1 | self::assertEquals(1, $entity->getId()); |
|
124 | 1 | self::assertInstanceOf(\Countable::class, $entity->getReferences()); |
|
125 | 1 | self::assertEquals(1, $this->getResponseMock('test-reference-client')->count()); |
|
126 | 1 | self::assertCount(2, $entity->getReferences()); |
|
127 | 1 | self::assertEquals(0, $this->getResponseMock('test-reference-client')->count()); |
|
128 | 1 | self::assertInstanceOf(Collection::class, $entity->getReferences()); |
|
129 | |||
130 | |||
131 | 1 | foreach ($entity->getReferences() as $reference) { |
|
132 | 1 | self::assertInternalType('int', $reference->getId()); |
|
133 | 1 | self::assertInternalType('int', $reference->getOwner()->getId()); |
|
134 | 1 | self::assertInstanceOf(TestReference::class, $reference); |
|
135 | 1 | self::assertEquals('test-payload-' . $reference->getId(), $reference->getReferencePayload()); |
|
136 | 1 | self::assertEquals($entity, $reference->getOwner()); |
|
137 | 1 | } |
|
138 | 1 | } |
|
139 | |||
140 | 1 | public function testFindByReference() |
|
141 | { |
||
142 | 1 | $repository = $this->getManager()->getRepository(TestEntity::class); |
|
143 | |||
144 | 1 | $this->getResponseMock('test-client')->append( |
|
145 | 1 | new Response( |
|
146 | 1 | 200, |
|
147 | 1 | [], |
|
148 | 1 | json_encode( |
|
149 | [ |
||
150 | 1 | 'jsonrpc' => '2.0', |
|
151 | 1 | 'id' => 'test', |
|
152 | 'result' => [ |
||
153 | 1 | 'id' => 1, |
|
154 | 1 | 'payload' => 'test-payload', |
|
155 | 1 | 'references' => [], |
|
156 | 1 | ], |
|
157 | ] |
||
158 | 1 | ) |
|
159 | 1 | ) |
|
160 | 1 | ); |
|
161 | |||
162 | /** @var TestEntity $parent */ |
||
163 | 1 | $parent = $repository->find(1); |
|
164 | 1 | self::assertInstanceOf(TestEntity::class, $parent); |
|
165 | 1 | self::assertEquals('test-payload', $parent->getPayload()); |
|
166 | 1 | self::assertEquals(1, $parent->getId()); |
|
167 | |||
168 | 1 | $this->getResponseMock('test-client')->append( |
|
169 | 1 | new Response( |
|
170 | 1 | 200, |
|
171 | 1 | [], |
|
172 | 1 | json_encode( |
|
173 | [ |
||
174 | 1 | 'jsonrpc' => '2.0', |
|
175 | 1 | 'id' => 'test', |
|
176 | 'result' => [ |
||
177 | [ |
||
178 | 1 | 'id' => 2, |
|
179 | 1 | 'payload' => 'test-child', |
|
180 | 1 | 'references' => [], |
|
181 | 1 | 'parent' => 1, |
|
182 | 1 | ], |
|
183 | 1 | ], |
|
184 | ] |
||
185 | 1 | ) |
|
186 | 1 | ) |
|
187 | 1 | ); |
|
188 | |||
189 | 1 | $children = $repository->findBy(['parent' => $parent]); |
|
190 | |||
191 | 1 | self::assertCount(1, $children); |
|
192 | /** @var TestEntity $child */ |
||
193 | |||
194 | 1 | $childrenArray = $children->toArray(); |
|
195 | 1 | $child = array_shift($childrenArray); |
|
196 | |||
197 | 1 | self::assertEquals($parent, $child->getParent()); |
|
198 | 1 | self::assertEquals($parent->getPayload(), $child->getParent()->getPayload()); |
|
199 | 1 | } |
|
200 | |||
201 | 1 | public function testSubEntityRelations() |
|
202 | { |
||
203 | 1 | $repository = $this->getManager()->getRepository(SubEntity::class); |
|
204 | |||
205 | 1 | $this->getResponseMock('test-client')->append( |
|
206 | 1 | new Response( |
|
207 | 1 | 200, |
|
208 | 1 | [], |
|
209 | 1 | json_encode( |
|
210 | [ |
||
211 | 1 | 'jsonrpc' => '2.0', |
|
212 | 1 | 'id' => 'test', |
|
213 | 'result' => [ |
||
214 | 1 | 'id' => '1', |
|
215 | 1 | 'payload' => 'test-payload', |
|
216 | 1 | 'references' => ['5', '7'], |
|
217 | 1 | 'sub-payload' => 'sub-payload', |
|
218 | 1 | ], |
|
219 | ] |
||
220 | 1 | ) |
|
221 | 1 | ) |
|
222 | 1 | ); |
|
223 | |||
224 | 1 | $this->getResponseMock('test-reference-client')->append( |
|
225 | 1 | new Response( |
|
226 | 1 | 200, |
|
227 | 1 | [], |
|
228 | 1 | json_encode( |
|
229 | [ |
||
230 | 1 | 'jsonrpc' => '2.0', |
|
231 | 1 | 'id' => 'test', |
|
232 | 'result' => [ |
||
233 | [ |
||
234 | 1 | 'id' => '5', |
|
235 | 1 | 'reference-payload' => 'test-payload-5', |
|
236 | 1 | 'owner' => '1', |
|
237 | 1 | ], |
|
238 | [ |
||
239 | 1 | 'id' => '7', |
|
240 | 1 | 'reference-payload' => 'test-payload-7', |
|
241 | 1 | 'owner' => '1', |
|
242 | 1 | ], |
|
243 | 1 | ], |
|
244 | ] |
||
245 | 1 | ) |
|
246 | 1 | ) |
|
247 | 1 | ); |
|
248 | |||
249 | |||
250 | /** @var SubEntity $entity */ |
||
251 | 1 | $entity = $repository->find(1); |
|
252 | 1 | self::assertSame(1, $entity->getId()); |
|
253 | 1 | self::assertSame('test-payload', $entity->getPayload()); |
|
254 | 1 | self::assertSame('sub-payload', $entity->getSubPayload()); |
|
255 | 1 | self::assertNull($entity->getStringPayload()); |
|
256 | /** @var TestReference[] $references */ |
||
257 | 1 | $references = $entity->getReferences()->toArray(); |
|
258 | |||
259 | 1 | self::assertCount(2, $references); |
|
260 | 1 | foreach ($references as $reference) { |
|
261 | 1 | self::assertSame('test-payload-'.$reference->getId(), $reference->getReferencePayload()); |
|
262 | 1 | } |
|
263 | 1 | } |
|
264 | |||
265 | 4 | protected function getClientNames() |
|
266 | { |
||
267 | 4 | return array_merge(parent::getClientNames(), ['test-reference-client']); |
|
268 | } |
||
269 | } |
||
270 |