UriFactoryTest::testConstructCorrectInterface()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\Common\Http\Uri;
4
5
use OAuth\Common\Http\Uri\UriFactory;
6
use PHPUnit\Framework\TestCase;
7
8
class UriFactoryTest extends TestCase
9
{
10
    public function testConstructCorrectInterface(): void
11
    {
12
        $factory = new UriFactory();
13
14
        self::assertInstanceOf('\\OAuth\\Common\\Http\\Uri\\UriFactoryInterface', $factory);
15
    }
16
17
    /**
18
     * @covers \OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
19
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
20
     */
21
    public function testCreateFromSuperGlobalArrayUsingProxyStyle(): void
22
    {
23
        $factory = new UriFactory();
24
25
        $uri = $factory->createFromSuperGlobalArray(['REQUEST_URI' => 'http://example.com']);
26
27
        self::assertInstanceOf(
28
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
29
            $uri
30
        );
31
32
        self::assertSame('http://example.com', $uri->getAbsoluteUri());
33
    }
34
35
    /**
36
     * @covers \OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
37
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromParts
38
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
39
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectHost
40
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPath
41
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPort
42
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectQuery
43
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectScheme
44
     */
45
    public function testCreateFromSuperGlobalArrayHttp(): void
46
    {
47
        $factory = new UriFactory();
48
49
        $uri = $factory->createFromSuperGlobalArray([
50
            'HTTPS' => 'off',
51
            'HTTP_HOST' => 'example.com',
52
            'REQUEST_URI' => '/foo',
53
            'QUERY_STRING' => 'param1=value1',
54
        ]);
55
56
        self::assertInstanceOf(
57
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
58
            $uri
59
        );
60
61
        self::assertSame('http://example.com/foo?param1=value1', $uri->getAbsoluteUri());
62
    }
63
64
    /**
65
     * This looks wonky David. Should the port really fallback to 80 even when supplying https as scheme?
66
     *
67
     * @covers \OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
68
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromParts
69
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
70
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectHost
71
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPath
72
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPort
73
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectQuery
74
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectScheme
75
     */
76
    public function testCreateFromSuperGlobalArrayHttps(): void
77
    {
78
        $factory = new UriFactory();
79
80
        $uri = $factory->createFromSuperGlobalArray([
81
            'HTTPS' => 'on',
82
            'HTTP_HOST' => 'example.com',
83
            'REQUEST_URI' => '/foo',
84
            'QUERY_STRING' => 'param1=value1',
85
        ]);
86
87
        self::assertInstanceOf(
88
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
89
            $uri
90
        );
91
92
        self::assertSame('https://example.com:80/foo?param1=value1', $uri->getAbsoluteUri());
93
    }
94
95
    /**
96
     * @covers \OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
97
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromParts
98
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
99
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectHost
100
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPath
101
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPort
102
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectQuery
103
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectScheme
104
     */
105
    public function testCreateFromSuperGlobalArrayPortSupplied(): void
106
    {
107
        $factory = new UriFactory();
108
109
        $uri = $factory->createFromSuperGlobalArray([
110
            'HTTP_HOST' => 'example.com',
111
            'SERVER_PORT' => 21,
112
            'REQUEST_URI' => '/foo',
113
            'QUERY_STRING' => 'param1=value1',
114
        ]);
115
116
        self::assertInstanceOf(
117
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
118
            $uri
119
        );
120
121
        self::assertSame('http://example.com:21/foo?param1=value1', $uri->getAbsoluteUri());
122
    }
123
124
    /**
125
     * @covers \OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
126
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromParts
127
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
128
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectHost
129
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPath
130
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPort
131
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectQuery
132
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectScheme
133
     */
134
    public function testCreateFromSuperGlobalArrayPortNotSet(): void
135
    {
136
        $factory = new UriFactory();
137
138
        $uri = $factory->createFromSuperGlobalArray([
139
            'HTTP_HOST' => 'example.com',
140
            'REQUEST_URI' => '/foo',
141
            'QUERY_STRING' => 'param1=value1',
142
        ]);
143
144
        self::assertInstanceOf(
145
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
146
            $uri
147
        );
148
149
        self::assertSame('http://example.com/foo?param1=value1', $uri->getAbsoluteUri());
150
    }
151
152
    /**
153
     * @covers \OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
154
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromParts
155
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
156
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectHost
157
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPath
158
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPort
159
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectQuery
160
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectScheme
161
     */
162
    public function testCreateFromSuperGlobalArrayRequestUriSet(): void
163
    {
164
        $factory = new UriFactory();
165
166
        $uri = $factory->createFromSuperGlobalArray([
167
            'HTTP_HOST' => 'example.com',
168
            'REQUEST_URI' => '/foo',
169
            'QUERY_STRING' => 'param1=value1',
170
        ]);
171
172
        self::assertInstanceOf(
173
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
174
            $uri
175
        );
176
177
        self::assertSame('http://example.com/foo?param1=value1', $uri->getAbsoluteUri());
178
    }
179
180
    /**
181
     * @covers \OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
182
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromParts
183
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
184
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectHost
185
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPath
186
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPort
187
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectQuery
188
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectScheme
189
     */
190
    public function testCreateFromSuperGlobalArrayRedirectUrlSet(): void
191
    {
192
        $factory = new UriFactory();
193
194
        $uri = $factory->createFromSuperGlobalArray([
195
            'HTTP_HOST' => 'example.com',
196
            'REDIRECT_URL' => '/foo',
197
            'QUERY_STRING' => 'param1=value1',
198
        ]);
199
200
        self::assertInstanceOf(
201
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
202
            $uri
203
        );
204
205
        self::assertSame('http://example.com/foo?param1=value1', $uri->getAbsoluteUri());
206
    }
207
208
    /**
209
     * @covers \OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
210
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromParts
211
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
212
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectHost
213
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPath
214
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPort
215
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectQuery
216
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectScheme
217
     */
218
    public function testCreateFromSuperGlobalArrayThrowsExceptionOnDetectingPathMissingIndices(): void
219
    {
220
        $factory = new UriFactory();
221
222
        $this->expectException('\\RuntimeException');
223
224
        $uri = $factory->createFromSuperGlobalArray([
0 ignored issues
show
Unused Code introduced by
The assignment to $uri is dead and can be removed.
Loading history...
225
            'HTTP_HOST' => 'example.com',
226
            'QUERY_STRING' => 'param1=value1',
227
        ]);
228
    }
229
230
    /**
231
     * @covers \OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
232
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromParts
233
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
234
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectHost
235
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPath
236
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPort
237
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectQuery
238
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectScheme
239
     */
240
    public function testCreateFromSuperGlobalArrayWithQueryString(): void
241
    {
242
        $factory = new UriFactory();
243
244
        $uri = $factory->createFromSuperGlobalArray([
245
            'HTTP_HOST' => 'example.com',
246
            'REQUEST_URI' => '/foo?param1=value1',
247
            'QUERY_STRING' => 'param1=value1',
248
        ]);
249
250
        self::assertInstanceOf(
251
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
252
            $uri
253
        );
254
255
        self::assertSame('http://example.com/foo?param1=value1', $uri->getAbsoluteUri());
256
    }
257
258
    /**
259
     * @covers \OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
260
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromParts
261
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
262
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectHost
263
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPath
264
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPort
265
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectQuery
266
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectScheme
267
     */
268
    public function testCreateFromSuperGlobalArrayWithoutQueryString(): void
269
    {
270
        $factory = new UriFactory();
271
272
        $uri = $factory->createFromSuperGlobalArray([
273
            'HTTP_HOST' => 'example.com',
274
            'REQUEST_URI' => '/foo',
275
        ]);
276
277
        self::assertInstanceOf(
278
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
279
            $uri
280
        );
281
282
        self::assertSame('http://example.com/foo', $uri->getAbsoluteUri());
283
    }
284
285
    /**
286
     * @covers \OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
287
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromParts
288
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
289
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectHost
290
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPath
291
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectPort
292
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectQuery
293
     * @covers \OAuth\Common\Http\Uri\UriFactory::detectScheme
294
     */
295
    public function testCreateFromSuperGlobalArrayHostWithColon(): void
296
    {
297
        $factory = new UriFactory();
298
299
        $uri = $factory->createFromSuperGlobalArray([
300
            'HTTP_HOST' => 'example.com:80',
301
            'REQUEST_URI' => '/foo',
302
        ]);
303
304
        self::assertInstanceOf(
305
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
306
            $uri
307
        );
308
309
        self::assertSame('http://example.com/foo', $uri->getAbsoluteUri());
310
    }
311
312
    /**
313
     * @covers \OAuth\Common\Http\Uri\UriFactory::createFromAbsolute
314
     */
315
    public function testCreateFromAbsolute(): void
316
    {
317
        $factory = new UriFactory();
318
319
        $uri = $factory->createFromAbsolute('http://example.com');
320
321
        self::assertInstanceOf(
322
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
323
            $uri
324
        );
325
326
        self::assertSame('http://example.com', $uri->getAbsoluteUri());
327
    }
328
}
329