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

ParentLocationTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 130
Duplicated Lines 25.38 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

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

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