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

FactoryTest::validServerArray()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 144

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 144
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Exceptions\SyntaxError;
15
use League\Uri\Http;
16
use League\Uri\Uri;
17
use PHPUnit\Framework\TestCase;
18
use TypeError;
19
20
/**
21
 * @group factory
22
 * @coversDefaultClass League\Uri\Uri
23
 */
24
class FactoryTest extends TestCase
25
{
26
    /**
27
     * @covers ::createFromDataPath
28
     *
29
     * @dataProvider invalidDataPath
30
     *
31
     * @param string $path
32
     */
33
    public function testCreateFromPathFailed($path): void
34
    {
35
        self::expectException(SyntaxError::class);
36
        Uri::createFromDataPath($path);
37
    }
38
39
    public function invalidDataPath(): array
40
    {
41
        return [
42
            'invalid format' => ['/usr/bin/yeah'],
43
        ];
44
    }
45
46
    /**
47
     * @covers ::createFromDataPath
48
     *
49
     * @dataProvider validFilePath
50
     */
51
    public function testCreateFromPath(string $path, string $expected): void
52
    {
53
        $context = stream_context_create([
54
            'http'=> [
55
                'method' => 'GET',
56
                'header' => "Accept-language: en\r\nCookie: foo=bar\r\n",
57
            ],
58
        ]);
59
60
        $uri = Uri::createFromDataPath(__DIR__.'/data/'.$path, $context);
61
        self::assertStringContainsString($expected, $uri->getPath());
62
    }
63
64
    public function validFilePath(): array
65
    {
66
        return [
67
            'text file' => ['hello-world.txt', 'text/plain'],
68
            'img file' => ['red-nose.gif', 'image/gif'],
69
        ];
70
    }
71
72
    /**
73
     * @covers ::createFromUnixPath
74
     *
75
     * @dataProvider unixpathProvider
76
     */
77
    public function testCreateFromUnixPath(string $uri, string $expected): void
78
    {
79
        self::assertSame($expected, (string) Uri::createFromUnixPath($uri));
80
    }
81
82
    public function unixpathProvider(): array
83
    {
84
        return [
85
            'relative path' => [
86
                'input' => 'path',
87
                'expected' => 'path',
88
            ],
89
            'absolute path' => [
90
                'input' => '/path',
91
                'expected' => 'file:///path',
92
            ],
93
            'path with empty char' => [
94
                'input' => '/path empty/bar',
95
                'expected' => 'file:///path%20empty/bar',
96
            ],
97
            'relative path with dot segments' => [
98
                'input' => 'path/./relative',
99
                'expected' => 'path/./relative',
100
            ],
101
            'absolute path with dot segments' => [
102
                'input' => '/path/./../relative',
103
                'expected' => 'file:///path/./../relative',
104
            ],
105
        ];
106
    }
107
108
    /**
109
     * @covers ::createFromWindowsPath
110
     *
111
     * @dataProvider windowLocalPathProvider
112
     */
113
    public function testCreateFromWindowsLocalPath(string $uri, string $expected): void
114
    {
115
        self::assertSame($expected, (string) Uri::createFromWindowsPath($uri));
116
    }
117
118
    public function windowLocalPathProvider(): array
119
    {
120
        return [
121
            'relative path' => [
122
                'input' => 'path',
123
                'expected' => 'path',
124
            ],
125
            'relative path with dot segments' => [
126
                'input' => 'path\.\relative',
127
                'expected' => 'path/./relative',
128
            ],
129
            'absolute path' => [
130
                'input' => 'c:\windows\My Documents 100%20\foo.txt',
131
                'expected' => 'file:///c:/windows/My%20Documents%20100%2520/foo.txt',
132
            ],
133
            'windows relative path' => [
134
                'input' => 'c:My Documents 100%20\foo.txt',
135
                'expected' => 'file:///c:My%20Documents%20100%2520/foo.txt',
136
            ],
137
            'absolute path with `|`' => [
138
                'input' => 'c|\windows\My Documents 100%20\foo.txt',
139
                'expected' => 'file:///c:/windows/My%20Documents%20100%2520/foo.txt',
140
            ],
141
            'windows relative path with `|`' => [
142
                'input' => 'c:My Documents 100%20\foo.txt',
143
                'expected' => 'file:///c:My%20Documents%20100%2520/foo.txt',
144
            ],
145
            'absolute path with dot segments' => [
146
                'input' => '\path\.\..\relative',
147
                'expected' => '/path/./../relative',
148
            ],
149
            'absolute UNC path' => [
150
                'input' => '\\\\server\share\My Documents 100%20\foo.txt',
151
                'expected' => 'file://server/share/My%20Documents%20100%2520/foo.txt',
152
            ],
153
        ];
154
    }
155
156
    public function testCreateFromUri(): void
157
    {
158
        $expected = 'http://login:[email protected]:443/test/query.php?kingkong=toto#doc3';
159
        $psr7 = Http::createFromString($expected);
160
        $leagueUri = Uri::createFromString($expected);
161
162
        $uriFromPsr7 = Uri::createFromUri($psr7);
163
        $uriFromLeagueUri = Uri::createFromUri($leagueUri);
164
165
        self::assertSame((string) $psr7, (string) $uriFromPsr7);
166
        self::assertSame((string) $psr7, (string) $uriFromLeagueUri);
167
168
        $uribis = Http::createFromString();
169
        self::assertSame((string) $uribis, Uri::createFromUri($uribis)->__toString());
170
    }
171
172
    public function testCreateFromUriFails(): void
173
    {
174
        self::expectException(TypeError::class);
175
        Http::createFromUri('http://example.com');
176
    }
177
178
    /**
179
     * @covers ::createFromServer
180
     * @covers League\Uri\Http::createFromServer
181
     * @covers ::fetchScheme
182
     * @covers ::fetchUserInfo
183
     * @covers ::fetchHostname
184
     * @covers ::fetchRequestUri
185
     *
186
     * @dataProvider validServerArray
187
     */
188
    public function testCreateFromServer(string $expected, array $input): void
189
    {
190
        self::assertSame($expected, (string) Uri::createFromServer($input));
191
        self::assertSame($expected, (string) Http::createFromServer($input));
192
    }
193
194
    public function validServerArray(): array
195
    {
196
        return [
197
            'with host' => [
198
                'https://example.com:23',
199
                [
200
                    'PHP_SELF' => '',
201
                    'REQUEST_URI' => '',
202
                    'SERVER_ADDR' => '127.0.0.1',
203
                    'HTTPS' => 'on',
204
                    'SERVER_PORT' => '23',
205
                    'HTTP_HOST' => 'example.com',
206
                ],
207
            ],
208
            'server address IPv4' => [
209
                'https://127.0.0.1:23',
210
                [
211
                    'PHP_SELF' => '',
212
                    'REQUEST_URI' => '',
213
                    'SERVER_ADDR' => '127.0.0.1',
214
                    'HTTPS' => 'on',
215
                    'SERVER_PORT' => 23,
216
                ],
217
            ],
218
            'server address IPv6' => [
219
                'https://[::1]:23',
220
                [
221
                    'PHP_SELF' => '',
222
                    'REQUEST_URI' => '',
223
                    'SERVER_ADDR' => '::1',
224
                    'HTTPS' => 'on',
225
                    'SERVER_PORT' => 23,
226
                ],
227
            ],
228
            'with port attached to host' => [
229
                'https://localhost:23',
230
                [
231
                    'PHP_SELF' => '',
232
                    'REQUEST_URI' => '',
233
                    'SERVER_ADDR' => '127.0.0.1',
234
                    'HTTPS' => 'on',
235
                    'SERVER_PORT' => 80,
236
                    'HTTP_HOST' => 'localhost:23',
237
                ],
238
            ],
239
            'with standard apache HTTP server' => [
240
                'http://localhost:23',
241
                [
242
                    'PHP_SELF' => '',
243
                    'REQUEST_URI' => '',
244
                    'SERVER_ADDR' => '127.0.0.1',
245
                    'HTTPS' => '',
246
                    'SERVER_PORT' => 80,
247
                    'HTTP_HOST' => 'localhost:23',
248
                ],
249
            ],
250
            'with IIS HTTP server' => [
251
                'http://localhost:23',
252
                [
253
                    'PHP_SELF' => '',
254
                    'REQUEST_URI' => '',
255
                    'SERVER_ADDR' => '127.0.0.1',
256
                    'HTTPS' => 'off',
257
                    'SERVER_PORT' => 80,
258
                    'HTTP_HOST' => 'localhost:23',
259
                ],
260
            ],
261
            'with IIS Rewritting server' => [
262
                'http://localhost:23/foo/bar?foo=bar',
263
                [
264
                    'PHP_SELF' => '',
265
                    'IIS_WasUrlRewritten' => '1',
266
                    'UNENCODED_URL' => '/foo/bar?foo=bar',
267
                    'REQUEST_URI' => 'toto',
268
                    'SERVER_PORT' => 23,
269
                    'HTTP_HOST' => 'localhost',
270
                ],
271
            ],
272
            'with standard port setting' => [
273
                'https://localhost:23',
274
                [
275
                    'PHP_SELF' => '',
276
                    'REQUEST_URI' => '',
277
                    'SERVER_ADDR' => '127.0.0.1',
278
                    'HTTPS' => 'on',
279
                    'SERVER_PORT' => 23,
280
                    'HTTP_HOST' => 'localhost',
281
                ],
282
            ],
283
            'without port' => [
284
                'https://localhost',
285
                [
286
                    'PHP_SELF' => '',
287
                    'REQUEST_URI' => '',
288
                    'SERVER_ADDR' => '127.0.0.1',
289
                    'HTTPS' => 'on',
290
                    'HTTP_HOST' => 'localhost',
291
                ],
292
            ],
293
            'with user info' => [
294
                'https://foo:bar@localhost:23',
295
                [
296
                    'PHP_SELF' => '',
297
                    'REQUEST_URI' => '',
298
                    'SERVER_ADDR' => '127.0.0.1',
299
                    'PHP_AUTH_USER' => 'foo',
300
                    'PHP_AUTH_PW' => 'bar',
301
                    'HTTPS' => 'on',
302
                    'SERVER_PORT' => 23,
303
                    'HTTP_HOST' => 'localhost:23',
304
                ],
305
            ],
306
            'with user info and HTTP AUTHORIZATION' => [
307
                'https://foo:bar@localhost:23',
308
                [
309
                    'PHP_SELF' => '',
310
                    'REQUEST_URI' => '',
311
                    'SERVER_ADDR' => '127.0.0.1',
312
                    'HTTP_AUTHORIZATION' => 'basic '.base64_encode('foo:bar'),
313
                    'HTTPS' => 'on',
314
                    'SERVER_PORT' => 23,
315
                    'HTTP_HOST' => 'localhost:23',
316
                ],
317
            ],
318
            'without request uri' => [
319
                'https://127.0.0.1:23/toto?foo=bar',
320
                [
321
                    'PHP_SELF' => '/toto',
322
                    'SERVER_ADDR' => '127.0.0.1',
323
                    'HTTPS' => 'on',
324
                    'SERVER_PORT' => 23,
325
                    'QUERY_STRING' => 'foo=bar',
326
                ],
327
            ],
328
            'without request uri and server host' => [
329
                'https://127.0.0.1:23',
330
                [
331
                    'SERVER_ADDR' => '127.0.0.1',
332
                    'HTTPS' => 'on',
333
                    'SERVER_PORT' => 23,
334
                ],
335
            ],
336
        ];
337
    }
338
339
    /**
340
     * @covers ::fetchHostname
341
     */
342
    public function testFailCreateFromServerWithoutHost(): void
343
    {
344
        self::expectException(SyntaxError::class);
345
        Uri::createFromServer([
346
            'PHP_SELF' => '',
347
            'REQUEST_URI' => '',
348
            'HTTPS' => 'on',
349
            'SERVER_PORT' => 23,
350
        ]);
351
    }
352
353
    /**
354
     * @covers ::fetchUserInfo
355
     */
356
    public function testFailCreateFromServerWithoutInvalidUserInfo(): void
357
    {
358
        self::expectException(SyntaxError::class);
359
        Uri::createFromServer([
360
            'PHP_SELF' => '/toto',
361
            'SERVER_ADDR' => '127.0.0.1',
362
            'HTTPS' => 'on',
363
            'SERVER_PORT' => 23,
364
            'QUERY_STRING' => 'foo=bar',
365
            'HTTP_AUTHORIZATION' => 'basic foo:bar',
366
        ]);
367
    }
368
369
    /**
370
     * @covers ::createFromBaseUri
371
     *
372
     * @dataProvider createProvider
373
     */
374
    public function testCreateFromBaseUri(string $base_uri, string $uri, string $expected): void
375
    {
376
        self::assertSame($expected, (string) Uri::createFromBaseUri($uri, $base_uri));
377
    }
378
    public function createProvider(): array
379
    {
380
        $base_uri = 'http://a/b/c/d;p?q';
381
382
        return [
383
            'base uri'                => [$base_uri, '',              $base_uri],
384
            'scheme'                  => [$base_uri, 'http://d/e/f',  'http://d/e/f'],
385
            'path 1'                  => [$base_uri, 'g',             'http://a/b/c/g'],
386
            'path 2'                  => [$base_uri, './g',           'http://a/b/c/g'],
387
            'path 3'                  => [$base_uri, 'g/',            'http://a/b/c/g/'],
388
            'path 4'                  => [$base_uri, '/g',            'http://a/g'],
389
            'authority'               => [$base_uri, '//g',           'http://g'],
390
            'query'                   => [$base_uri, '?y',            'http://a/b/c/d;p?y'],
391
            'path + query'            => [$base_uri, 'g?y',           'http://a/b/c/g?y'],
392
            'fragment'                => [$base_uri, '#s',            'http://a/b/c/d;p?q#s'],
393
            'path + fragment'         => [$base_uri, 'g#s',           'http://a/b/c/g#s'],
394
            'path + query + fragment' => [$base_uri, 'g?y#s',         'http://a/b/c/g?y#s'],
395
            'single dot 1'            => [$base_uri, '.',             'http://a/b/c/'],
396
            'single dot 2'            => [$base_uri, './',            'http://a/b/c/'],
397
            'single dot 3'            => [$base_uri, './g/.',         'http://a/b/c/g/'],
398
            'single dot 4'            => [$base_uri, 'g/./h',         'http://a/b/c/g/h'],
399
            'double dot 1'            => [$base_uri, '..',            'http://a/b/'],
400
            'double dot 2'            => [$base_uri, '../',           'http://a/b/'],
401
            'double dot 3'            => [$base_uri, '../g',          'http://a/b/g'],
402
            'double dot 4'            => [$base_uri, '../..',         'http://a/'],
403
            'double dot 5'            => [$base_uri, '../../',        'http://a/'],
404
            'double dot 6'            => [$base_uri, '../../g',       'http://a/g'],
405
            'double dot 7'            => [$base_uri, '../../../g',    'http://a/g'],
406
            'double dot 8'            => [$base_uri, '../../../../g', 'http://a/g'],
407
            'double dot 9'            => [$base_uri, 'g/../h' ,       'http://a/b/c/h'],
408
            'mulitple slashes'        => [$base_uri, 'foo////g',      'http://a/b/c/foo////g'],
409
            'complex path 1'          => [$base_uri, ';x',            'http://a/b/c/;x'],
410
            'complex path 2'          => [$base_uri, 'g;x',           'http://a/b/c/g;x'],
411
            'complex path 3'          => [$base_uri, 'g;x?y#s',       'http://a/b/c/g;x?y#s'],
412
            'complex path 4'          => [$base_uri, 'g;x=1/./y',     'http://a/b/c/g;x=1/y'],
413
            'complex path 5'          => [$base_uri, 'g;x=1/../y',    'http://a/b/c/y'],
414
            'dot segments presence 1' => [$base_uri, '/./g',          'http://a/g'],
415
            'dot segments presence 2' => [$base_uri, '/../g',         'http://a/g'],
416
            'dot segments presence 3' => [$base_uri, 'g.',            'http://a/b/c/g.'],
417
            'dot segments presence 4' => [$base_uri, '.g',            'http://a/b/c/.g'],
418
            'dot segments presence 5' => [$base_uri, 'g..',           'http://a/b/c/g..'],
419
            'dot segments presence 6' => [$base_uri, '..g',           'http://a/b/c/..g'],
420
            'origin uri without path' => ['http://h:b@a', 'b/../y',   'http://h:b@a/y'],
421
            'uri without auhtority'   => ['mailto:[email protected]', '[email protected]?subject=baz', 'mailto:[email protected]?subject=baz'],
422
        ];
423
    }
424
425
    /**
426
     * @covers ::createFromBaseUri
427
     */
428
    public function testCreateThrowExceptionWithBaseUriNotAbsolute(): void
429
    {
430
        self::expectException(SyntaxError::class);
431
        Uri::createFromBaseUri('/path/to/you', '//example.com');
432
    }
433
434
    /**
435
     * @covers ::createFromBaseUri
436
     */
437
    public function testCreateThrowExceptionWithUriNotAbsolute(): void
438
    {
439
        self::expectException(SyntaxError::class);
440
        Uri::createFromBaseUri('/path/to/you');
441
    }
442
443
    /**
444
     * @covers ::createFromBaseUri
445
     */
446
    public function testCreateWithUriWithoutAuthority(): void
447
    {
448
        self::assertSame(
449
            'data:text/plain;charset=us-ascii,',
450
            (string) Uri::createFromBaseUri('data:text/plain;charset=us-ascii,')
451
        );
452
    }
453
454
    /**
455
     * @covers ::createFromBaseUri
456
     */
457
    public function testCreateWithAbasoluteUriWithoutBaseUri(): void
458
    {
459
        self::assertSame(
460
            'scheme://host/sky?q#f',
461
            (string) Uri::createFromBaseUri('scheme://host/path/../sky?q#f')
462
        );
463
    }
464
}
465