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

RouterSpecialPortsTest::createRouter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 28
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 28
loc 28
rs 9.472
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\RouterSpecialPortsTest 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\Map\Port as PortMatcher;
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 RouterSpecialPortsTest 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
            ]);
41
        $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...
42
            ->method('getSiteAccess')
43
            ->willReturnMap([
44
                ['first_sa', new SiteAccess('first_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
45
                ['second_sa', new SiteAccess('second_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
46
                ['third_sa', new SiteAccess('third_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
47
                ['fourth_sa', new SiteAccess('fourth_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
48
                ['fifth_sa', new SiteAccess('fifth_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
49
            ]);
50
    }
51
52
    public function testConstruct()
53
    {
54
        return $this->createRouter();
55
    }
56
57
    /**
58
     * @dataProvider matchProvider
59
     */
60 View Code Duplication
    public function testMatch(SimplifiedRequest $request, string $siteAccess)
61
    {
62
        $router = $this->createRouter();
63
        $sa = $router->match($request);
64
        $this->assertInstanceOf(SiteAccess::class, $sa);
65
        $this->assertSame($siteAccess, $sa->name);
66
        $router->setSiteAccess();
67
    }
68
69
    public function matchProvider()
70
    {
71
        return [
72
            [SimplifiedRequest::fromUrl('http://example.com'), 'fifth_sa'],
73
            [SimplifiedRequest::fromUrl('https://example.com'), 'fourth_sa'],
74
            [SimplifiedRequest::fromUrl('http://example.com/'), 'fifth_sa'],
75
            [SimplifiedRequest::fromUrl('https://example.com/'), 'fourth_sa'],
76
            [SimplifiedRequest::fromUrl('http://example.com//'), 'fifth_sa'],
77
            [SimplifiedRequest::fromUrl('https://example.com//'), 'fourth_sa'],
78
            [SimplifiedRequest::fromUrl('http://example.com:8080/'), 'default_sa'],
79
            [SimplifiedRequest::fromUrl('http://example.com/first_siteaccess/'), 'fifth_sa'],
80
            [SimplifiedRequest::fromUrl('http://example.com/?first_siteaccess'), 'fifth_sa'],
81
            [SimplifiedRequest::fromUrl('http://example.com/?first_sa'), 'fifth_sa'],
82
            [SimplifiedRequest::fromUrl('http://example.com/first_salt'), 'fifth_sa'],
83
            [SimplifiedRequest::fromUrl('http://example.com/first_sa.foo'), 'fifth_sa'],
84
            [SimplifiedRequest::fromUrl('http://example.com/test'), 'fifth_sa'],
85
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/'), 'fifth_sa'],
86
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/'), 'fifth_sa'],
87
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/first_sa'), 'fifth_sa'],
88
            [SimplifiedRequest::fromUrl('http://example.com/default_sa'), 'fifth_sa'],
89
90
            [SimplifiedRequest::fromUrl('http://example.com/first_sa'), 'first_sa'],
91
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/'), 'first_sa'],
92
            // Shouldn't match "first_sa" because of double slash
93
            [SimplifiedRequest::fromUrl('http://example.com//first_sa/'), 'fifth_sa'],
94
            [SimplifiedRequest::fromUrl('http://example.com/first_sa//'), 'first_sa'],
95
            [SimplifiedRequest::fromUrl('http://example.com/first_sa///test'), 'first_sa'],
96
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/foo'), 'first_sa'],
97
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/foo/bar'), 'first_sa'],
98
            [SimplifiedRequest::fromUrl('http://example.com:82/first_sa/'), 'first_sa'],
99
            [SimplifiedRequest::fromUrl('http://third_siteaccess/first_sa/'), 'first_sa'],
100
            [SimplifiedRequest::fromUrl('http://first_sa/'), 'first_sa'],
101
            [SimplifiedRequest::fromUrl('https://first_sa/'), 'first_sa'],
102
            [SimplifiedRequest::fromUrl('http://first_sa:81/'), 'first_sa'],
103
            [SimplifiedRequest::fromUrl('http://first_siteaccess/'), 'first_sa'],
104
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/'), 'first_sa'],
105
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/'), 'first_sa'],
106
            [SimplifiedRequest::fromUrl('http://first_siteaccess/foo/'), 'first_sa'],
107
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/foo/'), 'first_sa'],
108
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/foo/'), 'first_sa'],
109
110
            [SimplifiedRequest::fromUrl('http://example.com/second_sa'), 'second_sa'],
111
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/'), 'second_sa'],
112
            [SimplifiedRequest::fromUrl('http://example.com/second_sa?param1=foo'), 'second_sa'],
113
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/foo/'), 'second_sa'],
114
            [SimplifiedRequest::fromUrl('http://example.com:82/second_sa/'), 'second_sa'],
115
            [SimplifiedRequest::fromUrl('http://example.com:83/second_sa/'), 'second_sa'],
116
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/second_sa/'), 'second_sa'],
117
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/second_sa/'), 'second_sa'],
118
119
            [SimplifiedRequest::fromUrl('http://example.com:81/'), 'third_sa'],
120
            [SimplifiedRequest::fromUrl('https://example.com:81/'), 'third_sa'],
121
            [SimplifiedRequest::fromUrl('http://example.com:81/foo'), 'third_sa'],
122
            [SimplifiedRequest::fromUrl('http://example.com:81/foo/bar'), 'third_sa'],
123
124
            [SimplifiedRequest::fromUrl('http://example.com:82/'), 'fourth_sa'],
125
            [SimplifiedRequest::fromUrl('https://example.com:82/'), 'fourth_sa'],
126
            [SimplifiedRequest::fromUrl('https://example.com:82/foo'), 'fourth_sa'],
127
        ];
128
    }
129
130
    public function testGetName()
131
    {
132
        $matcher = new PortMatcher(['port' => '8080', 'scheme' => 'http'], []);
133
        $this->assertSame('port', $matcher->getName());
134
    }
135
136 View Code Duplication
    private function createRouter(): Router
137
    {
138
        return new Router(
139
            $this->matcherBuilder,
140
            $this->createMock(LoggerInterface::class),
141
            'default_sa',
142
            [
143
                'Map\\URI' => [
144
                    'first_sa' => 'first_sa',
145
                    'second_sa' => 'second_sa',
146
                ],
147
                'Map\\Host' => [
148
                    'first_sa' => 'first_sa',
149
                    'first_siteaccess' => 'first_sa',
150
                    'third_siteaccess' => 'third_sa',
151
                ],
152
                'Map\\Port' => [
153
                    80 => 'fifth_sa',
154
                    81 => 'third_sa',
155
                    82 => 'fourth_sa',
156
                    83 => 'first_sa',
157
                    85 => 'first_sa',
158
                    443 => 'fourth_sa',
159
                ],
160
            ],
161
            $this->siteAccessProvider
162
        );
163
    }
164
}
165