Completed
Pull Request — master (#178)
by ignace nyamagana
03:19
created

UriInfoTest::sameValueAsProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 9.0909
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * League.Uri (https://uri.thephpleague.com)
5
 *
6
 * (c) Ignace Nyamagana Butera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LeagueTest\Uri;
13
14
use League\Uri\Http;
15
use League\Uri\Uri;
16
use League\Uri\UriInfo;
17
use PHPUnit\Framework\TestCase;
18
use Psr\Http\Message\UriInterface as Psr7UriInterface;
19
use TypeError;
20
21
/**
22
 * @group modifier
23
 * @coversDefaultClass League\Uri\UriInfo
24
 */
25
class UriInfoTest extends TestCase
26
{
27
    /**
28
     * @dataProvider uriProvider
29
     *
30
     * @param Psr7UriInterface|Uri      $uri
31
     * @param null|Psr7UriInterface|Uri $base_uri
32
     * @param bool[]                    $infos
33
     */
34
    public function testInfo($uri, $base_uri, array $infos): void
35
    {
36
        if (null !== $base_uri) {
37
            self::assertSame($infos['same_document'], UriInfo::isSameDocument($uri, $base_uri));
38
        }
39
        self::assertSame($infos['relative_path'], UriInfo::isRelativePath($uri));
40
        self::assertSame($infos['absolute_path'], UriInfo::isAbsolutePath($uri));
41
        self::assertSame($infos['absolute_uri'], UriInfo::isAbsolute($uri));
42
        self::assertSame($infos['network_path'], UriInfo::isNetworkPath($uri));
43
    }
44
45
    public function uriProvider(): array
46
    {
47
        return [
48
            'absolute uri' => [
49
                'uri' => Http::createFromString('http://a/p?q#f'),
50
                'base_uri' => null,
51
                'infos' => [
52
                    'absolute_uri' => true,
53
                    'network_path' => false,
54
                    'absolute_path' => false,
55
                    'relative_path' => false,
56
                    'same_document' => false,
57
                ],
58
            ],
59
            'network relative uri' => [
60
                'uri' => Http::createFromString('//스타벅스코리아.com/p?q#f'),
61
                'base_uri' => Http::createFromString('//xn--oy2b35ckwhba574atvuzkc.com/p?q#z'),
62
                'infos' => [
63
                    'absolute_uri' => false,
64
                    'network_path' => true,
65
                    'absolute_path' => false,
66
                    'relative_path' => false,
67
                    'same_document' => true,
68
                ],
69
            ],
70
            'path relative uri with non empty path' => [
71
                'uri' => Http::createFromString('p?q#f'),
72
                'base_uri' => null,
73
                'infos' => [
74
                    'absolute_uri' => false,
75
                    'network_path' => false,
76
                    'absolute_path' => false,
77
                    'relative_path' => true,
78
                    'same_document' => false,
79
                ],
80
            ],
81
            'path relative uri with empty' => [
82
                'uri' => Http::createFromString('?q#f'),
83
                'base_uri' => null,
84
                'infos' => [
85
                    'absolute_uri' => false,
86
                    'network_path' => false,
87
                    'absolute_path' => false,
88
                    'relative_path' => true,
89
                    'same_document' => false,
90
                ],
91
            ],
92
        ];
93
    }
94
95
    /**
96
     * @dataProvider failedUriProvider
97
     *
98
     * @param null|mixed $uri
99
     * @param null|mixed $base_uri
100
     */
101
    public function testStatThrowsInvalidArgumentException($uri, $base_uri): void
102
    {
103
        self::expectException(TypeError::class);
104
        UriInfo::isSameDocument($uri, $base_uri);
105
    }
106
107
    public function failedUriProvider(): array
108
    {
109
        return [
110
            'invalid uri' => [
111
                'uri' => Http::createFromString('http://a/p?q#f'),
112
                'base_uri' => 'http://example.com',
113
            ],
114
            'invalid base uri' => [
115
                'uri' => 'http://example.com',
116
                'base_uri' => Http::createFromString('//a/p?q#f'),
117
            ],
118
        ];
119
    }
120
121
    /**
122
     * @dataProvider functionProvider
123
     */
124
    public function testIsFunctionsThrowsTypeError(string $function): void
125
    {
126
        self::expectException(TypeError::class);
127
128
        UriInfo::$function('http://example.com');
129
    }
130
131
    public function functionProvider(): array
132
    {
133
        return [
134
            ['isAbsolute'],
135
            ['isNetworkPath'],
136
            ['isAbsolutePath'],
137
            ['isRelativePath'],
138
        ];
139
    }
140
141
    /**
142
     * @dataProvider sameValueAsProvider
143
     *
144
     * @param Psr7UriInterface|Uri $uri1
145
     * @param Psr7UriInterface|Uri $uri2
146
     */
147
    public function testSameValueAs($uri1, $uri2, bool $expected): void
148
    {
149
        self::assertSame($expected, UriInfo::isSameDocument($uri1, $uri2));
150
    }
151
152
    public function sameValueAsProvider(): array
153
    {
154
        return [
155
            '2 disctincts URIs' => [
156
                Http::createFromString('http://example.com'),
157
                Uri::createFromString('ftp://example.com'),
158
                false,
159
            ],
160
            '2 identical URIs' => [
161
                Http::createFromString('http://example.com'),
162
                Http::createFromString('http://example.com'),
163
                true,
164
            ],
165
            '2 identical URIs after removing dot segment' => [
166
                Http::createFromString('http://example.org/~foo/'),
167
                Http::createFromString('http://example.ORG/bar/./../~foo/'),
168
                true,
169
            ],
170
            '2 distincts relative URIs' => [
171
                Http::createFromString('~foo/'),
172
                Http::createFromString('../~foo/'),
173
                false,
174
            ],
175
            '2 identical relative URIs' => [
176
                Http::createFromString('../%7efoo/'),
177
                Http::createFromString('../~foo/'),
178
                true,
179
            ],
180
            '2 identical URIs after normalization (1)' => [
181
                Http::createFromString('HtTp://مثال.إختبار:80/%7efoo/%7efoo/'),
182
                Http::createFromString('http://xn--mgbh0fb.xn--kgbechtv/%7Efoo/~foo/'),
183
                true,
184
            ],
185
            '2 identical URIs after normalization (2)' => [
186
                Http::createFromString('http://www.example.com'),
187
                Http::createFromString('http://www.example.com/'),
188
                true,
189
            ],
190
            '2 identical URIs after normalization (3)' => [
191
                Http::createFromString('http://www.example.com'),
192
                Http::createFromString('http://www.example.com:/'),
193
                true,
194
            ],
195
            '2 identical URIs after normalization (4)' => [
196
                Http::createFromString('http://www.example.com'),
197
                Http::createFromString('http://www.example.com:80/'),
198
                true,
199
            ],
200
        ];
201
    }
202
203
    /**
204
     * @dataProvider getOriginProvider
205
     *
206
     * @param Psr7UriInterface|Uri $uri
207
     * @param ?string              $expectedOrigin
0 ignored issues
show
Documentation introduced by
The doc-type ?string could not be parsed: Unknown type name "?string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
208
     */
209
    public function testGetOrigin($uri, ?string $expectedOrigin): void
210
    {
211
        self::assertSame($expectedOrigin, UriInfo::getOrigin($uri));
212
    }
213
214
    public function getOriginProvider(): array
215
    {
216
        return [
217
            'http uri' => [
218
                'uri' => Uri::createFromString('https://example.com/path?query#fragment'),
219
                'expectedOrigin' => 'https://example.com',
220
            ],
221
            'http uri with non standard port' => [
222
                'uri' => Uri::createFromString('https://example.com:81/path?query#fragment'),
223
                'expectedOrigin' => 'https://example.com:81',
224
            ],
225
            'relative uri' => [
226
                'uri' => Uri::createFromString('//example.com:81/path?query#fragment'),
227
                'expectedOrigin' => null,
228
            ],
229
            'absolute uri with user info' => [
230
                'uri' => Uri::createFromString('https://user:[email protected]:81/path?query#fragment'),
231
                'expectedOrigin' => 'https://example.com:81',
232
            ],
233
            'opaque URI' => [
234
                'uri' => Uri::createFromString('mailto:[email protected]'),
235
                'expectedOrigin' => null,
236
            ],
237
            'file URI' => [
238
                'uri' => Uri::createFromString('file:///usr/bin/test'),
239
                'expectedOrigin' => null,
240
            ],
241
            'blob' => [
242
                'uri' => Uri::createFromString('blob:https://mozilla.org:443/'),
243
                'expectedOrigin' => 'https://mozilla.org',
244
            ],
245
        ];
246
    }
247
}
248