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

ParentLocationTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 132
Duplicated Lines 25 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 33
loc 132
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testMatchLocation() 0 5 1
A matchLocationProvider() 0 25 1
A testMatchContentInfo() 9 9 1
A matchContentInfoProvider() 0 25 1
A generateRepositoryMockForParentLocationId() 24 24 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 ParentLocationTest 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\Id;
10
11
use eZ\Publish\API\Repository\LocationService;
12
use eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Id\ParentLocation as ParentLocationIdMatcher;
13
use eZ\Publish\API\Repository\Values\Content\Location;
14
use eZ\Publish\Core\MVC\Symfony\Matcher\Tests\ContentBased\BaseTest;
15
use eZ\Publish\API\Repository\Repository;
16
17
class ParentLocationTest extends BaseTest
18
{
19
    /**
20
     * @var \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Id\ParentLocation
21
     */
22
    private $matcher;
23
24
    protected function setUp()
25
    {
26
        parent::setUp();
27
        $this->matcher = new ParentLocationIdMatcher();
28
    }
29
30
    /**
31
     * @dataProvider matchLocationProvider
32
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Id\ParentLocation::matchLocation
33
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\MultipleValued::setMatchingConfig
34
     *
35
     * @param int|int[] $matchingConfig
36
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
37
     * @param bool $expectedResult
38
     */
39
    public function testMatchLocation($matchingConfig, Location $location, $expectedResult)
40
    {
41
        $this->matcher->setMatchingConfig($matchingConfig);
42
        $this->assertSame($expectedResult, $this->matcher->matchLocation($location));
43
    }
44
45
    public function matchLocationProvider()
46
    {
47
        return [
48
            [
49
                123,
50
                $this->getLocationMock(['parentLocationId' => 123]),
51
                true,
52
            ],
53
            [
54
                123,
55
                $this->getLocationMock(['parentLocationId' => 456]),
56
                false,
57
            ],
58
            [
59
                [123, 789],
60
                $this->getLocationMock(['parentLocationId' => 456]),
61
                false,
62
            ],
63
            [
64
                [123, 789],
65
                $this->getLocationMock(['parentLocationId' => 789]),
66
                true,
67
            ],
68
        ];
69
    }
70
71
    /**
72
     * @dataProvider matchContentInfoProvider
73
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Id\ParentLocation::matchContentInfo
74
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\MultipleValued::setMatchingConfig
75
     * @covers \eZ\Publish\Core\MVC\RepositoryAware::setRepository
76
     *
77
     * @param int|int[] $matchingConfig
78
     * @param \eZ\Publish\API\Repository\Repository $repository
79
     * @param bool $expectedResult
80
     */
81 View Code Duplication
    public function testMatchContentInfo($matchingConfig, Repository $repository, $expectedResult)
82
    {
83
        $this->matcher->setRepository($repository);
84
        $this->matcher->setMatchingConfig($matchingConfig);
85
        $this->assertSame(
86
            $expectedResult,
87
            $this->matcher->matchContentInfo($this->getContentInfoMock(['mainLocationId' => 42]))
88
        );
89
    }
90
91
    public function matchContentInfoProvider()
92
    {
93
        return [
94
            [
95
                123,
96
                $this->generateRepositoryMockForParentLocationId(123),
97
                true,
98
            ],
99
            [
100
                123,
101
                $this->generateRepositoryMockForParentLocationId(456),
102
                false,
103
            ],
104
            [
105
                [123, 789],
106
                $this->generateRepositoryMockForParentLocationId(456),
107
                false,
108
            ],
109
            [
110
                [123, 789],
111
                $this->generateRepositoryMockForParentLocationId(789),
112
                true,
113
            ],
114
        ];
115
    }
116
117
    /**
118
     * Returns a Repository mock configured to return the appropriate Location object with given parent location Id.
119
     *
120
     * @param int $parentLocationId
121
     *
122
     * @return \PHPUnit_Framework_MockObject_MockObject
123
     */
124 View Code Duplication
    private function generateRepositoryMockForParentLocationId($parentLocationId)
125
    {
126
        $locationServiceMock = $this->createMock(LocationService::class);
127
        $locationServiceMock->expects($this->once())
128
            ->method('loadLocation')
129
            ->with(42)
130
            ->will(
131
                $this->returnValue(
132
                    $this->getLocationMock(['parentLocationId' => $parentLocationId])
133
                )
134
            );
135
136
        $repository = $this->getRepositoryMock();
137
        $repository
138
            ->expects($this->once())
139
            ->method('getLocationService')
140
            ->will($this->returnValue($locationServiceMock));
141
        $repository
142
            ->expects($this->any())
143
            ->method('getPermissionResolver')
144
            ->will($this->returnValue($this->getPermissionResolverMock()));
145
146
        return $repository;
147
    }
148
}
149