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 |
||
17 | class SectionTest extends BaseTest |
||
18 | { |
||
19 | /** @var \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Identifier\Section */ |
||
20 | private $matcher; |
||
21 | |||
22 | protected function setUp(): void |
||
23 | { |
||
24 | parent::setUp(); |
||
25 | $this->matcher = new SectionIdentifierMatcher(); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Returns a Repository mock configured to return the appropriate Section object with given section identifier. |
||
30 | * |
||
31 | * @param string $sectionIdentifier |
||
32 | * |
||
33 | * @return \PHPUnit\Framework\MockObject\MockObject |
||
34 | */ |
||
35 | View Code Duplication | private function generateRepositoryMockForSectionIdentifier($sectionIdentifier) |
|
36 | { |
||
37 | $sectionServiceMock = $this->createMock(SectionService::class); |
||
38 | $sectionServiceMock->expects($this->once()) |
||
39 | ->method('loadSection') |
||
40 | ->will( |
||
41 | $this->returnValue( |
||
42 | $this |
||
43 | ->getMockBuilder(Section::class) |
||
44 | ->setConstructorArgs( |
||
45 | [ |
||
46 | ['identifier' => $sectionIdentifier], |
||
47 | ] |
||
48 | ) |
||
49 | ->getMockForAbstractClass() |
||
50 | ) |
||
51 | ); |
||
52 | |||
53 | $repository = $this->getRepositoryMock(); |
||
54 | $repository |
||
55 | ->expects($this->once()) |
||
56 | ->method('getSectionService') |
||
57 | ->will($this->returnValue($sectionServiceMock)); |
||
58 | $repository |
||
59 | ->expects($this->any()) |
||
60 | ->method('getPermissionResolver') |
||
61 | ->will($this->returnValue($this->getPermissionResolverMock())); |
||
62 | |||
63 | return $repository; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @dataProvider matchSectionProvider |
||
68 | * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Identifier\Section::matchLocation |
||
69 | * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\MultipleValued::setMatchingConfig |
||
70 | * @covers \eZ\Publish\Core\MVC\RepositoryAware::setRepository |
||
71 | * |
||
72 | * @param string|string[] $matchingConfig |
||
73 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
74 | * @param bool $expectedResult |
||
75 | */ |
||
76 | public function testMatchLocation($matchingConfig, Repository $repository, $expectedResult) |
||
77 | { |
||
78 | $this->matcher->setRepository($repository); |
||
79 | $this->matcher->setMatchingConfig($matchingConfig); |
||
80 | |||
81 | $location = $this->getLocationMock(); |
||
82 | $location |
||
83 | ->expects($this->once()) |
||
84 | ->method('getContentInfo') |
||
85 | ->will( |
||
86 | $this->returnValue( |
||
87 | $this->getContentInfoMock(['sectionId' => 1]) |
||
88 | ) |
||
89 | ); |
||
90 | |||
91 | $this->assertSame( |
||
92 | $expectedResult, |
||
93 | $this->matcher->matchLocation($location) |
||
94 | ); |
||
95 | } |
||
96 | |||
97 | public function matchSectionProvider() |
||
122 | |||
123 | /** |
||
124 | * @dataProvider matchSectionProvider |
||
125 | * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Identifier\Section::matchContentInfo |
||
126 | * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\MultipleValued::setMatchingConfig |
||
127 | * @covers \eZ\Publish\Core\MVC\RepositoryAware::setRepository |
||
128 | * |
||
129 | * @param string|string[] $matchingConfig |
||
130 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
131 | * @param bool $expectedResult |
||
132 | */ |
||
133 | View Code Duplication | public function testMatchContentInfo($matchingConfig, Repository $repository, $expectedResult) |
|
142 | } |
||
143 |