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

testUriResolverThrowExceptionOnConstructor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
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\UriResolver;
17
use PHPUnit\Framework\TestCase;
18
use TypeError;
19
20
/**
21
 * @group modifier
22
 * @coversDefaultClass League\Uri\UriResolver
23
 */
24
class RelativizerTest extends TestCase
25
{
26
    const BASE_URI = 'http://a/b/c/d;p?q';
27
28 View Code Duplication
    public function testRelativizeIsNotMade(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        $uri = Uri::createFromString('//path#fragment');
31
        $base_uri = Http::createFromString('http://example.com/path');
32
        $result = UriResolver::relativize($uri, $base_uri);
33
        self::assertEquals($result, $uri);
34
    }
35
36
    /**
37
     * @dataProvider relativizeProvider
38
     */
39
    public function testRelativize(string $uri, string $resolved, string $expected): void
40
    {
41
        $uri   = Http::createFromString($uri);
42
        $resolved = Uri::createFromString($resolved);
43
        self::assertSame($expected, (string) UriResolver::relativize($resolved, $uri));
44
    }
45
46
    public function relativizeProvider(): array
47
    {
48
        return [
49
            'different scheme'        => [self::BASE_URI,       'https://a/b/c/d;p?q',   'https://a/b/c/d;p?q'],
50
            'different authority'     => [self::BASE_URI,       'https://g/b/c/d;p?q',   'https://g/b/c/d;p?q'],
51
            'empty uri'               => [self::BASE_URI,       '',                      ''],
52
            'same uri'                => [self::BASE_URI,       self::BASE_URI,          ''],
53
            'same path'               => [self::BASE_URI,       'http://a/b/c/d;p',      'd;p'],
54
            'parent path 1'           => [self::BASE_URI,       'http://a/b/c/',         './'],
55
            'parent path 2'           => [self::BASE_URI,       'http://a/b/',           '../'],
56
            'parent path 3'           => [self::BASE_URI,       'http://a/',             '../../'],
57
            'parent path 4'           => [self::BASE_URI,       'http://a',              '../../'],
58
            'sibling path 1'          => [self::BASE_URI,       'http://a/b/c/g',        'g'],
59
            'sibling path 2'          => [self::BASE_URI,       'http://a/b/c/g/h',      'g/h'],
60
            'sibling path 3'          => [self::BASE_URI,       'http://a/b/g',          '../g'],
61
            'sibling path 4'          => [self::BASE_URI,       'http://a/g',            '../../g'],
62
            'query'                   => [self::BASE_URI,       'http://a/b/c/d;p?y',    '?y'],
63
            'fragment'                => [self::BASE_URI,       'http://a/b/c/d;p?q#s',  '#s'],
64
            'path + query'            => [self::BASE_URI,       'http://a/b/c/g?y',      'g?y'],
65
            'path + fragment'         => [self::BASE_URI,       'http://a/b/c/g#s',      'g#s'],
66
            'path + query + fragment' => [self::BASE_URI,       'http://a/b/c/g?y#s',    'g?y#s'],
67
            'empty segments'          => [self::BASE_URI,       'http://a/b/c/foo////g', 'foo////g'],
68
            'empty segments 1'        => [self::BASE_URI,       'http://a/b////c/foo/g', '..////c/foo/g'],
69
            'relative single dot 1'   => [self::BASE_URI,       '.',                     '.'],
70
            'relative single dot 2'   => [self::BASE_URI,       './',                    './'],
71
            'relative double dot 1'   => [self::BASE_URI,       '..',                    '..'],
72
            'relative double dot 2'   => [self::BASE_URI,       '../',                   '../'],
73
            'path with colon 1'       => ['http://a/',          'http://a/d:p',          './d:p'],
74
            'path with colon 2'       => [self::BASE_URI,       'http://a/b/c/g/d:p',    'g/d:p'],
75
            'scheme + auth 1'         => ['http://a',           'http://a?q#s',          '?q#s'],
76
            'scheme + auth 2'         => ['http://a/',          'http://a?q#s',          '/?q#s'],
77
            '2 relative paths 1'      => ['a/b',                '../..',                 '../..'],
78
            '2 relative paths 2'      => ['a/b',                './.',                   './.'],
79
            '2 relative paths 3'      => ['a/b',                '../c',                  '../c'],
80
            '2 relative paths 4'      => ['a/b',                'c/..',                  'c/..'],
81
            '2 relative paths 5'      => ['a/b',                'c/.',                   'c/.'],
82
            'baseUri with query'      => ['/a/b/?q',            '/a/b/#h',               './#h'],
83
            'targetUri with fragment' => ['/',                  '/#h',                   '#h'],
84
            'same document'           => ['/',                  '/',                     ''],
85
            'same URI normalized'     => ['http://a',           'http://a/',             ''],
86
        ];
87
    }
88
89
    public function testUriResolverThrowExceptionOnConstructor(): void
90
    {
91
        self::expectException(TypeError::class);
92
        UriResolver::relativize('ftp//a/b/c/d;p', 'toto');
0 ignored issues
show
Documentation introduced by
'ftp//a/b/c/d;p' is of type string, but the function expects a object<Psr\Http\Message\...Contracts\UriInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'toto' is of type string, but the function expects a object<Psr\Http\Message\...Contracts\UriInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
    }
94
95
    /**
96
     * @dataProvider relativizeAndResolveProvider
97
     */
98 View Code Duplication
    public function testRelativizeAndResolve(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
        string $baseUri,
100
        string $uri,
101
        string $expectedRelativize,
102
        string $expectedResolved
0 ignored issues
show
Unused Code introduced by
The parameter $expectedResolved is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    ): void {
104
        $baseUri = Uri::createFromString($baseUri);
105
        $uri = Http::createFromString($uri);
106
107
        $relativeUri = UriResolver::relativize($uri, $baseUri);
108
        self::assertSame($expectedRelativize, (string) $relativeUri);
109
    }
110
111
    public function relativizeAndResolveProvider(): array
112
    {
113
        return [
114
            'empty path'            => [self::BASE_URI, 'http://a/', '../../',   'http://a/'],
115
            'absolute empty path'   => [self::BASE_URI, 'http://a',  '../../',   'http://a/'],
116
            'relative single dot 1' => [self::BASE_URI, '.',         '.',        'http://a/b/c/'],
117
            'relative single dot 2' => [self::BASE_URI, './',        './',       'http://a/b/c/'],
118
            'relative double dot 1' => [self::BASE_URI, '..',        '..',       'http://a/b/'],
119
            'relative double dot 2' => [self::BASE_URI, '../',       '../',      'http://a/b/'],
120
            '2 relative paths 1'    => ['a/b',          '../..',     '../..',    '/'],
121
            '2 relative paths 2'    => ['a/b',          './.',       './.',      'a/'],
122
            '2 relative paths 3'    => ['a/b',          '../c',      '../c',     'c'],
123
            '2 relative paths 4'    => ['a/b',          'c/..',      'c/..',     'a/'],
124
            '2 relative paths 5'    => ['a/b',          'c/.',       'c/.',      'a/c/'],
125
            'path with colon'       => ['http://a/',    'http://a/d:p', './d:p', 'http://a/d:p'],
126
        ];
127
    }
128
}
129