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 NodeRepositoryTest 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 NodeRepositoryTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class NodeRepositoryTest extends AbstractKernelTestCase |
||
17 | { |
||
18 | /** |
||
19 | * @var NodeRepository |
||
20 | */ |
||
21 | protected $repository; |
||
22 | protected $userRepository; |
||
23 | |||
24 | /** |
||
25 | * Set up test |
||
26 | */ |
||
27 | View Code Duplication | protected function setUp() |
|
35 | |||
36 | /** |
||
37 | * @param string $language |
||
38 | * @param int $version |
||
39 | * @param string $siteId |
||
40 | * |
||
41 | * @dataProvider provideLanguageLastVersionAndSiteId |
||
42 | */ |
||
43 | public function testFindOnePublished($language, $version, $siteId) |
||
49 | |||
50 | /** |
||
51 | * @return array |
||
52 | */ |
||
53 | View Code Duplication | public function provideLanguageLastVersionAndSiteId() |
|
54 | { |
||
55 | return array( |
||
56 | array('en', '1', '2'), |
||
57 | array('fr', '1', '2'), |
||
58 | ); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param string $language |
||
63 | * @param int $version |
||
64 | * @param string $siteId |
||
65 | * |
||
66 | * @dataProvider provideLanguageLastVersionAndSiteIdNotPublished |
||
67 | */ |
||
68 | public function testFindVersionNotDeleted($language, $version, $siteId) |
||
75 | |||
76 | /** |
||
77 | * @return array |
||
78 | */ |
||
79 | View Code Duplication | public function provideLanguageLastVersionAndSiteIdNotPublished() |
|
86 | |||
87 | /** |
||
88 | * @param string $nodeId |
||
89 | * @param string $language |
||
90 | * @param string $siteId |
||
91 | * @param int $versionExpected |
||
92 | * |
||
93 | * @dataProvider provideNodeLanguageLastVersionAndSiteId |
||
94 | */ |
||
95 | public function testFindInLastVersion($nodeId, $language, $siteId, $versionExpected) |
||
101 | |||
102 | /** |
||
103 | * @return array |
||
104 | */ |
||
105 | View Code Duplication | public function provideNodeLanguageLastVersionAndSiteId() |
|
112 | |||
113 | /** |
||
114 | * @param int $countVersions |
||
115 | * @param string $language |
||
116 | * @param string $siteId |
||
117 | * |
||
118 | * @dataProvider provideLanguageAndVersionListAndSiteId |
||
119 | */ |
||
120 | public function testFindNotDeletedSortByUpdatedAt($countVersions, $language, $siteId) |
||
134 | |||
135 | /** |
||
136 | * @param int $countVersions |
||
137 | * @param string $language |
||
138 | * @param string $siteId |
||
139 | * |
||
140 | * @dataProvider provideLanguageAndVersionListAndSiteId |
||
141 | */ |
||
142 | public function testCountNotDeletedVersions($countVersions, $language, $siteId) |
||
148 | |||
149 | /** |
||
150 | * @return array |
||
151 | */ |
||
152 | View Code Duplication | public function provideLanguageAndVersionListAndSiteId() |
|
153 | { |
||
154 | return array( |
||
155 | array(1, 'en', '2'), |
||
156 | array(3, 'fr', '2'), |
||
157 | ); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param string $nodeId |
||
162 | * @param string $siteId |
||
163 | * @param int $count |
||
164 | * |
||
165 | * @dataProvider provideNodeSiteAndCount |
||
166 | */ |
||
167 | public function testFindByNodeAndSite($nodeId, $siteId, $count) |
||
171 | |||
172 | /** |
||
173 | * @return array |
||
174 | */ |
||
175 | public function provideNodeSiteAndCount() |
||
182 | |||
183 | /** |
||
184 | * @param string $parentId |
||
185 | * @param string $siteId |
||
186 | * @param int $count |
||
187 | * |
||
188 | * @dataProvider provideParentIdSiteIdAndCount |
||
189 | */ |
||
190 | public function testFindByParent($parentId, $siteId, $count) |
||
196 | |||
197 | /** |
||
198 | * @return array |
||
199 | */ |
||
200 | public function provideParentIdSiteIdAndCount() |
||
208 | |||
209 | /** |
||
210 | * Tets find last version by type |
||
211 | */ |
||
212 | public function testFindLastVersionByType() |
||
219 | |||
220 | /** |
||
221 | * @param string $language |
||
222 | * @param int $version |
||
223 | * @param string $siteId |
||
224 | * @param NodeInterface $node |
||
225 | * @param string $nodeId |
||
226 | */ |
||
227 | protected function assertSameNode($language, $version, $siteId, $node, $nodeId = NodeInterface::ROOT_NODE_ID) |
||
236 | |||
237 | /** |
||
238 | * @param string $siteId |
||
239 | * @param int $nodeNumber |
||
240 | * @param int $version |
||
241 | * @param string $language |
||
242 | * @param string|null $nodeId |
||
243 | * |
||
244 | * @dataProvider provideForGetFooter() |
||
245 | */ |
||
246 | View Code Duplication | public function testGetFooterTree($siteId, $nodeNumber, $version, $language = 'fr', $nodeId = null) |
|
256 | |||
257 | /** |
||
258 | * @return array |
||
259 | */ |
||
260 | View Code Duplication | public function provideForGetFooter() |
|
268 | |||
269 | /** |
||
270 | * @param string $siteId |
||
271 | * @param int $nodeNumber |
||
272 | * @param int $version |
||
273 | * @param string $language |
||
274 | * |
||
275 | * @dataProvider provideForGetMenu() |
||
276 | */ |
||
277 | View Code Duplication | public function testGetMenuTree($siteId, $nodeNumber, $version, $language = 'fr') |
|
278 | { |
||
279 | $nodes = $this->repository->getMenuTree($language, $siteId); |
||
280 | |||
281 | $this->assertCount($nodeNumber, $nodes); |
||
282 | $this->assertSameNode($language, $version, $siteId, $nodes[0]); |
||
283 | $this->assertSame('published', $nodes[0]->getStatus()->getName()); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @return array |
||
288 | */ |
||
289 | View Code Duplication | public function provideForGetMenu() |
|
296 | |||
297 | /** |
||
298 | * @param string $nodeId |
||
299 | * @param int $nbLevel |
||
300 | * @param int $nodeNumber |
||
301 | * @param int $version |
||
302 | * @param string $siteId |
||
303 | * @param string $local |
||
304 | * |
||
305 | * @dataProvider provideForGetSubMenu |
||
306 | */ |
||
307 | public function testGetSubMenu($nodeId, $nbLevel, $nodeNumber, $version, $siteId, $local) |
||
308 | { |
||
309 | $nodes = $this->repository->getSubMenu($nodeId, $nbLevel, $local, $siteId); |
||
310 | |||
311 | $this->assertCount($nodeNumber, $nodes); |
||
312 | if ($nodeNumber > 0) { |
||
313 | $this->assertSameNode($local, $version, $siteId, $nodes[0], $nodeId); |
||
314 | $this->assertSame('published', $nodes[0]->getStatus()->getName()); |
||
315 | } |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @return array |
||
320 | */ |
||
321 | public function provideForGetSubMenu() |
||
322 | { |
||
323 | return array( |
||
324 | array(NodeInterface::ROOT_NODE_ID, 1, 5, '1', '2', 'fr'), |
||
325 | array(NodeInterface::ROOT_NODE_ID, 2, 5, '1', '2', 'fr'), |
||
326 | array(NodeInterface::ROOT_NODE_ID, 0, 5, '1', '2', 'fr'), |
||
327 | array(NodeInterface::ROOT_NODE_ID, 0, 6, '1', '2', 'en'), |
||
328 | array('fixture_page_community', 1, 1, '1', '2', 'fr'), |
||
329 | array('fixture_page_community', 1, 1, '1', '2', 'en'), |
||
330 | array('page_unexistant', 1, 0, 1, '2', 'fr'), |
||
331 | ); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @param string $language |
||
336 | * @param string $siteId |
||
337 | * @param int $count |
||
338 | * |
||
339 | * @dataProvider provideLanguageSiteIdAndCount |
||
340 | */ |
||
341 | public function testFindPublishedByLanguageAndSiteId($language, $siteId, $count) |
||
350 | |||
351 | /** |
||
352 | * @return array |
||
353 | */ |
||
354 | View Code Duplication | public function provideLanguageSiteIdAndCount() |
|
355 | { |
||
356 | return array( |
||
357 | array('en', '2', 6), |
||
358 | array('fr', '2', 5), |
||
359 | ); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * @param string $user |
||
364 | * @param string $siteId |
||
365 | * @param array $eventTypes |
||
366 | * @param boolean|null $published |
||
367 | * @param int $limit |
||
368 | * @param array|null $sort |
||
369 | * @param int $count |
||
370 | * |
||
371 | * @dataProvider provideFindByHistoryAndSiteId |
||
372 | */ |
||
373 | View Code Duplication | public function testFindByHistoryAndSiteId($user, $siteId, array $eventTypes, $published, $limit, $sort, $count) |
|
382 | |||
383 | /** |
||
384 | * @return array |
||
385 | */ |
||
386 | public function provideFindByHistoryAndSiteId() |
||
395 | |||
396 | /** |
||
397 | * @param string $nodeId |
||
398 | * @param string $language |
||
399 | * @param int $count |
||
400 | * |
||
401 | * @dataProvider provideFindPublishedSortedVersionData |
||
402 | */ |
||
403 | public function testFindPublishedSortedByVersion($nodeId, $language, $count) |
||
407 | |||
408 | /** |
||
409 | * @return array |
||
410 | */ |
||
411 | public function provideFindPublishedSortedVersionData() |
||
419 | |||
420 | /** |
||
421 | * @param string $language |
||
422 | * @apram int $expectedCount |
||
423 | * |
||
424 | * @dataProvider provideLanguage |
||
425 | */ |
||
426 | public function testFindSubTreeByPath($language, $expectedCount) |
||
432 | |||
433 | /** |
||
434 | * @return array |
||
435 | */ |
||
436 | public function provideLanguage() |
||
437 | { |
||
438 | return array( |
||
439 | array('en', 5), |
||
440 | array('fr', 4), |
||
441 | ); |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * @param string $parentId |
||
446 | * @param string $routePattern |
||
447 | * @param string $nodeId |
||
448 | * |
||
449 | * @dataProvider provideParentRouteAndNodeId |
||
450 | */ |
||
451 | public function testFindByParentAndRoutePattern($parentId, $routePattern, $nodeId) |
||
455 | |||
456 | /** |
||
457 | * @return array |
||
458 | */ |
||
459 | public function provideParentRouteAndNodeId() |
||
466 | |||
467 | /** |
||
468 | * @param string $parentId |
||
469 | * @param int $order |
||
470 | * @param string $nodeId |
||
471 | * @param bool $expectedValue |
||
472 | * @param string $siteId |
||
473 | * |
||
474 | * @dataProvider provideParentAndOrder |
||
475 | */ |
||
476 | public function testHasOtherNodeWithSameParentAndOrder($parentId, $order, $nodeId, $expectedValue, $siteId = '2') |
||
480 | |||
481 | /** |
||
482 | * @return array |
||
483 | */ |
||
484 | public function provideParentAndOrder() |
||
493 | |||
494 | /** |
||
495 | * Test has statused element |
||
496 | */ |
||
497 | View Code Duplication | public function testHasStatusedElement() |
|
504 | |||
505 | /** |
||
506 | * @return array |
||
507 | */ |
||
508 | public function provideNodeIdAndLanguageForPublishedFlag() |
||
517 | |||
518 | /** |
||
519 | * @param string $siteId |
||
520 | * @param integer $expectedCount |
||
521 | * |
||
522 | * @dataProvider provideFindPublishedByType |
||
523 | */ |
||
524 | public function testFindPublishedByType($siteId, $expectedCount) |
||
528 | |||
529 | /** |
||
530 | * @return array |
||
531 | */ |
||
532 | public function provideFindPublishedByType() |
||
539 | |||
540 | /** |
||
541 | * @param string $path |
||
542 | * @param string $siteId |
||
543 | * @param string $language |
||
544 | * @param integer $expectedCount |
||
545 | * |
||
546 | * @dataProvider provideFindPublishedByPathAndLanguage |
||
547 | */ |
||
548 | public function testFindPublishedByPathAndLanguage($path, $siteId, $language, $expectedCount) |
||
552 | |||
553 | /** |
||
554 | * @return array |
||
555 | */ |
||
556 | View Code Duplication | public function provideFindPublishedByPathAndLanguage() |
|
563 | |||
564 | /** |
||
565 | * @param string $path |
||
566 | * @param string $siteId |
||
567 | * @param integer $expectedCount |
||
568 | * |
||
569 | * @dataProvider provideFindByPath |
||
570 | */ |
||
571 | public function testFindByPath($path, $siteId, $expectedCount) |
||
575 | |||
576 | /** |
||
577 | * @return array |
||
578 | */ |
||
579 | View Code Duplication | public function provideFindByPath() |
|
586 | |||
587 | /** |
||
588 | * @param string $path |
||
589 | * @param string $siteId |
||
590 | * @param string $language |
||
591 | * @param integer $expectedCount |
||
592 | * |
||
593 | * @dataProvider provideFindByIncludedPathSiteIdAndLanguage |
||
594 | */ |
||
595 | public function testFindByIncludedPathSiteIdAndLanguage($path, $siteId, $language, $expectedCount) |
||
599 | |||
600 | /** |
||
601 | * @return array |
||
602 | */ |
||
603 | public function provideFindByIncludedPathSiteIdAndLanguage() |
||
609 | |||
610 | /** |
||
611 | * Test find all router pattern |
||
612 | */ |
||
613 | public function testFindAllRoutePattern() |
||
623 | |||
624 | /** |
||
625 | * test find tree node |
||
626 | */ |
||
627 | public function testFindTreeNode() |
||
654 | |||
655 | /** |
||
656 | * @param string $siteId |
||
657 | * @param string $language |
||
658 | * @param int $count |
||
659 | * |
||
660 | * @dataProvider provideSiteIdAndLanguage |
||
661 | */ |
||
662 | public function testCount($siteId, $language, $count) |
||
666 | |||
667 | /** |
||
668 | * @return array |
||
669 | */ |
||
670 | View Code Duplication | public function provideSiteIdAndLanguage() |
|
679 | |||
680 | /** |
||
681 | * @param PaginateFinderConfiguration $configuration |
||
682 | * @param string $siteId |
||
683 | * @param string $language |
||
684 | * @param int $count |
||
685 | * |
||
686 | * @dataProvider provideCountWithFilterPaginateConfiguration |
||
687 | */ |
||
688 | public function testCountWithFilter($configuration, $siteId, $language, $count) |
||
692 | |||
693 | /** |
||
694 | * @return array |
||
695 | */ |
||
696 | public function provideCountWithFilterPaginateConfiguration() |
||
708 | |||
709 | /** |
||
710 | * @param PaginateFinderConfiguration $configuration |
||
711 | * @param string $siteId |
||
712 | * @param string $language |
||
713 | * @param int $count |
||
714 | * |
||
715 | * @dataProvider provideFindWithFilterPaginateConfiguration |
||
716 | */ |
||
717 | public function testFindForPaginate($configuration, $siteId, $language, $count) |
||
721 | |||
722 | /** |
||
723 | * @return array |
||
724 | */ |
||
725 | public function provideFindWithFilterPaginateConfiguration() |
||
741 | |||
742 | /** |
||
743 | * Test update order of brothers |
||
744 | */ |
||
745 | public function testUpdateOrderOfBrothers() |
||
766 | |||
767 | /** |
||
768 | * Test remove block in area |
||
769 | */ |
||
770 | public function testRemoveBlockInArea() |
||
789 | |||
790 | /** |
||
791 | * @param string $nodeId |
||
792 | * @param string $siteId |
||
793 | * @param string $areaId |
||
794 | * @param int $count |
||
795 | * |
||
796 | * @dataProvider provideFindByNodeIdAndSiteIdWithBlocksInArea |
||
797 | */ |
||
798 | public function testFindByNodeIdAndSiteIdWithBlocksInArea($nodeId, $siteId, $areaId, $count) |
||
803 | |||
804 | /** |
||
805 | * @return array |
||
806 | */ |
||
807 | public function provideFindByNodeIdAndSiteIdWithBlocksInArea() |
||
814 | |||
815 | /** |
||
816 | * Test remove node version |
||
817 | */ |
||
818 | View Code Duplication | public function testRemoveVersion() |
|
831 | |||
832 | /** |
||
833 | * @param string $language |
||
834 | * @param string $siteId |
||
835 | * @param int $count |
||
836 | * |
||
837 | * @dataProvider provideLanguageAndSiteIdSpecialPage |
||
838 | */ |
||
839 | public function testFindAllPublishedSpecialPage($language, $siteId, $count) |
||
844 | |||
845 | /** |
||
846 | * @param string $language |
||
847 | * @param string $siteId |
||
848 | * @param int $count |
||
849 | * |
||
850 | * @dataProvider provideLanguageAndSiteIdSpecialPage |
||
851 | */ |
||
852 | public function testFindAllSpecialPage($language, $siteId, $count) |
||
857 | |||
858 | /** |
||
859 | * @return array |
||
860 | */ |
||
861 | public function provideLanguageAndSiteIdSpecialPage() |
||
872 | |||
873 | /** |
||
874 | * @param string $nodeId |
||
875 | * @param string $language |
||
876 | * @param string $siteId |
||
877 | * @param string $name |
||
878 | * @param int $count |
||
879 | * |
||
880 | * @dataProvider provideCountOtherNodeWithSameSpecialPageName |
||
881 | */ |
||
882 | public function testCountOtherNodeWithSameSpecialPageName($nodeId, $language, $siteId, $name, $count) |
||
887 | |||
888 | /** |
||
889 | * @return array |
||
890 | */ |
||
891 | public function provideCountOtherNodeWithSameSpecialPageName() |
||
905 | |||
906 | /** |
||
907 | * Test update use Reference |
||
908 | */ |
||
909 | public function testUpdateUseReference() |
||
921 | |||
922 | /** |
||
923 | * Test soft delete node |
||
924 | */ |
||
925 | public function testSoftDeleteAndRestoreNode() |
||
946 | |||
947 | /** |
||
948 | * @param string $nodeId |
||
949 | * @param string $siteId |
||
950 | * @param bool $has |
||
951 | * |
||
952 | * @dataProvider provideNodeNotOffline |
||
953 | */ |
||
954 | public function testHasNodeIdWithoutAutoUnpublishToState($nodeId, $siteId, $has) |
||
958 | |||
959 | /** |
||
960 | * @return array |
||
961 | */ |
||
962 | public function provideNodeNotOffline() |
||
969 | |||
970 | /** |
||
971 | * @param string $parentId |
||
972 | * @param string $siteId |
||
973 | * @param int $count |
||
974 | * |
||
975 | * @dataProvider provideCountByParentId |
||
976 | */ |
||
977 | public function testCountByParentId($parentId, $siteId, $count) |
||
981 | |||
982 | /** |
||
983 | * @return array |
||
984 | */ |
||
985 | View Code Duplication | public function provideCountByParentId() |
|
992 | |||
993 | /** |
||
994 | * @param string $nodeId |
||
995 | * @param string $siteId |
||
996 | * @param int $count |
||
997 | * |
||
998 | * @dataProvider provideCountById |
||
999 | */ |
||
1000 | public function testCountById($nodeId, $siteId, $count) |
||
1004 | |||
1005 | /** |
||
1006 | * @return array |
||
1007 | */ |
||
1008 | View Code Duplication | public function provideCountById() |
|
1015 | |||
1016 | /** |
||
1017 | * Test update embedded status |
||
1018 | */ |
||
1019 | View Code Duplication | public function testUpdateEmbeddedStatus() |
|
1034 | |||
1035 | /** |
||
1036 | * Test findLastVersionByLanguage |
||
1037 | * @param string $siteId |
||
1038 | * @param string $language |
||
1039 | * @param int $count |
||
1040 | * |
||
1041 | * @dataProvider provideSiteAndLanguage |
||
1042 | */ |
||
1043 | public function testFindLastVersionByLanguage($siteId, $language, $count) |
||
1049 | |||
1050 | /** |
||
1051 | * @return array |
||
1052 | */ |
||
1053 | View Code Duplication | public function provideSiteAndLanguage() |
|
1062 | |||
1063 | } |
||
1064 |
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.