Completed
Push — 6.13 ( 9c335d...8c8805 )
by
unknown
16:33
created

SectionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 126
Duplicated Lines 30.95 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A generateRepositoryMockForSectionIdentifier() 30 30 1
A testMatchLocation() 0 18 1
A matchSectionProvider() 0 25 1
A testMatchContentInfo() 9 9 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
 * File containing the SectionTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
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
    /**
20
     * @var \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Identifier\Section
21
     */
22
    private $matcher;
23
24
    protected function setUp()
25
    {
26
        parent::setUp();
27
        $this->matcher = new SectionIdentifierMatcher();
28
    }
29
30
    /**
31
     * Returns a Repository mock configured to return the appropriate Section object with given section identifier.
32
     *
33
     * @param string $sectionIdentifier
34
     *
35
     * @return \PHPUnit_Framework_MockObject_MockObject
36
     */
37 View Code Duplication
    private function generateRepositoryMockForSectionIdentifier($sectionIdentifier)
38
    {
39
        $sectionServiceMock = $this->createMock(SectionService::class);
40
        $sectionServiceMock->expects($this->once())
41
            ->method('loadSection')
42
            ->will(
43
                $this->returnValue(
44
                    $this
45
                        ->getMockBuilder(Section::class)
46
                        ->setConstructorArgs(
47
                            [
48
                                ['identifier' => $sectionIdentifier],
49
                            ]
50
                        )
51
                        ->getMockForAbstractClass()
52
                )
53
            );
54
55
        $repository = $this->getRepositoryMock();
56
        $repository
57
            ->expects($this->once())
58
            ->method('getSectionService')
59
            ->will($this->returnValue($sectionServiceMock));
60
        $repository
61
            ->expects($this->any())
62
            ->method('getPermissionResolver')
63
            ->will($this->returnValue($this->getPermissionResolverMock()));
64
65
        return $repository;
66
    }
67
68
    /**
69
     * @dataProvider matchSectionProvider
70
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Identifier\Section::matchLocation
71
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\MultipleValued::setMatchingConfig
72
     * @covers \eZ\Publish\Core\MVC\RepositoryAware::setRepository
73
     *
74
     * @param string|string[] $matchingConfig
75
     * @param \eZ\Publish\API\Repository\Repository $repository
76
     * @param bool $expectedResult
77
     */
78
    public function testMatchLocation($matchingConfig, Repository $repository, $expectedResult)
79
    {
80
        $this->matcher->setRepository($repository);
81
        $this->matcher->setMatchingConfig($matchingConfig);
82
        $location = $this->getLocationMock();
83
        $location
84
            ->expects($this->once())
85
            ->method('getContentInfo')
86
            ->will(
87
                $this->returnValue(
88
                    $this->getContentInfoMock()
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())
140
        );
141
    }
142
}
143