Completed
Push — EZP-31287 ( fe8c5c...af9523 )
by
unknown
34:01
created

UrlAliasTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 145
Duplicated Lines 13.1 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 19
loc 145
rs 10
c 0
b 0
f 0
wmc 7
lcom 2
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testSetMatchingConfig() 0 8 1
A generateRepositoryMockForUrlAlias() 0 36 1
A testMatchLocation() 9 9 1
A matchLocationProvider() 0 30 1
A testMatchContentInfo() 0 5 1
A setMatchingConfigProvider() 0 10 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;
8
9
use eZ\Publish\API\Repository\URLAliasService;
10
use eZ\Publish\API\Repository\Values\Content\Location;
11
use eZ\Publish\API\Repository\Values\Content\URLAlias;
12
use eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\UrlAlias as UrlAliasMatcher;
13
use eZ\Publish\API\Repository\Repository;
14
15
class UrlAliasTest extends BaseTest
16
{
17
    /** @var \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\UrlAlias */
18
    private $matcher;
19
20
    protected function setUp()
21
    {
22
        parent::setUp();
23
        $this->matcher = new UrlAliasMatcher();
24
    }
25
26
    /**
27
     * @dataProvider setMatchingConfigProvider
28
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\UrlAlias::setMatchingConfig
29
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\MultipleValued::setMatchingConfig
30
     *
31
     * @param string $matchingConfig
32
     * @param string[] $expectedValues
33
     */
34
    public function testSetMatchingConfig($matchingConfig, $expectedValues)
35
    {
36
        $this->matcher->setMatchingConfig($matchingConfig);
37
        $this->assertSame(
38
            $this->matcher->getValues(),
39
            $expectedValues
40
        );
41
    }
42
43
    public function setMatchingConfigProvider()
44
    {
45
        return [
46
            ['/foo/bar/', ['foo/bar']],
47
            ['/foo/bar/', ['foo/bar']],
48
            ['/foo/bar', ['foo/bar']],
49
            [['/foo/bar/', 'baz/biz/'], ['foo/bar', 'baz/biz']],
50
            [['foo/bar', 'baz/biz'], ['foo/bar', 'baz/biz']],
51
        ];
52
    }
53
54
    /**
55
     * Returns a Repository mock configured to return the appropriate Section object with given section identifier.
56
     *
57
     * @param string $path
58
     *
59
     * @return \PHPUnit\Framework\MockObject\MockObject
60
     */
61
    private function generateRepositoryMockForUrlAlias($path)
62
    {
63
        // First an url alias that will never match, then the right url alias.
64
        // This ensures to test even if the location has several url aliases.
65
        $urlAliasList = [
66
            $this->createMock(URLAlias::class),
67
            $this
68
                ->getMockBuilder(URLAlias::class)
69
                ->setConstructorArgs([['path' => $path]])
70
                ->getMockForAbstractClass(),
71
        ];
72
73
        $urlAliasServiceMock = $this->createMock(URLAliasService::class);
74
        $urlAliasServiceMock->expects($this->at(0))
75
            ->method('listLocationAliases')
76
            ->with(
77
                $this->isInstanceOf(Location::class),
78
                true
79
            )
80
            ->will($this->returnValue([]));
81
        $urlAliasServiceMock->expects($this->at(1))
82
            ->method('listLocationAliases')
83
            ->with(
84
                $this->isInstanceOf(Location::class),
85
                false
86
            )
87
            ->will($this->returnValue($urlAliasList));
88
89
        $repository = $this->getRepositoryMock();
90
        $repository
91
            ->expects($this->once())
92
            ->method('getURLAliasService')
93
            ->will($this->returnValue($urlAliasServiceMock));
94
95
        return $repository;
96
    }
97
98
    /**
99
     * @dataProvider matchLocationProvider
100
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\UrlAlias::matchLocation
101
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\UrlAlias::setMatchingConfig
102
     * @covers \eZ\Publish\Core\MVC\RepositoryAware::setRepository
103
     *
104
     * @param string|string[] $matchingConfig
105
     * @param \eZ\Publish\API\Repository\Repository $repository
106
     * @param bool $expectedResult
107
     */
108 View Code Duplication
    public function testMatchLocation($matchingConfig, Repository $repository, $expectedResult)
109
    {
110
        $this->matcher->setRepository($repository);
111
        $this->matcher->setMatchingConfig($matchingConfig);
112
        $this->assertSame(
113
            $expectedResult,
114
            $this->matcher->matchLocation($this->getLocationMock())
115
        );
116
    }
117
118
    public function matchLocationProvider()
119
    {
120
        return [
121
            [
122
                'foo/url',
123
                $this->generateRepositoryMockForUrlAlias('/foo/url'),
124
                true,
125
            ],
126
            [
127
                '/foo/url',
128
                $this->generateRepositoryMockForUrlAlias('/foo/url'),
129
                true,
130
            ],
131
            [
132
                'foo/url',
133
                $this->generateRepositoryMockForUrlAlias('/bar/url'),
134
                false,
135
            ],
136
            [
137
                ['foo/url', 'baz'],
138
                $this->generateRepositoryMockForUrlAlias('/bar/url'),
139
                false,
140
            ],
141
            [
142
                ['foo/url   ', 'baz   '],
143
                $this->generateRepositoryMockForUrlAlias('/baz'),
144
                true,
145
            ],
146
        ];
147
    }
148
149
    /**
150
     * @expectedException \RuntimeException
151
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\UrlAlias::matchContentInfo
152
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\UrlAlias::setMatchingConfig
153
     */
154
    public function testMatchContentInfo()
155
    {
156
        $this->matcher->setMatchingConfig('foo/bar');
157
        $this->matcher->matchContentInfo($this->getContentInfoMock());
158
    }
159
}
160