Completed
Push — master ( ff8f52...ed8581 )
by
unknown
12:31 queued 10s
created

SectionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 126
Duplicated Lines 30.95 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 39
loc 126
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A matchSectionProvider() 0 25 1
A testMatchContentInfo() 9 9 1
A setUp() 0 5 1
A generateRepositoryMockForSectionIdentifier() 30 30 1
A testMatchLocation() 0 20 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\MVC\Symfony\Matcher\Tests\ContentBased\Identifier;
10
11
use eZ\Publish\API\Repository\SectionService;
12
use eZ\Publish\API\Repository\Values\Content\Section;
13
use eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Identifier\Section as SectionIdentifierMatcher;
14
use eZ\Publish\Core\MVC\Symfony\Matcher\Tests\ContentBased\BaseTest;
15
use eZ\Publish\API\Repository\Repository;
16
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()
98
    {
99
        return [
100
            [
101
                'foo',
102
                $this->generateRepositoryMockForSectionIdentifier('foo'),
103
                true,
104
            ],
105
            [
106
                'foo',
107
                $this->generateRepositoryMockForSectionIdentifier('bar'),
108
                false,
109
            ],
110
            [
111
                ['foo', 'baz'],
112
                $this->generateRepositoryMockForSectionIdentifier('bar'),
113
                false,
114
            ],
115
            [
116
                ['foo', 'baz'],
117
                $this->generateRepositoryMockForSectionIdentifier('baz'),
118
                true,
119
            ],
120
        ];
121
    }
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)
134
    {
135
        $this->matcher->setRepository($repository);
136
        $this->matcher->setMatchingConfig($matchingConfig);
137
        $this->assertSame(
138
            $expectedResult,
139
            $this->matcher->matchContentInfo($this->getContentInfoMock(['sectionId' => 1]))
140
        );
141
    }
142
}
143