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:
Complex classes like DomainManagerTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DomainManagerTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class DomainManagerTest extends \PHPUnit_Framework_TestCase |
||
28 | { |
||
29 | /** |
||
30 | * @var DomainManager |
||
31 | */ |
||
32 | private $domainManager; |
||
33 | |||
34 | /** |
||
35 | * @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
||
36 | */ |
||
37 | private $resource; |
||
38 | |||
39 | /** |
||
40 | * @var \PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface |
||
41 | */ |
||
42 | private $eventDispatcher; |
||
43 | |||
44 | /** |
||
45 | * @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager |
||
46 | */ |
||
47 | private $objectManager; |
||
48 | |||
49 | /** |
||
50 | * @var \PHPUnit_Framework_MockObject_MockObject|RepositoryInterface |
||
51 | */ |
||
52 | private $repository; |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | protected function setUp() |
||
58 | { |
||
59 | $this->resource = $this->createResourceMock(); |
||
60 | $this->eventDispatcher = $this->createEventDispatcherMock(); |
||
61 | $this->objectManager = $this->createObjectManagerMock(); |
||
62 | $this->repository = $this->createRepositoryMock(); |
||
63 | |||
64 | $this->domainManager = new DomainManager( |
||
65 | $this->resource, |
||
66 | $this->eventDispatcher, |
||
67 | $this->objectManager, |
||
68 | $this->repository |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | public function testInheritance() |
||
73 | { |
||
74 | $this->assertInstanceOf(DomainManagerInterface::class, $this->domainManager); |
||
75 | } |
||
76 | |||
77 | public function testFind() |
||
78 | { |
||
79 | $this->resource |
||
|
|||
80 | ->expects($this->exactly(2)) |
||
81 | ->method('getName') |
||
82 | ->will($this->returnValue($name = 'name')); |
||
83 | |||
84 | $this->repository |
||
85 | ->expects($this->once()) |
||
86 | ->method($repositoryMethod = 'findForShow') |
||
87 | ->with( |
||
88 | $this->identicalTo($criteria = ['criteria']), |
||
89 | $this->identicalTo($sorting = ['sorting']) |
||
90 | ) |
||
91 | ->will($this->returnValue($data = new \stdClass())); |
||
92 | |||
93 | $this->eventDispatcher |
||
94 | ->expects($this->at(0)) |
||
95 | ->method('dispatch') |
||
96 | ->with( |
||
97 | $this->identicalTo('lug.'.$name.'.pre_find.'.($action = 'action')), |
||
98 | $this->callback(function (DomainEvent $event) use ($action) { |
||
99 | return $event->getResource() === $this->resource |
||
100 | && $event->getData() === null |
||
101 | && $event->getAction() === 'find.'.$action; |
||
102 | }) |
||
103 | ); |
||
104 | |||
105 | $this->eventDispatcher |
||
106 | ->expects($this->at(1)) |
||
107 | ->method('dispatch') |
||
108 | ->with( |
||
109 | $this->identicalTo('lug.'.$name.'.post_find.'.$action), |
||
110 | $this->callback(function (DomainEvent $event) use ($data, $action) { |
||
111 | return $event->getResource() === $this->resource |
||
112 | && $event->getData() === $data |
||
113 | && $event->getAction() === 'find.'.$action; |
||
114 | }) |
||
115 | ); |
||
116 | |||
117 | $this->assertSame($data, $this->domainManager->find($action, $repositoryMethod, $criteria, $sorting)); |
||
118 | } |
||
119 | |||
120 | public function testFindThrowException() |
||
121 | { |
||
122 | $this->resource |
||
123 | ->expects($this->exactly(2)) |
||
124 | ->method('getName') |
||
125 | ->will($this->returnValue($name = 'name')); |
||
126 | |||
127 | $this->repository |
||
128 | ->expects($this->once()) |
||
129 | ->method($repositoryMethod = 'findForShow') |
||
130 | ->with( |
||
131 | $this->identicalTo($criteria = ['criteria']), |
||
132 | $this->identicalTo($sorting = ['sorting']) |
||
133 | ) |
||
134 | ->will($this->throwException(new \Exception())); |
||
135 | |||
136 | $this->eventDispatcher |
||
137 | ->expects($this->at(0)) |
||
138 | ->method('dispatch') |
||
139 | ->with( |
||
140 | $this->identicalTo('lug.'.$name.'.pre_find.'.($action = 'action')), |
||
141 | $this->callback(function (DomainEvent $event) use ($action) { |
||
142 | return $event->getResource() === $this->resource |
||
143 | && $event->getData() === null |
||
144 | && $event->getAction() === 'find.'.$action; |
||
145 | }) |
||
146 | ); |
||
147 | |||
148 | $statusCode = Response::HTTP_BAD_REQUEST; |
||
149 | $message = 'message'; |
||
150 | |||
151 | $this->eventDispatcher |
||
152 | ->expects($this->at(1)) |
||
153 | ->method('dispatch') |
||
154 | ->with( |
||
155 | $this->identicalTo('lug.'.$name.'.error_find.'.$action), |
||
156 | $this->callback(function (DomainEvent $event) use ($action, $statusCode, $message) { |
||
157 | $result = $event->getResource() === $this->resource |
||
158 | && $event->getData() === null |
||
159 | && $event->getAction() === 'find.'.$action; |
||
160 | |||
161 | $event->setStatusCode($statusCode); |
||
162 | $event->setMessage($message); |
||
163 | |||
164 | return $result; |
||
165 | }) |
||
166 | ); |
||
167 | |||
168 | try { |
||
169 | $this->domainManager->find($action, $repositoryMethod, $criteria, $sorting); |
||
170 | $this->fail(); |
||
171 | } catch (DomainException $e) { |
||
172 | $this->assertSame($statusCode, $e->getStatusCode()); |
||
173 | $this->assertSame($message, $e->getMessage()); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | public function testFindByPassException() |
||
178 | { |
||
179 | $this->resource |
||
180 | ->expects($this->exactly(2)) |
||
181 | ->method('getName') |
||
182 | ->will($this->returnValue($name = 'name')); |
||
183 | |||
184 | $this->repository |
||
185 | ->expects($this->once()) |
||
186 | ->method($repositoryMethod = 'findForShow') |
||
187 | ->with( |
||
188 | $this->identicalTo($criteria = ['criteria']), |
||
189 | $this->identicalTo($sorting = ['sorting']) |
||
190 | ) |
||
191 | ->will($this->throwException(new \Exception())); |
||
192 | |||
193 | $this->eventDispatcher |
||
194 | ->expects($this->at(0)) |
||
195 | ->method('dispatch') |
||
196 | ->with( |
||
197 | $this->identicalTo('lug.'.$name.'.pre_find.'.($action = 'action')), |
||
198 | $this->callback(function (DomainEvent $event) use ($action) { |
||
199 | return $event->getResource() === $this->resource |
||
200 | && $event->getData() === null |
||
201 | && $event->getAction() === 'find.'.$action; |
||
202 | }) |
||
203 | ); |
||
204 | |||
205 | $this->eventDispatcher |
||
206 | ->expects($this->at(1)) |
||
207 | ->method('dispatch') |
||
208 | ->with( |
||
209 | $this->identicalTo('lug.'.$name.'.error_find.'.$action), |
||
210 | $this->callback(function (DomainEvent $event) use ($action) { |
||
211 | $result = $event->getResource() === $this->resource |
||
212 | && $event->getData() === null |
||
213 | && $event->getAction() === 'find.'.$action; |
||
214 | |||
215 | $event->setStopped(false); |
||
216 | |||
217 | return $result; |
||
218 | }) |
||
219 | ); |
||
220 | |||
221 | $this->assertNull($this->domainManager->find($action, $repositoryMethod, $criteria, $sorting)); |
||
222 | } |
||
223 | |||
224 | View Code Duplication | public function testCreate() |
|
225 | { |
||
226 | $this->resource |
||
227 | ->expects($this->exactly(4)) |
||
228 | ->method('getName') |
||
229 | ->will($this->returnValue($name = 'name')); |
||
230 | |||
231 | $this->objectManager |
||
232 | ->expects($this->once()) |
||
233 | ->method('persist') |
||
234 | ->with($this->identicalTo($data = new \stdClass())); |
||
235 | |||
236 | $this->objectManager |
||
237 | ->expects($this->once()) |
||
238 | ->method('flush'); |
||
239 | |||
240 | $this->eventDispatcher |
||
241 | ->expects($this->at(0)) |
||
242 | ->method('dispatch') |
||
243 | ->with( |
||
244 | $this->identicalTo('lug.'.$name.'.pre_'.($createAction = 'create')), |
||
245 | $this->callback(function (DomainEvent $event) use ($createAction, $data) { |
||
246 | return $event->getResource() === $this->resource |
||
247 | && $event->getData() === $data |
||
248 | && $event->getAction() === $createAction; |
||
249 | }) |
||
250 | ); |
||
251 | |||
252 | $this->eventDispatcher |
||
253 | ->expects($this->at(1)) |
||
254 | ->method('dispatch') |
||
255 | ->with( |
||
256 | $this->identicalTo('lug.'.$name.'.pre_'.($flushAction = 'flush')), |
||
257 | $this->callback(function (DomainEvent $event) use ($flushAction, $data) { |
||
258 | return $event->getResource() === $this->resource |
||
259 | && $event->getData() === $data |
||
260 | && $event->getAction() === $flushAction; |
||
261 | }) |
||
262 | ); |
||
263 | |||
264 | $this->eventDispatcher |
||
265 | ->expects($this->at(2)) |
||
266 | ->method('dispatch') |
||
267 | ->with( |
||
268 | $this->identicalTo('lug.'.$name.'.post_'.$flushAction), |
||
269 | $this->callback(function (DomainEvent $event) use ($flushAction, $data) { |
||
270 | return $event->getResource() === $this->resource |
||
271 | && $event->getData() === $data |
||
272 | && $event->getAction() === $flushAction; |
||
273 | }) |
||
274 | ); |
||
275 | |||
276 | $this->eventDispatcher |
||
277 | ->expects($this->at(3)) |
||
278 | ->method('dispatch') |
||
279 | ->with( |
||
280 | $this->identicalTo('lug.'.$name.'.post_'.$createAction), |
||
281 | $this->callback(function (DomainEvent $event) use ($data, $createAction) { |
||
282 | return $event->getResource() === $this->resource |
||
283 | && $event->getData() === $data |
||
284 | && $event->getAction() === $createAction; |
||
285 | }) |
||
286 | ); |
||
287 | |||
288 | $this->domainManager->create($data); |
||
289 | } |
||
290 | |||
291 | View Code Duplication | public function testCreateWithoutFlush() |
|
292 | { |
||
293 | $this->resource |
||
294 | ->expects($this->exactly(2)) |
||
295 | ->method('getName') |
||
296 | ->will($this->returnValue($name = 'name')); |
||
297 | |||
298 | $this->objectManager |
||
299 | ->expects($this->once()) |
||
300 | ->method('persist') |
||
301 | ->with($this->identicalTo($data = new \stdClass())); |
||
302 | |||
303 | $this->objectManager |
||
304 | ->expects($this->never()) |
||
305 | ->method('flush'); |
||
306 | |||
307 | $this->eventDispatcher |
||
308 | ->expects($this->at(0)) |
||
309 | ->method('dispatch') |
||
310 | ->with( |
||
311 | $this->identicalTo('lug.'.$name.'.pre_'.($createAction = 'create')), |
||
312 | $this->callback(function (DomainEvent $event) use ($createAction, $data) { |
||
313 | return $event->getResource() === $this->resource |
||
314 | && $event->getData() === $data |
||
315 | && $event->getAction() === $createAction; |
||
316 | }) |
||
317 | ); |
||
318 | |||
319 | $this->eventDispatcher |
||
320 | ->expects($this->at(1)) |
||
321 | ->method('dispatch') |
||
322 | ->with( |
||
323 | $this->identicalTo('lug.'.$name.'.post_'.$createAction), |
||
324 | $this->callback(function (DomainEvent $event) use ($data, $createAction) { |
||
325 | return $event->getResource() === $this->resource |
||
326 | && $event->getData() === $data |
||
327 | && $event->getAction() === $createAction; |
||
328 | }) |
||
329 | ); |
||
330 | |||
331 | $this->domainManager->create($data, false); |
||
332 | } |
||
333 | |||
334 | View Code Duplication | public function testCreateThrowException() |
|
335 | { |
||
336 | $this->resource |
||
337 | ->expects($this->exactly(4)) |
||
338 | ->method('getName') |
||
339 | ->will($this->returnValue($name = 'name')); |
||
340 | |||
341 | $this->objectManager |
||
342 | ->expects($this->once()) |
||
343 | ->method('persist') |
||
344 | ->with($this->identicalTo($data = new \stdClass())); |
||
345 | |||
346 | $this->objectManager |
||
347 | ->expects($this->once()) |
||
348 | ->method('flush') |
||
349 | ->will($this->throwException(new \Exception())); |
||
350 | |||
351 | $this->eventDispatcher |
||
352 | ->expects($this->at(0)) |
||
353 | ->method('dispatch') |
||
354 | ->with( |
||
355 | $this->identicalTo('lug.'.$name.'.pre_'.($createAction = 'create')), |
||
356 | $this->callback(function (DomainEvent $event) use ($createAction, $data) { |
||
357 | return $event->getResource() === $this->resource |
||
358 | && $event->getData() === $data |
||
359 | && $event->getAction() === $createAction; |
||
360 | }) |
||
361 | ); |
||
362 | |||
363 | $this->eventDispatcher |
||
364 | ->expects($this->at(1)) |
||
365 | ->method('dispatch') |
||
366 | ->with( |
||
367 | $this->identicalTo('lug.'.$name.'.pre_'.($flushAction = 'flush')), |
||
368 | $this->callback(function (DomainEvent $event) use ($flushAction, $data) { |
||
369 | return $event->getResource() === $this->resource |
||
370 | && $event->getData() === $data |
||
371 | && $event->getAction() === $flushAction; |
||
372 | }) |
||
373 | ); |
||
374 | |||
375 | $this->eventDispatcher |
||
376 | ->expects($this->at(2)) |
||
377 | ->method('dispatch') |
||
378 | ->with( |
||
379 | $this->identicalTo('lug.'.$name.'.error_'.$flushAction), |
||
380 | $this->callback(function (DomainEvent $event) use ($flushAction, $data) { |
||
381 | return $event->getResource() === $this->resource |
||
382 | && $event->getData() === $data |
||
383 | && $event->getAction() === $flushAction; |
||
384 | }) |
||
385 | ); |
||
386 | |||
387 | $statusCode = Response::HTTP_BAD_REQUEST; |
||
388 | $message = 'message'; |
||
389 | |||
390 | $this->eventDispatcher |
||
391 | ->expects($this->at(3)) |
||
392 | ->method('dispatch') |
||
393 | ->with( |
||
394 | $this->identicalTo('lug.'.$name.'.error_'.$createAction), |
||
395 | $this->callback(function (DomainEvent $event) use ($data, $createAction, $statusCode, $message) { |
||
396 | $result = $event->getResource() === $this->resource |
||
397 | && $event->getData() === $data |
||
398 | && $event->getAction() === $createAction; |
||
399 | |||
400 | $event->setStatusCode($statusCode); |
||
401 | $event->setMessage($message); |
||
402 | |||
403 | return $result; |
||
404 | }) |
||
405 | ); |
||
406 | |||
407 | try { |
||
408 | $this->domainManager->create($data); |
||
409 | $this->fail(); |
||
410 | } catch (DomainException $e) { |
||
411 | $this->assertSame($statusCode, $e->getStatusCode()); |
||
412 | $this->assertSame($message, $e->getMessage()); |
||
413 | } |
||
414 | } |
||
415 | |||
416 | View Code Duplication | public function testCreateByPassException() |
|
417 | { |
||
418 | $this->resource |
||
419 | ->expects($this->exactly(4)) |
||
420 | ->method('getName') |
||
421 | ->will($this->returnValue($name = 'name')); |
||
422 | |||
423 | $this->objectManager |
||
424 | ->expects($this->once()) |
||
425 | ->method('persist') |
||
426 | ->with($this->identicalTo($data = new \stdClass())); |
||
427 | |||
428 | $this->objectManager |
||
429 | ->expects($this->once()) |
||
430 | ->method('flush') |
||
431 | ->will($this->throwException(new \Exception())); |
||
432 | |||
433 | $this->eventDispatcher |
||
434 | ->expects($this->at(0)) |
||
435 | ->method('dispatch') |
||
436 | ->with( |
||
437 | $this->identicalTo('lug.'.$name.'.pre_'.($createAction = 'create')), |
||
438 | $this->callback(function (DomainEvent $event) use ($createAction, $data) { |
||
439 | return $event->getResource() === $this->resource |
||
440 | && $event->getData() === $data |
||
441 | && $event->getAction() === $createAction; |
||
442 | }) |
||
443 | ); |
||
444 | |||
445 | $this->eventDispatcher |
||
446 | ->expects($this->at(1)) |
||
447 | ->method('dispatch') |
||
448 | ->with( |
||
449 | $this->identicalTo('lug.'.$name.'.pre_'.($flushAction = 'flush')), |
||
450 | $this->callback(function (DomainEvent $event) use ($flushAction, $data) { |
||
451 | return $event->getResource() === $this->resource |
||
452 | && $event->getData() === $data |
||
453 | && $event->getAction() === $flushAction; |
||
454 | }) |
||
455 | ); |
||
456 | |||
457 | $this->eventDispatcher |
||
458 | ->expects($this->at(2)) |
||
459 | ->method('dispatch') |
||
460 | ->with( |
||
461 | $this->identicalTo('lug.'.$name.'.error_'.$flushAction), |
||
462 | $this->callback(function (DomainEvent $event) use ($flushAction, $data) { |
||
463 | return $event->getResource() === $this->resource |
||
464 | && $event->getData() === $data |
||
465 | && $event->getAction() === $flushAction; |
||
466 | }) |
||
467 | ); |
||
468 | |||
469 | $this->eventDispatcher |
||
470 | ->expects($this->at(3)) |
||
471 | ->method('dispatch') |
||
472 | ->with( |
||
473 | $this->identicalTo('lug.'.$name.'.error_'.$createAction), |
||
474 | $this->callback(function (DomainEvent $event) use ($data, $createAction) { |
||
475 | $result = $event->getResource() === $this->resource |
||
476 | && $event->getData() === $data |
||
477 | && $event->getAction() === $createAction; |
||
478 | |||
479 | $event->setStopped(false); |
||
480 | |||
481 | return $result; |
||
482 | }) |
||
483 | ); |
||
484 | |||
485 | $this->domainManager->create($data); |
||
486 | } |
||
487 | |||
488 | View Code Duplication | public function testUpdateWithManagedObject() |
|
559 | |||
560 | View Code Duplication | public function testUpdateWithoutManagedObject() |
|
632 | |||
633 | View Code Duplication | public function testUpdateWithoutFlush() |
|
634 | { |
||
635 | $this->resource |
||
636 | ->expects($this->exactly(2)) |
||
637 | ->method('getName') |
||
638 | ->will($this->returnValue($name = 'name')); |
||
639 | |||
640 | $this->objectManager |
||
641 | ->expects($this->once()) |
||
675 | |||
676 | public function testUpdateThrowException() |
||
763 | |||
764 | public function testUpdateByPassException() |
||
841 | |||
842 | View Code Duplication | public function testDelete() |
|
908 | |||
909 | View Code Duplication | public function testDeleteWithoutFlush() |
|
951 | |||
952 | View Code Duplication | public function testDeleteThrowException() |
|
1033 | |||
1034 | View Code Duplication | public function testDeleteByPassException() |
|
1105 | |||
1106 | public function testFlush() |
||
1145 | |||
1146 | public function testFlushWithoutObject() |
||
1183 | |||
1184 | public function testFlushThrowException() |
||
1236 | |||
1237 | public function testFlushByPassException() |
||
1279 | |||
1280 | /** |
||
1281 | * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
||
1282 | */ |
||
1283 | private function createResourceMock() |
||
1287 | |||
1288 | /** |
||
1289 | * @return \PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface |
||
1290 | */ |
||
1291 | private function createEventDispatcherMock() |
||
1295 | |||
1296 | /** |
||
1297 | * @return \PHPUnit_Framework_MockObject_MockObject|ObjectManager |
||
1298 | */ |
||
1299 | private function createObjectManagerMock() |
||
1303 | |||
1304 | /** |
||
1305 | * @return \PHPUnit_Framework_MockObject_MockObject|RepositoryInterface |
||
1306 | */ |
||
1307 | private function createRepositoryMock() |
||
1311 | } |
||
1312 |
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: