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

RouterHostRegexTest::createRouter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 22
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\RouterHostRegexTest 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 PHPUnit\Framework\TestCase;
12
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Router;
13
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
14
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\Regex\Host as HostRegexMatcher;
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 RouterHostRegexTest extends TestCase
20
{
21
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder */
22
    private $matcherBuilder;
23
24
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface */
25
    private $siteAccessProvider;
26
27 View Code Duplication
    protected function setUp(): void
28
    {
29
        parent::setUp();
30
        $this->matcherBuilder = new MatcherBuilder();
31
        $this->siteAccessProvider = $this->createMock(SiteAccess\SiteAccessProviderInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...oviderInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\M...ccessProviderInterface> of property $siteAccessProvider.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
        $this->siteAccessProvider
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33
            ->method('isDefined')
34
            ->willReturnMap([
35
                ['first_sa', true],
36
                ['second_sa', true],
37
                ['third_sa', true],
38
                ['fourth_sa', true],
39
                ['fifth_sa', true],
40
                ['example', true],
41
            ]);
42
        $this->siteAccessProvider
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
            ->method('getSiteAccess')
44
            ->willReturnMap([
45
                ['first_sa', new SiteAccess('first_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
46
                ['second_sa', new SiteAccess('second_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
47
                ['third_sa', new SiteAccess('third_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
48
                ['fourth_sa', new SiteAccess('fourth_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
49
                ['fifth_sa', new SiteAccess('fifth_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
50
                ['example', new SiteAccess('example', Router::DEFAULT_SA_MATCHING_TYPE)],
51
            ]);
52
    }
53
54
    public function testConstruct()
55
    {
56
        return $this->createRouter();
57
    }
58
59
    /**
60
     * @dataProvider matchProvider
61
     */
62 View Code Duplication
    public function testMatch(SimplifiedRequest $request, string $siteAccess)
63
    {
64
        $router = $this->createRouter();
65
        $sa = $router->match($request);
66
        $this->assertInstanceOf(SiteAccess::class, $sa);
67
        $this->assertSame($siteAccess, $sa->name);
68
        $router->setSiteAccess();
69
    }
70
71 View Code Duplication
    public function matchProvider()
72
    {
73
        return [
74
            [SimplifiedRequest::fromUrl('http://example.com'), 'default_sa'],
75
            [SimplifiedRequest::fromUrl('https://example.com'), 'default_sa'],
76
            [SimplifiedRequest::fromUrl('http://example.com/'), 'default_sa'],
77
            [SimplifiedRequest::fromUrl('https://example.com/'), 'default_sa'],
78
            [SimplifiedRequest::fromUrl('http://example.com//'), 'default_sa'],
79
            [SimplifiedRequest::fromUrl('https://example.com//'), 'default_sa'],
80
            [SimplifiedRequest::fromUrl('http://example.com:8080/'), 'default_sa'],
81
            [SimplifiedRequest::fromUrl('http://example.com/first_siteaccess/'), 'default_sa'],
82
            [SimplifiedRequest::fromUrl('http://example.com/?first_siteaccess'), 'default_sa'],
83
            [SimplifiedRequest::fromUrl('http://example.com/?first_sa'), 'default_sa'],
84
            [SimplifiedRequest::fromUrl('http://example.com/first_salt'), 'default_sa'],
85
            [SimplifiedRequest::fromUrl('http://example.com/first_sa.foo'), 'default_sa'],
86
            [SimplifiedRequest::fromUrl('http://example.com/test'), 'default_sa'],
87
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/'), 'default_sa'],
88
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/'), 'default_sa'],
89
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/first_sa'), 'default_sa'],
90
            [SimplifiedRequest::fromUrl('http://example.com/default_sa'), 'default_sa'],
91
92
            [SimplifiedRequest::fromUrl('http://example.com/first_sa'), 'first_sa'],
93
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/'), 'first_sa'],
94
            // Double slashes shouldn't be considered as one
95
            [SimplifiedRequest::fromUrl('http://example.com//first_sa//'), 'default_sa'],
96
            [SimplifiedRequest::fromUrl('http://example.com///first_sa///test'), 'default_sa'],
97
            [SimplifiedRequest::fromUrl('http://example.com//first_sa//foo/bar'), 'default_sa'],
98
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/foo'), 'first_sa'],
99
            [SimplifiedRequest::fromUrl('http://example.com:82/first_sa/'), 'first_sa'],
100
            [SimplifiedRequest::fromUrl('http://third_siteaccess/first_sa/'), 'first_sa'],
101
            [SimplifiedRequest::fromUrl('http://first_sa/'), 'first_sa'],
102
            [SimplifiedRequest::fromUrl('https://first_sa/'), 'first_sa'],
103
            [SimplifiedRequest::fromUrl('http://first_sa:81/'), 'first_sa'],
104
            [SimplifiedRequest::fromUrl('http://first_sa/'), 'first_sa'],
105
            [SimplifiedRequest::fromUrl('http://first_sa:82/'), 'first_sa'],
106
            [SimplifiedRequest::fromUrl('http://first_sa:83/'), 'first_sa'],
107
            [SimplifiedRequest::fromUrl('http://first_sa/foo/'), 'first_sa'],
108
            [SimplifiedRequest::fromUrl('http://first_sa:82/foo/'), 'first_sa'],
109
            [SimplifiedRequest::fromUrl('http://first_sa:83/foo/'), 'first_sa'],
110
            [SimplifiedRequest::fromUrl('http://first_sa/foobar/'), 'first_sa'],
111
            [SimplifiedRequest::fromUrl('http://second_sa:82/'), 'second_sa'],
112
            [SimplifiedRequest::fromUrl('http://second_sa:83/'), 'second_sa'],
113
            [SimplifiedRequest::fromUrl('http://second_sa/foo/'), 'second_sa'],
114
            [SimplifiedRequest::fromUrl('http://second_sa:82/foo/'), 'second_sa'],
115
            [SimplifiedRequest::fromUrl('http://second_sa:83/foo/'), 'second_sa'],
116
            [SimplifiedRequest::fromUrl('http://second_sa/foobar/'), 'second_sa'],
117
118
            [SimplifiedRequest::fromUrl('http://example.com/second_sa'), 'second_sa'],
119
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/'), 'second_sa'],
120
            [SimplifiedRequest::fromUrl('http://example.com/second_sa?param1=foo'), 'second_sa'],
121
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/foo/'), 'second_sa'],
122
            [SimplifiedRequest::fromUrl('http://example.com:82/second_sa/'), 'second_sa'],
123
            [SimplifiedRequest::fromUrl('http://example.com:83/second_sa/'), 'second_sa'],
124
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/second_sa/'), 'second_sa'],
125
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/second_sa/'), 'second_sa'],
126
        ];
127
    }
128
129
    public function testGetName()
130
    {
131
        $matcher = new HostRegexMatcher(['host' => 'foo'], []);
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\MVC\Symf...cess\Matcher\Regex\Host 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...
132
        $this->assertSame('host:regexp', $matcher->getName());
133
    }
134
135
    /**
136
     * @return \eZ\Publish\Core\MVC\Symfony\SiteAccess\Router
137
     */
138 View Code Duplication
    private function createRouter(): Router
139
    {
140
        return new Router(
141
            $this->matcherBuilder,
142
            $this->createMock(LoggerInterface::class),
143
            'default_sa',
144
            [
145
                'Regex\\Host' => [
146
                    'regex' => '^(\\w+_sa)$',
147
                ],
148
                'Map\\URI' => [
149
                    'first_sa' => 'first_sa',
150
                    'second_sa' => 'second_sa',
151
                ],
152
                'Map\\Host' => [
153
                    'first_sa' => 'first_sa',
154
                    'first_siteaccess' => 'first_sa',
155
                ],
156
            ],
157
            $this->siteAccessProvider
158
        );
159
    }
160
}
161