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() |
|
|
|||
28 | { |
||
29 | parent::setUp(); |
||
30 | |||
31 | static::bootKernel(); |
||
32 | $this->repository = static::$kernel->getContainer()->get('open_orchestra_model.repository.node'); |
||
33 | $this->userRepository = static::$kernel->getContainer()->get('open_orchestra_user.repository.user'); |
||
34 | } |
||
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) |
||
44 | { |
||
45 | $node = $this->repository->findOnePublished(NodeInterface::ROOT_NODE_ID, $language, $siteId); |
||
46 | |||
47 | $this->assertSameNode($language, $version, $siteId, $node); |
||
48 | } |
||
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) |
||
69 | { |
||
70 | $node = $this->repository->findVersionNotDeleted(NodeInterface::ROOT_NODE_ID, $language, $siteId, $version); |
||
71 | |||
72 | $this->assertSame($node->getNodeId(), NodeInterface::ROOT_NODE_ID); |
||
73 | $this->assertSame($node->getVersion(), $version); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return array |
||
78 | */ |
||
79 | View Code Duplication | public function provideLanguageLastVersionAndSiteIdNotPublished() |
|
80 | { |
||
81 | return array( |
||
82 | array('fr', 2, '2', 3), |
||
83 | array('fr', 1, '2', 3), |
||
84 | ); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param string $language |
||
89 | * @param int $version |
||
90 | * @param string $siteId |
||
91 | * @param int $versionExpected |
||
92 | * |
||
93 | * @dataProvider provideLanguageLastVersionAndSiteIdNotPublished |
||
94 | */ |
||
95 | public function testFindInLastVersion($language, $version = null, $siteId, $versionExpected) |
||
96 | { |
||
97 | $node = $this->repository->findInLastVersion(NodeInterface::ROOT_NODE_ID, $language, $siteId); |
||
98 | |||
99 | $this->assertSameNode($language, $versionExpected, $siteId, $node); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param int $countVersions |
||
104 | * @param string $language |
||
105 | * @param string $siteId |
||
106 | * |
||
107 | * @dataProvider provideLanguageAndVersionListAndSiteId |
||
108 | */ |
||
109 | public function testFindNotDeletedSortByUpdatedAt($countVersions, $language, $siteId) |
||
110 | { |
||
111 | $nodes = $this->repository->findNotDeletedSortByUpdatedAt(NodeInterface::ROOT_NODE_ID, $language, $siteId); |
||
112 | |||
113 | $this->assertCount($countVersions, $nodes); |
||
114 | foreach ($nodes as $node) { |
||
115 | $this->assertSameNode($language, $node->getVersion(), $siteId, $node); |
||
116 | } |
||
117 | if (count($nodes) > 1) { |
||
118 | for ($i = 1; $i < count($nodes); $i++) { |
||
119 | $this->assertGreaterThan($nodes[$i]->getVersion(), $nodes[$i-1]->getVersion()); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param int $countVersions |
||
126 | * @param string $language |
||
127 | * @param string $siteId |
||
128 | * |
||
129 | * @dataProvider provideLanguageAndVersionListAndSiteId |
||
130 | */ |
||
131 | public function testCountNotDeletedVersions($countVersions, $language, $siteId) |
||
132 | { |
||
133 | $countNodes = $this->repository->countNotDeletedVersions(NodeInterface::ROOT_NODE_ID, $language, $siteId); |
||
134 | |||
135 | $this->assertSame($countVersions, $countNodes); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return array |
||
140 | */ |
||
141 | View Code Duplication | public function provideLanguageAndVersionListAndSiteId() |
|
142 | { |
||
143 | return array( |
||
144 | array(1, 'en', '2'), |
||
145 | array(3, 'fr', '2'), |
||
146 | ); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param string $nodeId |
||
151 | * @param string $siteId |
||
152 | * @param int $count |
||
153 | * |
||
154 | * @dataProvider provideNodeSiteAndCount |
||
155 | */ |
||
156 | public function testFindByNodeAndSite($nodeId, $siteId, $count) |
||
157 | { |
||
158 | $this->assertCount($count, $this->repository->findByNodeAndSite($nodeId, $siteId)); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @return array |
||
163 | */ |
||
164 | public function provideNodeSiteAndCount() |
||
165 | { |
||
166 | return array( |
||
167 | array(NodeInterface::ROOT_NODE_ID, '2', 5), |
||
168 | array('fixture_page_what_is_orchestra', '2', 0), |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param string $parentId |
||
174 | * @param string $siteId |
||
175 | * @param int $count |
||
176 | * |
||
177 | * @dataProvider provideParentIdSiteIdAndCount |
||
178 | */ |
||
179 | public function testFindByParent($parentId, $siteId, $count) |
||
180 | { |
||
181 | $nodes = $this->repository->findByParent($parentId, $siteId); |
||
182 | |||
183 | $this->assertGreaterThanOrEqual($count, count($nodes)); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return array |
||
188 | */ |
||
189 | public function provideParentIdSiteIdAndCount() |
||
190 | { |
||
191 | return array( |
||
192 | array(NodeInterface::ROOT_NODE_ID, '2', 5), |
||
193 | array('fixture_page_community', '2', 0), |
||
194 | array('fixture_page_what_is_orchestra', '2', 0), |
||
195 | ); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param string $path |
||
200 | * @param string $siteId |
||
201 | * @param int $count |
||
202 | * |
||
203 | * @dataProvider providePathSiteIdAndCount |
||
204 | */ |
||
205 | public function testFindByIncludedPathAndSiteId($path, $siteId, $count) |
||
211 | |||
212 | /** |
||
213 | * @return array |
||
214 | */ |
||
215 | public function providePathSiteIdAndCount() |
||
223 | |||
224 | /** |
||
225 | * @param string $siteId |
||
226 | * @param int $version |
||
227 | * |
||
228 | * @dataProvider provideSiteIdAndLastVersion |
||
229 | */ |
||
230 | public function testFindLastVersionByType($siteId, $version) |
||
236 | |||
237 | /** |
||
238 | * @return array |
||
239 | */ |
||
240 | public function provideSiteIdAndLastVersion() |
||
246 | |||
247 | /** |
||
248 | * @param string $language |
||
249 | * @param int $version |
||
250 | * @param string $siteId |
||
251 | * @param NodeInterface $node |
||
252 | * @param string $nodeId |
||
253 | */ |
||
254 | protected function assertSameNode($language, $version, $siteId, $node, $nodeId = NodeInterface::ROOT_NODE_ID) |
||
263 | |||
264 | /** |
||
265 | * @param string $siteId |
||
266 | * @param int $nodeNumber |
||
267 | * @param int $version |
||
268 | * @param string $language |
||
269 | * @param string|null $nodeId |
||
270 | * |
||
271 | * @dataProvider provideForGetFooter() |
||
272 | */ |
||
273 | View Code Duplication | public function testGetFooterTree($siteId, $nodeNumber, $version, $language = 'fr', $nodeId = null) |
|
274 | { |
||
275 | $nodes = $this->repository->getFooterTree($language, $siteId); |
||
276 | |||
277 | $this->assertCount($nodeNumber, $nodes); |
||
278 | if ($nodeId) { |
||
279 | $this->assertSameNode($language, $version, $siteId, $nodes[0], $nodeId); |
||
280 | $this->assertSame('published', $nodes[0]->getStatus()->getName()); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @return array |
||
286 | */ |
||
287 | View Code Duplication | public function provideForGetFooter() |
|
288 | { |
||
289 | return array( |
||
290 | array('2', 1, 1, 'fr', 'fixture_page_legal_mentions'), |
||
291 | array('2', 1, 1, 'en'), |
||
292 | array('2', 1, 1), |
||
293 | ); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @param string $siteId |
||
298 | * @param int $nodeNumber |
||
299 | * @param int $version |
||
300 | * @param string $language |
||
301 | * |
||
302 | * @dataProvider provideForGetMenu() |
||
303 | */ |
||
304 | View Code Duplication | public function testGetMenuTree($siteId, $nodeNumber, $version, $language = 'fr') |
|
305 | { |
||
306 | $nodes = $this->repository->getMenuTree($language, $siteId); |
||
307 | |||
308 | $this->assertCount($nodeNumber, $nodes); |
||
309 | $this->assertSameNode($language, $version, $siteId, $nodes[0]); |
||
310 | $this->assertSame('published', $nodes[0]->getStatus()->getName()); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * @return array |
||
315 | */ |
||
316 | View Code Duplication | public function provideForGetMenu() |
|
323 | |||
324 | /** |
||
325 | * @param string $nodeId |
||
326 | * @param int $nbLevel |
||
327 | * @param int $nodeNumber |
||
328 | * @param int $version |
||
329 | * @param string $siteId |
||
330 | * @param string $local |
||
331 | * |
||
332 | * @dataProvider provideForGetSubMenu |
||
333 | */ |
||
334 | public function testGetSubMenu($nodeId, $nbLevel, $nodeNumber, $version, $siteId, $local) |
||
335 | { |
||
336 | $nodes = $this->repository->getSubMenu($nodeId, $nbLevel, $local, $siteId); |
||
337 | |||
338 | $this->assertCount($nodeNumber, $nodes); |
||
339 | if ($nodeNumber > 0) { |
||
340 | $this->assertSameNode($local, $version, $siteId, $nodes[0], $nodeId); |
||
341 | $this->assertSame('published', $nodes[0]->getStatus()->getName()); |
||
342 | } |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @return array |
||
347 | */ |
||
348 | public function provideForGetSubMenu() |
||
360 | |||
361 | /** |
||
362 | * @param string $language |
||
363 | * @param string $siteId |
||
364 | * @param int $count |
||
365 | * |
||
366 | * @dataProvider provideLanguageSiteIdAndCount |
||
367 | */ |
||
368 | public function testFindPublishedByLanguageAndSiteId($language, $siteId, $count) |
||
369 | { |
||
370 | $nodes = $this->repository->findPublishedByLanguageAndSiteId($language, $siteId); |
||
371 | |||
372 | $this->assertCount($count, $nodes); |
||
373 | foreach ($nodes as $node) { |
||
374 | $this->assertSame($language, $node->getLanguage()); |
||
375 | } |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * @return array |
||
380 | */ |
||
381 | View Code Duplication | public function provideLanguageSiteIdAndCount() |
|
388 | |||
389 | /** |
||
390 | * @param string $user |
||
391 | * @param string $siteId |
||
392 | * @param array $eventTypes |
||
393 | * @param boolean|null $published |
||
394 | * @param int $limit |
||
395 | * @param array|null $sort |
||
396 | * @param int $count |
||
397 | * |
||
398 | * @dataProvider provideFindByHistoryAndSiteId |
||
399 | */ |
||
400 | View Code Duplication | public function testFindByHistoryAndSiteId($user, $siteId, array $eventTypes, $published, $limit, $sort, $count) |
|
409 | |||
410 | /** |
||
411 | * @return array |
||
412 | */ |
||
413 | public function provideFindByHistoryAndSiteId() |
||
422 | |||
423 | /** |
||
424 | * @param string $nodeId |
||
425 | * @param string $language |
||
426 | * @param int $count |
||
427 | * |
||
428 | * @dataProvider provideFindPublishedSortedVersionData |
||
429 | */ |
||
430 | public function testFindPublishedSortedByVersion($nodeId, $language, $count) |
||
434 | |||
435 | /** |
||
436 | * @return array |
||
437 | */ |
||
438 | public function provideFindPublishedSortedVersionData() |
||
446 | |||
447 | /** |
||
448 | * @param string $language |
||
449 | * @apram int $expectedCount |
||
450 | * |
||
451 | * @dataProvider provideLanguage |
||
452 | */ |
||
453 | public function testFindSubTreeByPath($language, $expectedCount) |
||
459 | |||
460 | /** |
||
461 | * @return array |
||
462 | */ |
||
463 | public function provideLanguage() |
||
470 | |||
471 | /** |
||
472 | * @param string $parentId |
||
473 | * @param string $routePattern |
||
474 | * @param string $nodeId |
||
475 | * |
||
476 | * @dataProvider provideParentRouteAndNodeId |
||
477 | */ |
||
478 | public function testFindByParentAndRoutePattern($parentId, $routePattern, $nodeId) |
||
482 | |||
483 | /** |
||
484 | * @return array |
||
485 | */ |
||
486 | public function provideParentRouteAndNodeId() |
||
493 | |||
494 | /** |
||
495 | * @param string $parentId |
||
496 | * @param int $order |
||
497 | * @param string $nodeId |
||
498 | * @param bool $expectedValue |
||
499 | * @param string $siteId |
||
500 | * |
||
501 | * @dataProvider provideParentAndOrder |
||
502 | */ |
||
503 | public function testHasOtherNodeWithSameParentAndOrder($parentId, $order, $nodeId, $expectedValue, $siteId = '2') |
||
507 | |||
508 | /** |
||
509 | * @return array |
||
510 | */ |
||
511 | public function provideParentAndOrder() |
||
520 | |||
521 | /** |
||
522 | * Test has statused element |
||
523 | */ |
||
524 | View Code Duplication | public function testHasStatusedElement() |
|
525 | { |
||
526 | $statusRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.status'); |
||
527 | $status = $statusRepository->findOneByInitial(); |
||
528 | |||
529 | $this->assertTrue($this->repository->hasStatusedElement($status)); |
||
530 | } |
||
531 | |||
532 | /** |
||
533 | * Test find by site and defaultTheme |
||
534 | */ |
||
535 | public function testFindBySiteIdAndDefaultTheme() |
||
540 | |||
541 | /** |
||
542 | * @return array |
||
543 | */ |
||
544 | public function provideNodeIdAndLanguageForPublishedFlag() |
||
553 | |||
554 | /** |
||
555 | * @param string $siteId |
||
556 | * @param integer $expectedCount |
||
557 | * |
||
558 | * @dataProvider provideFindPublishedByType |
||
559 | */ |
||
560 | public function testFindPublishedByType($siteId, $expectedCount) |
||
564 | |||
565 | /** |
||
566 | * @return array |
||
567 | */ |
||
568 | public function provideFindPublishedByType() |
||
575 | |||
576 | /** |
||
577 | * @param string $path |
||
578 | * @param string $siteId |
||
579 | * @param string $language |
||
580 | * @param integer $expectedCount |
||
581 | * |
||
582 | * @dataProvider provideFindPublishedByPathAndLanguage |
||
583 | */ |
||
584 | public function testFindPublishedByPathAndLanguage($path, $siteId, $language, $expectedCount) |
||
588 | |||
589 | /** |
||
590 | * @return array |
||
591 | */ |
||
592 | View Code Duplication | public function provideFindPublishedByPathAndLanguage() |
|
599 | |||
600 | /** |
||
601 | * @param string $path |
||
602 | * @param string $siteId |
||
603 | * @param string $language |
||
604 | * @param integer $expectedCount |
||
605 | * |
||
606 | * @dataProvider provideFindByIncludedPathSiteIdAndLanguage |
||
607 | */ |
||
608 | public function testFindByIncludedPathSiteIdAndLanguage($path, $siteId, $language, $expectedCount) |
||
612 | |||
613 | /** |
||
614 | * @return array |
||
615 | */ |
||
616 | public function provideFindByIncludedPathSiteIdAndLanguage() |
||
622 | |||
623 | /** |
||
624 | * @param string $theme |
||
625 | * @param integer $expectedCount |
||
626 | * |
||
627 | * @dataProvider provideTheme |
||
628 | */ |
||
629 | public function testFindByTheme($theme, $expectedCount) |
||
633 | |||
634 | /** |
||
635 | * @return array |
||
636 | */ |
||
637 | public function provideTheme() |
||
643 | |||
644 | /** |
||
645 | * test find tree node |
||
646 | */ |
||
647 | public function testFindTreeNode() |
||
674 | |||
675 | /** |
||
676 | * @param string $siteId |
||
677 | * @param string $language |
||
678 | * @param int $count |
||
679 | * |
||
680 | * @dataProvider provideSiteIdAndLanguage |
||
681 | */ |
||
682 | public function testCount($siteId, $language, $count) |
||
686 | |||
687 | /** |
||
688 | * @return array |
||
689 | */ |
||
690 | View Code Duplication | public function provideSiteIdAndLanguage() |
|
699 | |||
700 | /** |
||
701 | * @param PaginateFinderConfiguration $configuration |
||
702 | * @param string $siteId |
||
703 | * @param string $language |
||
704 | * @param int $count |
||
705 | * |
||
706 | * @dataProvider provideCountWithFilterPaginateConfiguration |
||
707 | */ |
||
708 | public function testCountWithFilter($configuration, $siteId, $language, $count) |
||
712 | |||
713 | /** |
||
714 | * @return array |
||
715 | */ |
||
716 | public function provideCountWithFilterPaginateConfiguration() |
||
728 | |||
729 | /** |
||
730 | * @param PaginateFinderConfiguration $configuration |
||
731 | * @param string $siteId |
||
732 | * @param string $language |
||
733 | * @param int $count |
||
734 | * |
||
735 | * @dataProvider provideFindWithFilterPaginateConfiguration |
||
736 | */ |
||
737 | public function testFindForPaginate($configuration, $siteId, $language, $count) |
||
741 | |||
742 | /** |
||
743 | * @return array |
||
744 | */ |
||
745 | public function provideFindWithFilterPaginateConfiguration() |
||
761 | |||
762 | /** |
||
763 | * Test update order of brothers |
||
764 | */ |
||
765 | public function testUpdateOrderOfBrothers() |
||
786 | |||
787 | /** |
||
788 | * Test remove block in area |
||
789 | */ |
||
790 | public function testRemoveBlockInArea() |
||
808 | |||
809 | /** |
||
810 | * @param string $nodeId |
||
811 | * @param string $siteId |
||
812 | * @param string $areaId |
||
813 | * @param int $count |
||
814 | * |
||
815 | * @dataProvider provideFindByNodeIdAndSiteIdWithBlocksInArea |
||
816 | */ |
||
817 | public function testFindByNodeIdAndSiteIdWithBlocksInArea($nodeId, $siteId, $areaId, $count) |
||
822 | |||
823 | /** |
||
824 | * @return array |
||
825 | */ |
||
826 | public function provideFindByNodeIdAndSiteIdWithBlocksInArea() |
||
835 | |||
836 | /** |
||
837 | * Test remove node version |
||
838 | */ |
||
839 | View Code Duplication | public function testRemoveVersion() |
|
852 | |||
853 | /** |
||
854 | * @param string $language |
||
855 | * @param string $siteId |
||
856 | * @param int $count |
||
857 | * |
||
858 | * @dataProvider provideLanguageAndSiteIdSpecialPage |
||
859 | */ |
||
860 | public function testFindAllPublishedSpecialPage($language, $siteId, $count) |
||
865 | |||
866 | /** |
||
867 | * @param string $language |
||
868 | * @param string $siteId |
||
869 | * @param int $count |
||
870 | * |
||
871 | * @dataProvider provideLanguageAndSiteIdSpecialPage |
||
872 | */ |
||
873 | public function testFindAllSpecialPage($language, $siteId, $count) |
||
878 | |||
879 | /** |
||
880 | * @return array |
||
881 | */ |
||
882 | public function provideLanguageAndSiteIdSpecialPage() |
||
893 | |||
894 | /** |
||
895 | * @param string $nodeId |
||
896 | * @param string $language |
||
897 | * @param string $siteId |
||
898 | * @param string $name |
||
899 | * @param int $count |
||
900 | * |
||
901 | * @dataProvider provideCountOtherNodeWithSameSpecialPageName |
||
902 | */ |
||
903 | public function testCountOtherNodeWithSameSpecialPageName($nodeId, $language, $siteId, $name, $count) |
||
908 | |||
909 | /** |
||
910 | * @return array |
||
911 | */ |
||
912 | public function provideCountOtherNodeWithSameSpecialPageName() |
||
926 | |||
927 | /** |
||
928 | * Test update use Reference |
||
929 | */ |
||
930 | public function testUpdateUseReference() |
||
942 | } |
||
943 |
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.