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

RouterHostTextTest::createRouter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 23
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\RouterHostTextTest 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\HostText as HostTextMatcher;
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 RouterHostTextTest 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
    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_siteaccess/'), 'first_sa'],
105
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/'), 'first_sa'],
106
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/'), 'first_sa'],
107
            [SimplifiedRequest::fromUrl('http://first_siteaccess/foo/'), 'first_sa'],
108
109
            [SimplifiedRequest::fromUrl('http://www.example.com/'), 'example'],
110
            [SimplifiedRequest::fromUrl('https://www.example.com/'), 'example'],
111
            [SimplifiedRequest::fromUrl('http://www.example.com:81/'), 'example'],
112
            [SimplifiedRequest::fromUrl('http://www.example.com/'), 'example'],
113
            [SimplifiedRequest::fromUrl('http://www.example.com:82/'), 'example'],
114
            [SimplifiedRequest::fromUrl('https://www.example.com:83/'), 'example'],
115
            [SimplifiedRequest::fromUrl('http://www.example.com/foo/'), 'example'],
116
117
            [SimplifiedRequest::fromUrl('http://example.com/second_sa'), 'second_sa'],
118
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/'), 'second_sa'],
119
            [SimplifiedRequest::fromUrl('http://example.com/second_sa?param1=foo'), 'second_sa'],
120
            [SimplifiedRequest::fromUrl('http://example.com/second_sa/foo/'), 'second_sa'],
121
            [SimplifiedRequest::fromUrl('http://example.com:82/second_sa/'), 'second_sa'],
122
            [SimplifiedRequest::fromUrl('http://example.com:83/second_sa/'), 'second_sa'],
123
            [SimplifiedRequest::fromUrl('http://first_siteaccess:82/second_sa/'), 'second_sa'],
124
            [SimplifiedRequest::fromUrl('http://first_siteaccess:83/second_sa/'), 'second_sa'],
125
        ];
126
    }
127
128
    public function testGetName()
129
    {
130
        $matcher = new HostTextMatcher(['host' => 'foo'], []);
131
        $this->assertSame('host:text', $matcher->getName());
132
    }
133
134
    public function testReverseMatch()
135
    {
136
        $matcher = new HostTextMatcher(
137
            [
138
                'prefix' => 'www.',
139
                'suffix' => '.com',
140
            ]
141
        );
142
143
        $matcher->setRequest(new SimplifiedRequest(['host' => 'www.my_siteaccess.com']));
144
145
        $result = $matcher->reverseMatch('foobar');
146
        $this->assertInstanceOf(HostTextMatcher::class, $result);
147
        $request = $result->getRequest();
148
        $this->assertInstanceOf(SimplifiedRequest::class, $request);
149
        $this->assertSame('www.foobar.com', $request->host);
150
    }
151
152 View Code Duplication
    private function createRouter(): Router
153
    {
154
        return new Router(
155
            $this->matcherBuilder,
156
            $this->createMock(LoggerInterface::class),
157
            'default_sa',
158
            [
159
                'HostText' => [
160
                    'prefix' => 'www.',
161
                    'suffix' => '.com',
162
                ],
163
                'Map\\URI' => [
164
                    'first_sa' => 'first_sa',
165
                    'second_sa' => 'second_sa',
166
                ],
167
                'Map\\Host' => [
168
                    'first_sa' => 'first_sa',
169
                    'first_siteaccess' => 'first_sa',
170
                ],
171
            ],
172
            $this->siteAccessProvider
173
        );
174
    }
175
}
176