Completed
Push — EZP-31044-site-access-provider ( 8220ce )
by
unknown
14:19
created

RouterURIRegexTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 122
Duplicated Lines 87.7 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 107
loc 122
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
B matchProvider() 57 57 1
A testGetName() 0 5 1
A testSerialize() 17 17 1
A createRouter() 22 22 1
A getSiteAccessProviderSettings() 11 11 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 eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\RouterURIRegexTest 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\SiteAccess\Tests;
10
11
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
12
use PHPUnit\Framework\TestCase;
13
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Router;
14
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\Regex\URI as RegexMatcher;
15
use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest;
16
use eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder;
17
use Psr\Log\LoggerInterface;
18
19
class RouterURIRegexTest extends RouterBaseTest
20
{
21 View Code Duplication
    public function matchProvider(): array
22
    {
23
        return [
24
            [SimplifiedRequest::fromUrl('http://example.com'), 'default_sa'],
25
            [SimplifiedRequest::fromUrl('https://example.com'), 'default_sa'],
26
            [SimplifiedRequest::fromUrl('http://example.com/'), 'default_sa'],
27
            [SimplifiedRequest::fromUrl('https://example.com/'), 'default_sa'],
28
            [SimplifiedRequest::fromUrl('http://example.com//'), 'default_sa'],
29
            [SimplifiedRequest::fromUrl('https://example.com//'), 'default_sa'],
30
            [SimplifiedRequest::fromUrl('http://example.com:8080/'), 'default_sa'],
31
            [SimplifiedRequest::fromUrl('http://example.com/first_siteaccess/'), 'default_sa'],
32
            [SimplifiedRequest::fromUrl('http://example.com/?first_siteaccess'), 'default_sa'],
33
            [SimplifiedRequest::fromUrl('http://example.com/?first_sa'), 'default_sa'],
34
            [SimplifiedRequest::fromUrl('http://example.com/first_salt'), 'default_sa'],
35
            [SimplifiedRequest::fromUrl('http://example.com/first_sa.foo'), 'default_sa'],
36
            [SimplifiedRequest::fromUrl('http://example.com/test'), 'default_sa'],
37
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/'), 'default_sa'],
38
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/'), 'default_sa'],
39
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/first_sa'), 'default_sa'],
40
            [SimplifiedRequest::fromUrl('http://example.com/default_sa'), 'default_sa'],
41
42
            [SimplifiedRequest::fromUrl('http://example.com/first_sa'), 'first_sa'],
43
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/'), 'first_sa'],
44
            // Double slashes shouldn't be considered as one
45
            [SimplifiedRequest::fromUrl('http://example.com//first_sa//'), 'default_sa'],
46
            [SimplifiedRequest::fromUrl('http://example.com///first_sa///test'), 'default_sa'],
47
            [SimplifiedRequest::fromUrl('http://example.com//first_sa//foo/bar'), 'default_sa'],
48
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/foo'), 'first_sa'],
49
            [SimplifiedRequest::fromUrl('http://example.com:82/first_sa/'), 'first_sa'],
50
            [SimplifiedRequest::fromUrl('http://third_siteaccess/first_sa/'), 'first_sa'],
51
            [SimplifiedRequest::fromUrl('http://first_sa/'), 'first_sa'],
52
            [SimplifiedRequest::fromUrl('https://first_sa/'), 'first_sa'],
53
            [SimplifiedRequest::fromUrl('http://first_sa:81/'), 'first_sa'],
54
            [SimplifiedRequest::fromUrl('http://first_siteaccess/'), 'first_sa'],
55
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/'), 'first_sa'],
56
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/'), 'first_sa'],
57
            [SimplifiedRequest::fromUrl('http://first_siteaccess/foo/'), 'first_sa'],
58
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/foo/'), 'first_sa'],
59
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/foo/'), 'first_sa'],
60
            [SimplifiedRequest::fromUrl('http://first_siteaccess/foobar/'), 'first_sa'],
61
            [SimplifiedRequest::fromUrl('http://first_siteaccess//foobar/'), 'first_sa'],
62
            [SimplifiedRequest::fromUrl('http://first_siteaccess//footestbar/'), 'first_sa'],
63
            [SimplifiedRequest::fromUrl('http://first_siteaccess/footestbar/'), 'test'],
64
            [SimplifiedRequest::fromUrl('http://first_siteaccess/footestbar/foobazbar/'), 'test'],
65
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/footestbar/'), 'test'],
66
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/footestbar/'), 'test'],
67
68
            [SimplifiedRequest::fromUrl('http://example.com/second_sa'), 'second_sa'],
69
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/'), 'second_sa'],
70
            [SimplifiedRequest::fromUrl('http://example.com/second_sa?param1=foo'), 'second_sa'],
71
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/foo/'), 'second_sa'],
72
            [SimplifiedRequest::fromUrl('http://example.com:82/second_sa/'), 'second_sa'],
73
            [SimplifiedRequest::fromUrl('http://example.com:83/second_sa/'), 'second_sa'],
74
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/second_sa/'), 'second_sa'],
75
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/second_sa/'), 'second_sa'],
76
        ];
77
    }
78
79
    public function testGetName()
80
    {
81
        $matcher = new RegexMatcher([], []);
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\MVC\Symf...ccess\Matcher\Regex\URI has been deprecated with message: since 5.3 as it cannot be reverted.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
82
        $this->assertSame('uri:regexp', $matcher->getName());
83
    }
84
85 View Code Duplication
    public function testSerialize()
86
    {
87
        $matcher = new RegexMatcher(
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\MVC\Symf...ccess\Matcher\Regex\URI has been deprecated with message: since 5.3 as it cannot be reverted.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
88
            [
89
                'regex' => '^/foo(\\w+)bar',
90
                'itemNumber' => 2,
91
            ]
92
        );
93
        $matcher->setRequest(new SimplifiedRequest(['pathinfo' => '/foo/bar']));
94
        $sa = new SiteAccess('test', 'test', $matcher);
95
        $serializedSA1 = serialize($sa);
96
97
        $matcher->setRequest(new SimplifiedRequest(['pathinfo' => '/foo/bar/baz']));
98
        $serializedSA2 = serialize($sa);
99
100
        $this->assertSame($serializedSA1, $serializedSA2);
101
    }
102
103 View Code Duplication
    protected function createRouter(): Router
104
    {
105
        return new Router(
106
            $this->matcherBuilder,
107
            $this->createMock(LoggerInterface::class),
108
            'default_sa',
109
            [
110
                'Regex\\URI' => [
111
                    'regex' => '^/foo(\\w+)bar',
112
                ],
113
                'Map\\URI' => [
114
                    'first_sa' => 'first_sa',
115
                    'second_sa' => 'second_sa',
116
                ],
117
                'Map\\Host' => [
118
                    'first_sa' => 'first_sa',
119
                    'first_siteaccess' => 'first_sa',
120
                ],
121
            ],
122
            $this->siteAccessProvider
123
        );
124
    }
125
126
    /**
127
     * @return \eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\SiteAccessSetting[]
128
     */
129 View Code Duplication
    public function getSiteAccessProviderSettings(): array
130
    {
131
        return [
132
            new SiteAccessSetting('first_sa', true),
133
            new SiteAccessSetting('second_sa', true),
134
            new SiteAccessSetting('third_sa', true),
135
            new SiteAccessSetting('fourth_sa', true),
136
            new SiteAccessSetting('fifth_sa', true),
137
            new SiteAccessSetting('test', true),
138
        ];
139
    }
140
}
141