Passed
Pull Request — master (#17)
by Mihail
15:10
created

UriSettersTest::it_should_set_the_scheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Tests\Koded\Http;
4
5
use Koded\Http\Uri;
6
use PHPUnit\Framework\TestCase;
7
8
class UriSettersTest extends TestCase
9
{
10
    private Uri $uri;
11
12
    protected function setUp(): void
13
    {
14
        $this->uri = new Uri('https://example.com:8080/foo/bar/#baz');
15
    }
16
17
    /**
18
     * @test
19
     */
20
    public function it_should_set_the_scheme()
21
    {
22
        $uri = $this->uri->withScheme('HTTP');
23
        $this->assertSame('http', $uri->getScheme());
24
        $this->assertNotSame($uri, $this->uri);
25
    }
26
27
    /**
28
     * @test
29
     */
30
    public function it_should_unset_the_scheme()
31
    {
32
        $uri = $this->uri->withScheme('');
33
        $this->assertSame('', $uri->getScheme());
34
        $this->assertNotSame($uri, $this->uri);
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function it_should_set_the_host()
41
    {
42
        $uri = $this->uri->withHost('example.net');
43
        $this->assertSame('example.net', $uri->getHost());
44
        $this->assertNotSame($uri, $this->uri);
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function it_should_unset_the_host()
51
    {
52
        $uri = $this->uri->withHost('');
53
        $this->assertSame('', $uri->getHost());
54
        $this->assertNotSame($uri, $this->uri);
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function it_should_set_the_userinfo()
61
    {
62
        $uri1 = $this->uri->withUserInfo('username');
63
        $this->assertSame('username', $uri1->getUserInfo(), 'Regular userinfo set');
64
        $this->assertNotSame($uri1, $this->uri);
65
66
        $uri2 = $uri1->withUserInfo('johndoe', 'pass');
67
        $this->assertNotSame($uri1, $uri2);
68
        $this->assertSame('johndoe:pass', $uri2->getUserInfo(), 'With userinfo password');
69
        $this->assertSame('username', $uri1->getUserInfo(), 'Not changed');
70
71
        $uri3 = $uri2->withUserInfo('', 'pass');
72
        $this->assertNotSame($uri3, $uri2);
73
        $this->assertSame('', $uri3->getUserInfo(), 'Without username the password os omitted');
74
    }
75
76
    /**
77
     * @test
78
     */
79
    public function it_should_unset_the_userinfo()
80
    {
81
        $uri = $this->uri->withHost('');
82
        $this->assertSame('', $uri->getHost());
83
        $this->assertNotSame($uri, $this->uri);
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function it_should_set_the_standard_port()
90
    {
91
        $uri = $this->uri->withPort(80);
92
        $this->assertSame(80, $uri->getPort());
93
        $this->assertNotSame($uri, $this->uri);
94
    }
95
96
    /**
97
     * @test
98
     */
99
    public function it_should_return_null_for_null_port_and_scheme()
100
    {
101
        $uri = (new Uri('/'))->withPort(null);
102
        $this->assertNull($uri->getPort());
103
    }
104
105
    /**
106
     * @test
107
     */
108
    public function it_should_set_the_nonstandard_port()
109
    {
110
        $uri = $this->uri->withPort(9000);
111
        $this->assertSame(9000, $uri->getPort());
112
        $this->assertNotSame($uri, $this->uri);
113
    }
114
115
    /**
116
     * @test
117
     */
118
    public function it_should_unset_the_port()
119
    {
120
        $uri = $this->uri->withPort(null);
121
        $this->assertNull($uri->getPort());
122
        $this->assertNotSame($uri, $this->uri);
123
    }
124
125
    /**
126
     * @test
127
     */
128
    public function it_should_set_the_path_as_is()
129
    {
130
        $uri = $this->uri->withPath('');
131
        $this->assertSame('', $uri->getPath());
132
133
        $uri = $this->uri->withPath('/foo');
134
        $this->assertSame('/foo', $uri->getPath());
135
136
        $uri = $this->uri->withPath('foo');
137
        $this->assertSame('foo', $uri->getPath());
138
    }
139
140
    /**
141
     * @test
142
     */
143
    public function it_should_reduce_multiple_slashes_in_path_without_authority()
144
    {
145
        $uri = $this->uri->withPath('//fubar');
146
        $this->assertSame('/fubar', $uri->getPath());
147
    }
148
149
    /**
150
     * @test
151
     */
152
    public function it_should_keep_multiple_slashes_in_path_with_present_authority()
153
    {
154
        $uri = $this->uri
155
            ->withPath('//fubar')
156
            ->withUserInfo('user');
157
158
        $this->assertSame('//fubar', $uri->getPath());
159
    }
160
161
    /**
162
     * @test
163
     */
164
    public function it_should_set_the_fragment()
165
    {
166
        $uri = $this->uri->withFragment('#foo-1.2.0');
167
        $this->assertSame('foo-1.2.0', $uri->getFragment());
168
169
        $uri = $this->uri->withFragment('%23foo-1.2.0');
170
        $this->assertSame('foo-1.2.0', $uri->getFragment());
171
172
        $uri = $this->uri->withFragment('foo-1.2.0');
173
        $this->assertSame('foo-1.2.0', $uri->getFragment());
174
    }
175
176
    /**
177
     * @test
178
     */
179
    public function it_should_remove_the_fragment()
180
    {
181
        $uri = $this->uri->withFragment('');
182
        $this->assertSame('', $uri->getFragment());
183
    }
184
}
185