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

RouterHostElementTest::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\RouterHostElementTest 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 eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\HostElement;
13
use PHPUnit\Framework\TestCase;
14
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Router;
15
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\Map\Host as HostMapMatcher;
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 RouterHostElementTest 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
                ['example', true],
42
            ]);
43
        $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...
44
            ->method('getSiteAccess')
45
            ->willReturnMap([
46
                ['first_sa', new SiteAccess('first_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
47
                ['second_sa', new SiteAccess('second_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
48
                ['third_sa', new SiteAccess('third_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
49
                ['fourth_sa', new SiteAccess('fourth_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
50
                ['fifth_sa', new SiteAccess('fifth_sa', Router::DEFAULT_SA_MATCHING_TYPE)],
51
                ['example', new SiteAccess('example', Router::DEFAULT_SA_MATCHING_TYPE)],
52
            ]);
53
    }
54
55
    public function testConstruct()
56
    {
57
        return $this->createRouter();
58
    }
59
60
    /**
61
     * @dataProvider matchProvider
62
     */
63 View Code Duplication
    public function testMatch(SimplifiedRequest $request, string $siteAccess)
64
    {
65
        $router = $this->createRouter();
66
        $sa = $router->match($request);
67
        $this->assertInstanceOf(SiteAccess::class, $sa);
68
        $this->assertSame($siteAccess, $sa->name);
69
        $router->setSiteAccess();
70
    }
71
72 View Code Duplication
    public function matchProvider()
73
    {
74
        return [
75
            [SimplifiedRequest::fromUrl('http://www.example.com'), 'example'],
76
            [SimplifiedRequest::fromUrl('https://www.example.com'), 'example'],
77
            [SimplifiedRequest::fromUrl('http://www.example.com/'), 'example'],
78
            [SimplifiedRequest::fromUrl('https://www.example.com/'), 'example'],
79
            [SimplifiedRequest::fromUrl('http://www.example.com//'), 'example'],
80
            [SimplifiedRequest::fromUrl('https://www.example.com//'), 'example'],
81
            [SimplifiedRequest::fromUrl('http://www.example.com:8080/'), 'example'],
82
            [SimplifiedRequest::fromUrl('http://www.example.com/first_siteaccess/'), 'example'],
83
            [SimplifiedRequest::fromUrl('http://www.example.com/?first_siteaccess'), 'example'],
84
            [SimplifiedRequest::fromUrl('http://www.example.com/?first_sa'), 'example'],
85
            [SimplifiedRequest::fromUrl('http://www.example.com/first_salt'), 'example'],
86
            [SimplifiedRequest::fromUrl('http://www.example.com/first_sa.foo'), 'example'],
87
            [SimplifiedRequest::fromUrl('http://www.example.com/test'), 'example'],
88
            [SimplifiedRequest::fromUrl('http://www.example.com/test/foo/'), 'example'],
89
            [SimplifiedRequest::fromUrl('http://www.example.com/test/foo/bar/'), 'example'],
90
            [SimplifiedRequest::fromUrl('http://www.example.com/test/foo/bar/first_sa'), 'example'],
91
            [SimplifiedRequest::fromUrl('http://www.example.com/default_sa'), 'example'],
92
93
            [SimplifiedRequest::fromUrl('http://www.example.com/first_sa'), 'example'],
94
            [SimplifiedRequest::fromUrl('http://www.example.com/first_sa/'), 'example'],
95
            [SimplifiedRequest::fromUrl('http://www.example.com//first_sa//'), 'example'],
96
            [SimplifiedRequest::fromUrl('http://www.example.com///first_sa///test'), 'example'],
97
            [SimplifiedRequest::fromUrl('http://www.example.com//first_sa//foo/bar'), 'example'],
98
            [SimplifiedRequest::fromUrl('http://www.example.com/first_sa/foo'), 'example'],
99
            [SimplifiedRequest::fromUrl('http://www.example.com:82/first_sa/'), 'example'],
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://dev.example.com/second_sa'), 'example'],
119
            [SimplifiedRequest::fromUrl('http://dev.example.com/second_sa/'), 'example'],
120
            [SimplifiedRequest::fromUrl('http://dev.example.com/second_sa?param1=foo'), 'example'],
121
            [SimplifiedRequest::fromUrl('http://dev.example.com/second_sa/foo/'), 'example'],
122
            [SimplifiedRequest::fromUrl('http://dev.example.com:82/second_sa/'), 'example'],
123
            [SimplifiedRequest::fromUrl('http://dev.example.com:83/second_sa/'), 'example'],
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 HostMapMatcher(['host' => 'foo'], []);
132
        $this->assertSame('host:map', $matcher->getName());
133
134
        $matcherHostElement = new HostElement([1]);
135
        $this->assertSame('host:element', $matcherHostElement->getName());
136
    }
137
138
    /**
139
     * @dataProvider reverseMatchProvider
140
     */
141 View Code Duplication
    public function testReverseMatch($siteAccessName, $elementNumber, SimplifiedRequest $request, $expectedHost)
142
    {
143
        $matcher = new HostElement([$elementNumber]);
144
        $matcher->setRequest($request);
145
        $result = $matcher->reverseMatch($siteAccessName);
146
        $this->assertInstanceOf(HostElement::class, $result);
147
        $this->assertSame($expectedHost, $result->getRequest()->host);
148
    }
149
150
    public function reverseMatchProvider()
151
    {
152
        return [
153
            ['foo', 1, SimplifiedRequest::fromUrl('http://bar.example.com/'), 'foo.example.com'],
154
            ['ezdemo_site', 1, SimplifiedRequest::fromUrl('http://ezflow_site.ez.no/'), 'ezdemo_site.ez.no'],
155
            ['metalfrance', 2, SimplifiedRequest::fromUrl('http://www.lolart.net/'), 'www.metalfrance.net'],
156
            ['fm', 3, SimplifiedRequest::fromUrl('http://www.phoenix-rises.fr/'), 'www.phoenix-rises.fm'],
157
        ];
158
    }
159
160
    public function testReverseMatchFail()
161
    {
162
        $matcher = new HostElement([3]);
163
        $matcher->setRequest(new SimplifiedRequest(['host' => 'ez.no']));
164
        $this->assertNull($matcher->reverseMatch('foo'));
165
    }
166
167 View Code Duplication
    public function testSerialize()
168
    {
169
        $matcher = new HostElement([1]);
170
        $matcher->setRequest(new SimplifiedRequest(['host' => 'ez.no', 'pathinfo' => '/foo/bar']));
171
        $sa = new SiteAccess('test', 'test', $matcher);
172
        $serializedSA1 = serialize($sa);
173
174
        $matcher->setRequest(new SimplifiedRequest(['host' => 'ez.no', 'pathinfo' => '/foo/bar/baz']));
175
        $serializedSA2 = serialize($sa);
176
177
        $this->assertSame($serializedSA1, $serializedSA2);
178
    }
179
180 View Code Duplication
    private function createRouter(): Router
181
    {
182
        return new Router(
183
            $this->matcherBuilder,
184
            $this->createMock(LoggerInterface::class),
185
            'default_sa',
186
            [
187
                'HostElement' => [
188
                    'value' => 2,
189
                ],
190
                'Map\\URI' => [
191
                    'first_sa' => 'first_sa',
192
                    'second_sa' => 'second_sa',
193
                ],
194
                'Map\\Host' => [
195
                    'first_sa' => 'first_sa',
196
                    'first_siteaccess' => 'first_sa',
197
                    'second_sa' => 'second_sa',
198
                ],
199
            ],
200
            $this->siteAccessProvider
201
        );
202
    }
203
}
204