Failed Conditions
Branch ignore-unknown-keywords (efa676)
by Stéphane
02:58
created

UriTest::testPointerSegments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal;
11
12
class UriTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @expectedException \InvalidArgumentException
16
     */
17
    public function testConstructorThrowsOnInvalidUri()
18
    {
19
        new Uri(':');
20
    }
21
22
    /**
23
     * @dataProvider absoluteAndRelativeProvider
24
     *
25
     * @param string $uri
26
     * @param bool   $isAbsolute
27
     */
28
    public function testIsAbsolute($uri, $isAbsolute)
29
    {
30
        $pointer = new Uri($uri);
31
        $this->assertEquals($isAbsolute, $pointer->isAbsolute());
32
    }
33
34
    /**
35
     * @dataProvider uriSegmentsProvider
36
     *
37
     * @param $uri
38
     * @param array $expectedSegments
39
     */
40
    public function testPointerSegments($uri, array $expectedSegments)
41
    {
42
        $pointer = new Uri($uri);
43
        $this->assertEquals($expectedSegments, $pointer->getPointerSegments());
44
    }
45
46
    /**
47
     * @expectedException \LogicException
48
     */
49
    public function testCannotResolveAgainstOtherUriIfAlreadyAbsolute()
50
    {
51
        $pointer = new Uri('http://localhost');
52
        $pointer->resolveAgainst(new Uri('file:///foo'));
53
    }
54
55
    /**
56
     * @expectedException \LogicException
57
     */
58
    public function testCannotResolveAgainstRelativeUri()
59
    {
60
        $pointer = new Uri('foo/bar');
61
        $pointer->resolveAgainst(new Uri('baz/quz'));
62
    }
63
64
    /**
65
     * @dataProvider againstUriProvider
66
     *
67
     * @param string $uri
68
     * @param string $againstUri
69
     * @param string $expectedResolved
70
     */
71
    public function testResolveAgainstAnotherUri($uri, $againstUri, $expectedResolved)
72
    {
73
        $pointer = new Uri($uri);
74
        $resolved = $pointer->resolveAgainst(new Uri($againstUri));
75
        $this->assertEquals($expectedResolved, $resolved);
76
    }
77
78
    public function testResolveAgainstUriChangesInternalState()
79
    {
80
        $pointer = new Uri('#quz/123');
81
        $against = new Uri('http://localhost:1234/foo/bar#baz');
82
        $pointer->resolveAgainst($against);
83
        $this->assertEquals('http://localhost:1234/foo/bar#quz/123', $pointer->getRawUri());
84
        $this->assertEquals('http', $pointer->getScheme());
85
        $this->assertEquals('http://localhost:1234/foo/bar', $pointer->getPrimaryResourceIdentifier());
86
        $this->assertEquals(['quz', '123'], $pointer->getPointerSegments());
87
    }
88
89
    /**
90
     * @expectedException \LogicException
91
     */
92
    public function testIsSamePrimaryResourceThrowsIfUrisAreNotAbsolute()
93
    {
94
        $pointer = new Uri('http://localhost');
95
        $against = new Uri('foo.bar/baz');
96
        $pointer->isSamePrimaryResource($against);
97
    }
98
99
    /**
100
     * @dataProvider sameResourceProvider
101
     *
102
     * @param string    uri
103
     * @param string $againstUri
104
     * @param bool   $isSame
105
     */
106
    public function testIsSamePrimaryResource($uri, $againstUri, $isSame)
107
    {
108
        $pointer = new Uri($uri);
109
        $against = new Uri($againstUri);
110
        $this->assertEquals($isSame, $pointer->isSamePrimaryResource($against));
111
    }
112
113
    public function absoluteAndRelativeProvider()
114
    {
115
        return [
116
            ['http://localhost?123', true],
117
            ['file://foo/bar', true],
118
            ['//foo.bar', false],
119
            ['#/foo/bar', false],
120
        ];
121
    }
122
123
    public function uriSegmentsProvider()
124
    {
125
        return [
126
            ['http://localhost', []],
127
            ['file:///foo/bar', []],
128
            ['http://foo.bar/baz?foo=bar#/foo/bar', ['foo', 'bar']],
129
            ['//localhost/foo#/bar/baz', ['bar', 'baz']],
130
            ['/quz', []],
131
            ['/quz/#//', []],
132
            ['/quz/#//foo/1%25/bar', ['foo', '1%', 'bar']],
133
            ['/quz/#//foo/1~02/bar', ['foo', '1~2', 'bar']],
134
            ['/quz/#//foo/1~12/bar', ['foo', '1/2', 'bar']],
135
        ];
136
    }
137
138
    public function againstUriProvider()
139
    {
140
        return [
141
            ['foo.json', 'http://localhost/bar', 'http://localhost/foo.json'],
142
            ['foo.json', 'http://localhost/bar/baz', 'http://localhost/bar/foo.json'],
143
            ['/foo.json', 'http://localhost/bar/baz', 'http://localhost/foo.json'],
144
            ['/foo.json#/baz', 'http://localhost/bar', 'http://localhost/foo.json#/baz'],
145
            ['/foo.json#/baz', 'http://localhost:1234/bar', 'http://localhost:1234/foo.json#/baz'],
146
            ['#/baz/quz', 'http://localhost/bar?a=b#foo/1', 'http://localhost/bar?a=b#/baz/quz'],
147
            ['#/baz/quz', 'file:///foo/bar', 'file:///foo/bar#/baz/quz'],
148
            ['baz#/baz/quz', 'file:///foo/bar#baz', 'file:///foo/baz#/baz/quz'],
149
            ['baz#/baz/quz', 'file:///C:/foo/bar#baz', 'file:///C:/foo/baz#/baz/quz'],
150
            ['baz#/baz/quz', 'file:///c:/foo/bar#baz', 'file:///c:/foo/baz#/baz/quz'],
151
            ['/baz#/baz/quz', 'file:///foo/bar#baz', 'file:///baz#/baz/quz'],
152
            ['//quz', 'http://foo/bar', 'http://quz'],
153
            ['//quz/baz#/baz', 'http://foo/bar#baz', 'http://quz/baz#/baz'],
154
            ['?foo=a#/baz', 'http://localhost/bar?foo=b#baz', 'http://localhost/bar?foo=a#/baz'],
155
            ['//john:123@localhost', 'http://localhost:456/bar', 'http://john:123@localhost'],
156
        ];
157
    }
158
159
    public function sameResourceProvider()
160
    {
161
        return [
162
            ['http://localhost', 'http://localhost', true],
163
            ['file://foo/bar#/baz', 'file://foo/bar#/baz/quz', true],
164
            ['http://localhost?a=b', 'http://localhost', false],
165
            ['file:///foo/bar', 'http://foo/bar', false],
166
        ];
167
    }
168
}
169