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

UrlAliasTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 147
Duplicated Lines 12.93 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

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