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

RouterHostPortURITest::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\RouterHostPortURITest 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\Matcher\Map\Host;
12
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\Map\Port;
13
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
14
use PHPUnit\Framework\TestCase;
15
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Router;
16
use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest;
17
use eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder;
18
use Psr\Log\LoggerInterface;
19
20
class RouterHostPortURITest extends TestCase
21
{
22
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder */
23
    private $matcherBuilder;
24
25
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface */
26
    private $siteAccessProvider;
27
28 View Code Duplication
    protected function setUp(): void
29
    {
30
        parent::setUp();
31
        $this->matcherBuilder = new MatcherBuilder();
32
        $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...
33
        $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...
34
            ->method('isDefined')
35
            ->willReturnMap([
36
                ['first_sa', true],
37
                ['second_sa', true],
38
                ['third_sa', true],
39
                ['fourth_sa', true],
40
                ['fifth_sa', 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
            ]);
51
    }
52
53
    public function testConstruct()
54
    {
55
        return $this->createRouter();
56
    }
57
58
    /**
59
     * @dataProvider matchProvider
60
     */
61 View Code Duplication
    public function testMatch(SimplifiedRequest $request, string $siteAccess)
62
    {
63
        $router = $this->createRouter();
64
        $sa = $router->match($request);
65
        $this->assertInstanceOf(SiteAccess::class, $sa);
66
        $this->assertSame($siteAccess, $sa->name);
67
        $router->setSiteAccess();
68
    }
69
70 View Code Duplication
    public function matchProvider()
71
    {
72
        return [
73
            [SimplifiedRequest::fromUrl('http://example.com'), 'fifth_sa'],
74
            [SimplifiedRequest::fromUrl('https://example.com'), 'fourth_sa'],
75
            [SimplifiedRequest::fromUrl('http://example.com/'), 'fifth_sa'],
76
            [SimplifiedRequest::fromUrl('https://example.com/'), 'fourth_sa'],
77
            [SimplifiedRequest::fromUrl('http://example.com//'), 'fifth_sa'],
78
            [SimplifiedRequest::fromUrl('https://example.com//'), 'fourth_sa'],
79
            [SimplifiedRequest::fromUrl('http://example.com:8080/'), 'default_sa'],
80
            [SimplifiedRequest::fromUrl('http://example.com/first_siteaccess/'), 'fifth_sa'],
81
            [SimplifiedRequest::fromUrl('http://example.com/?first_siteaccess'), 'fifth_sa'],
82
            [SimplifiedRequest::fromUrl('http://example.com/?first_sa'), 'fifth_sa'],
83
            [SimplifiedRequest::fromUrl('http://example.com/first_salt'), 'fifth_sa'],
84
            [SimplifiedRequest::fromUrl('http://example.com/first_sa.foo'), 'fifth_sa'],
85
            [SimplifiedRequest::fromUrl('http://example.com/test'), 'fifth_sa'],
86
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/'), 'fifth_sa'],
87
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/'), 'fifth_sa'],
88
            [SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/first_sa'), 'fifth_sa'],
89
            [SimplifiedRequest::fromUrl('http://example.com/default_sa'), 'fifth_sa'],
90
91
            [SimplifiedRequest::fromUrl('http://example.com/first_sa'), 'fifth_sa'],
92
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/'), 'fifth_sa'],
93
            [SimplifiedRequest::fromUrl('http://example.com/first_sa//'), 'fifth_sa'],
94
            [SimplifiedRequest::fromUrl('http://example.com/first_sa///test'), 'fifth_sa'],
95
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/foo'), 'fifth_sa'],
96
            [SimplifiedRequest::fromUrl('http://example.com/first_sa/foo/bar'), 'fifth_sa'],
97
            [SimplifiedRequest::fromUrl('http://example.com:82/first_sa/'), 'fourth_sa'],
98
            [SimplifiedRequest::fromUrl('http://third_siteaccess/first_sa/'), 'third_sa'],
99
            [SimplifiedRequest::fromUrl('http://first_sa/'), 'first_sa'],
100
            [SimplifiedRequest::fromUrl('https://first_sa/'), 'first_sa'],
101
            [SimplifiedRequest::fromUrl('http://first_sa:81/'), 'first_sa'],
102
            [SimplifiedRequest::fromUrl('http://first_siteaccess/'), 'first_sa'],
103
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/'), 'first_sa'],
104
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/'), 'first_sa'],
105
            [SimplifiedRequest::fromUrl('http://first_siteaccess/foo/'), 'first_sa'],
106
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/foo/'), 'first_sa'],
107
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/foo/'), 'first_sa'],
108
109
            [SimplifiedRequest::fromUrl('http://example.com/second_sa'), 'fifth_sa'],
110
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/'), 'fifth_sa'],
111
            [SimplifiedRequest::fromUrl('http://example.com/second_sa?param1=foo'), 'fifth_sa'],
112
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/foo/'), 'fifth_sa'],
113
            [SimplifiedRequest::fromUrl('http://example.com:82/second_sa/'), 'fourth_sa'],
114
            [SimplifiedRequest::fromUrl('http://example.com:83/second_sa/'), 'first_sa'],
115
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/second_sa/'), 'first_sa'],
116
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/second_sa/'), 'first_sa'],
117
118
            [SimplifiedRequest::fromUrl('http://first_sa:123/second_sa'), 'first_sa'],
119
            [SimplifiedRequest::fromUrl('http://first_siteaccess:123/second_sa/'), 'first_sa'],
120
            [SimplifiedRequest::fromUrl('http://example.com:123/second_sa?param1=foo'), 'second_sa'],
121
            [SimplifiedRequest::fromUrl('http://example.com:123/second_sa/foo/'), 'second_sa'],
122
            [SimplifiedRequest::fromUrl('http://example.com:123/second_sa'), 'second_sa'],
123
            [SimplifiedRequest::fromUrl('http://example.com:123/second_sa/'), 'second_sa'],
124
            [SimplifiedRequest::fromUrl('http://example.com:123/second_sa?param1=foo'), 'second_sa'],
125
            [SimplifiedRequest::fromUrl('http://example.com:123/second_sa/foo/'), 'second_sa'],
126
127
            [SimplifiedRequest::fromUrl('http://example.com:81/'), 'third_sa'],
128
            [SimplifiedRequest::fromUrl('https://example.com:81/'), 'third_sa'],
129
            [SimplifiedRequest::fromUrl('http://example.com:81/foo'), 'third_sa'],
130
            [SimplifiedRequest::fromUrl('http://example.com:81/foo/bar'), 'third_sa'],
131
132
            [SimplifiedRequest::fromUrl('http://example.com:82/'), 'fourth_sa'],
133
            [SimplifiedRequest::fromUrl('https://example.com:82/'), 'fourth_sa'],
134
            [SimplifiedRequest::fromUrl('https://example.com:82/foo'), 'fourth_sa'],
135
        ];
136
    }
137
138 View Code Duplication
    public function testSetGetRequestMapHost()
139
    {
140
        $mapKey = 'phoenix-rises.fm';
141
        $request = new SimplifiedRequest(['host' => $mapKey]);
142
        $matcher = new Host(['foo' => $mapKey]);
143
        $matcher->setRequest($request);
144
        $this->assertSame($request, $matcher->getRequest());
145
        $this->assertSame($mapKey, $matcher->getMapKey());
146
    }
147
148
    public function testReverseHostMatchFail()
149
    {
150
        $config = ['foo' => 'bar'];
151
        $matcher = new Host($config);
152
        $this->assertNull($matcher->reverseMatch('non_existent'));
153
    }
154
155 View Code Duplication
    public function testReverseMatchHost()
156
    {
157
        $config = [
158
            'ez.no' => 'some_siteaccess',
159
            'something_else' => 'another_siteaccess',
160
            'phoenix-rises.fm' => 'ezdemo_site',
161
        ];
162
        $request = new SimplifiedRequest(['host' => 'ez.no']);
163
        $matcher = new Host($config);
164
        $matcher->setRequest($request);
165
        $this->assertSame('ez.no', $matcher->getMapKey());
166
167
        $result = $matcher->reverseMatch('ezdemo_site');
168
        $this->assertInstanceOf(Host::class, $result);
169
        $this->assertSame($request, $matcher->getRequest());
170
        $this->assertSame('phoenix-rises.fm', $result->getMapKey());
171
        $this->assertSame('phoenix-rises.fm', $result->getRequest()->host);
172
    }
173
174 View Code Duplication
    public function testSetGetRequestMapPort()
175
    {
176
        $mapKey = '8000';
177
        $request = new SimplifiedRequest(['port' => $mapKey]);
178
        $matcher = new Port(['foo' => $mapKey]);
179
        $matcher->setRequest($request);
180
        $this->assertSame($request, $matcher->getRequest());
181
        $this->assertSame($mapKey, $matcher->getMapKey());
182
    }
183
184
    public function testReversePortMatchFail()
185
    {
186
        $config = ['foo' => '8080'];
187
        $matcher = new Port($config);
188
        $this->assertNull($matcher->reverseMatch('non_existent'));
189
    }
190
191
    public function testReverseMatchPort()
192
    {
193
        $config = [
194
            '80' => 'some_siteaccess',
195
            '443' => 'another_siteaccess',
196
            8000 => 'ezdemo_site',
197
        ];
198
        $request = new SimplifiedRequest(['scheme' => 'http', 'host' => 'ez.no']);
199
        $matcher = new Port($config);
200
        $matcher->setRequest($request);
201
        $this->assertSame(80, $matcher->getMapKey());
202
203
        $result = $matcher->reverseMatch('ezdemo_site');
204
        $this->assertInstanceOf(Port::class, $result);
205
        $this->assertSame($request, $matcher->getRequest());
206
        $this->assertSame(8000, $result->getMapKey());
207
        $this->assertSame(8000, $result->getRequest()->port);
208
        $this->assertSame('http', $result->getRequest()->scheme);
209
    }
210
211 View Code Duplication
    private function createRouter(): Router
212
    {
213
        return new Router(
214
            $this->matcherBuilder,
215
            $this->createMock(LoggerInterface::class),
216
            'default_sa',
217
            [
218
                'Map\\Host' => [
219
                    'first_sa' => 'first_sa',
220
                    'first_siteaccess' => 'first_sa',
221
                    'third_siteaccess' => 'third_sa',
222
                ],
223
                'Map\\Port' => [
224
                    80 => 'fifth_sa',
225
                    81 => 'third_sa',
226
                    82 => 'fourth_sa',
227
                    83 => 'first_sa',
228
                    85 => 'first_sa',
229
                    443 => 'fourth_sa',
230
                ],
231
                'Map\\URI' => [
232
                    'first_sa' => 'first_sa',
233
                    'second_sa' => 'second_sa',
234
                ],
235
            ],
236
            $this->siteAccessProvider
237
        );
238
    }
239
}
240