Completed
Pull Request — master (#467)
by
unknown
03:05
created

testCreateFromSuperGlobalArrayRequestUriSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\Common\Http\Uri;
4
5
use OAuth\Common\Http\Uri\UriFactory;
6
use OAuth\Common\Http\Uri\Uri;
7
8
class UriFactoryTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     *
12
     */
13
    public function testConstructCorrectInterface()
14
    {
15
        $factory = new UriFactory();
16
17
        $this->assertInstanceOf('\\OAuth\\Common\\Http\\Uri\\UriFactoryInterface', $factory);
18
    }
19
20
    /**
21
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
22
     * @covers OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
23
     */
24
    public function testCreateFromSuperGlobalArrayUsingProxyStyle()
25
    {
26
        $factory = new UriFactory();
27
28
        $uri = $factory->createFromSuperGlobalArray(array('REQUEST_URI' => 'http://example.com'));
29
30
        $this->assertInstanceOf(
31
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
32
            $uri
33
        );
34
35
        $this->assertSame('http://example.com', $uri->getAbsoluteUri());
36
    }
37
38
    /**
39
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
40
     * @covers OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
41
     * @covers OAuth\Common\Http\Uri\UriFactory::detectScheme
42
     * @covers OAuth\Common\Http\Uri\UriFactory::detectHost
43
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPort
44
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPath
45
     * @covers OAuth\Common\Http\Uri\UriFactory::detectQuery
46
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromParts
47
     */
48
    public function testCreateFromSuperGlobalArrayHttp()
49
    {
50
        $factory = new UriFactory();
51
52
        $uri = $factory->createFromSuperGlobalArray(array(
53
            'HTTPS'        => 'off',
54
            'HTTP_HOST'    => 'example.com',
55
            'REQUEST_URI'  => '/foo',
56
            'QUERY_STRING' => 'param1=value1',
57
        ));
58
59
        $this->assertInstanceOf(
60
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
61
            $uri
62
        );
63
64
        $this->assertSame('http://example.com/foo?param1=value1', $uri->getAbsoluteUri());
65
    }
66
67
    /**
68
     * This looks wonky David. Should the port really fallback to 80 even when supplying https as scheme?
69
     *
70
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
71
     * @covers OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
72
     * @covers OAuth\Common\Http\Uri\UriFactory::detectScheme
73
     * @covers OAuth\Common\Http\Uri\UriFactory::detectHost
74
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPort
75
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPath
76
     * @covers OAuth\Common\Http\Uri\UriFactory::detectQuery
77
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromParts
78
     */
79
    public function testCreateFromSuperGlobalArrayHttps()
80
    {
81
        $factory = new UriFactory();
82
83
        $uri = $factory->createFromSuperGlobalArray(array(
84
            'HTTPS'        => 'on',
85
            'HTTP_HOST'    => 'example.com',
86
            'REQUEST_URI'  => '/foo',
87
            'QUERY_STRING' => 'param1=value1',
88
        ));
89
90
        $this->assertInstanceOf(
91
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
92
            $uri
93
        );
94
95
        $this->assertSame('https://example.com:80/foo?param1=value1', $uri->getAbsoluteUri());
96
    }
97
98
    /**
99
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
100
     * @covers OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
101
     * @covers OAuth\Common\Http\Uri\UriFactory::detectScheme
102
     * @covers OAuth\Common\Http\Uri\UriFactory::detectHost
103
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPort
104
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPath
105
     * @covers OAuth\Common\Http\Uri\UriFactory::detectQuery
106
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromParts
107
     */
108
    public function testCreateFromSuperGlobalArrayPortSupplied()
109
    {
110
        $factory = new UriFactory();
111
112
        $uri = $factory->createFromSuperGlobalArray(array(
113
            'HTTP_HOST'    => 'example.com',
114
            'SERVER_PORT'  => 21,
115
            'REQUEST_URI'  => '/foo',
116
            'QUERY_STRING' => 'param1=value1',
117
        ));
118
119
        $this->assertInstanceOf(
120
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
121
            $uri
122
        );
123
124
        $this->assertSame('http://example.com:21/foo?param1=value1', $uri->getAbsoluteUri());
125
    }
126
127
    /**
128
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
129
     * @covers OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
130
     * @covers OAuth\Common\Http\Uri\UriFactory::detectScheme
131
     * @covers OAuth\Common\Http\Uri\UriFactory::detectHost
132
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPort
133
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPath
134
     * @covers OAuth\Common\Http\Uri\UriFactory::detectQuery
135
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromParts
136
     */
137
    public function testCreateFromSuperGlobalArrayPortNotSet()
138
    {
139
        $factory = new UriFactory();
140
141
        $uri = $factory->createFromSuperGlobalArray(array(
142
            'HTTP_HOST'    => 'example.com',
143
            'REQUEST_URI'  => '/foo',
144
            'QUERY_STRING' => 'param1=value1',
145
        ));
146
147
        $this->assertInstanceOf(
148
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
149
            $uri
150
        );
151
152
        $this->assertSame('http://example.com/foo?param1=value1', $uri->getAbsoluteUri());
153
    }
154
155
    /**
156
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
157
     * @covers OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
158
     * @covers OAuth\Common\Http\Uri\UriFactory::detectScheme
159
     * @covers OAuth\Common\Http\Uri\UriFactory::detectHost
160
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPort
161
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPath
162
     * @covers OAuth\Common\Http\Uri\UriFactory::detectQuery
163
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromParts
164
     */
165
    public function testCreateFromSuperGlobalArrayRequestUriSet()
166
    {
167
        $factory = new UriFactory();
168
169
        $uri = $factory->createFromSuperGlobalArray(array(
170
            'HTTP_HOST'    => 'example.com',
171
            'REQUEST_URI'  => '/foo',
172
            'QUERY_STRING' => 'param1=value1',
173
        ));
174
175
        $this->assertInstanceOf(
176
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
177
            $uri
178
        );
179
180
        $this->assertSame('http://example.com/foo?param1=value1', $uri->getAbsoluteUri());
181
    }
182
183
    /**
184
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
185
     * @covers OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
186
     * @covers OAuth\Common\Http\Uri\UriFactory::detectScheme
187
     * @covers OAuth\Common\Http\Uri\UriFactory::detectHost
188
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPort
189
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPath
190
     * @covers OAuth\Common\Http\Uri\UriFactory::detectQuery
191
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromParts
192
     */
193
    public function testCreateFromSuperGlobalArrayRedirectUrlSet()
194
    {
195
        $factory = new UriFactory();
196
197
        $uri = $factory->createFromSuperGlobalArray(array(
198
            'HTTP_HOST'    => 'example.com',
199
            'REDIRECT_URL' => '/foo',
200
            'QUERY_STRING' => 'param1=value1',
201
        ));
202
203
        $this->assertInstanceOf(
204
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
205
            $uri
206
        );
207
208
        $this->assertSame('http://example.com/foo?param1=value1', $uri->getAbsoluteUri());
209
    }
210
211
    /**
212
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
213
     * @covers OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
214
     * @covers OAuth\Common\Http\Uri\UriFactory::detectScheme
215
     * @covers OAuth\Common\Http\Uri\UriFactory::detectHost
216
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPort
217
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPath
218
     * @covers OAuth\Common\Http\Uri\UriFactory::detectQuery
219
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromParts
220
     */
221
    public function testCreateFromSuperGlobalArrayThrowsExceptionOnDetectingPathMissingIndices()
222
    {
223
        $factory = new UriFactory();
224
225
        $this->setExpectedException('\\RuntimeException');
226
227
        $uri = $factory->createFromSuperGlobalArray(array(
0 ignored issues
show
Unused Code introduced by
$uri is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
228
            'HTTP_HOST'    => 'example.com',
229
            'QUERY_STRING' => 'param1=value1',
230
        ));
231
    }
232
233
    /**
234
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
235
     * @covers OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
236
     * @covers OAuth\Common\Http\Uri\UriFactory::detectScheme
237
     * @covers OAuth\Common\Http\Uri\UriFactory::detectHost
238
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPort
239
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPath
240
     * @covers OAuth\Common\Http\Uri\UriFactory::detectQuery
241
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromParts
242
     */
243
    public function testCreateFromSuperGlobalArrayWithQueryString()
244
    {
245
        $factory = new UriFactory();
246
247
        $uri = $factory->createFromSuperGlobalArray(array(
248
            'HTTP_HOST'    => 'example.com',
249
            'REQUEST_URI' => '/foo?param1=value1',
250
            'QUERY_STRING' => 'param1=value1',
251
        ));
252
253
        $this->assertInstanceOf(
254
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
255
            $uri
256
        );
257
258
        $this->assertSame('http://example.com/foo?param1=value1', $uri->getAbsoluteUri());
259
    }
260
261
    /**
262
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
263
     * @covers OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
264
     * @covers OAuth\Common\Http\Uri\UriFactory::detectScheme
265
     * @covers OAuth\Common\Http\Uri\UriFactory::detectHost
266
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPort
267
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPath
268
     * @covers OAuth\Common\Http\Uri\UriFactory::detectQuery
269
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromParts
270
     */
271
    public function testCreateFromSuperGlobalArrayWithoutQueryString()
272
    {
273
        $factory = new UriFactory();
274
275
        $uri = $factory->createFromSuperGlobalArray(array(
276
            'HTTP_HOST'    => 'example.com',
277
            'REQUEST_URI' => '/foo',
278
        ));
279
280
        $this->assertInstanceOf(
281
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
282
            $uri
283
        );
284
285
        $this->assertSame('http://example.com/foo', $uri->getAbsoluteUri());
286
    }
287
288
    /**
289
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromSuperGlobalArray
290
     * @covers OAuth\Common\Http\Uri\UriFactory::attemptProxyStyleParse
291
     * @covers OAuth\Common\Http\Uri\UriFactory::detectScheme
292
     * @covers OAuth\Common\Http\Uri\UriFactory::detectHost
293
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPort
294
     * @covers OAuth\Common\Http\Uri\UriFactory::detectPath
295
     * @covers OAuth\Common\Http\Uri\UriFactory::detectQuery
296
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromParts
297
     */
298
    public function testCreateFromSuperGlobalArrayHostWithColon()
299
    {
300
        $factory = new UriFactory();
301
302
        $uri = $factory->createFromSuperGlobalArray(array(
303
            'HTTP_HOST'    => 'example.com:80',
304
            'REQUEST_URI' => '/foo',
305
        ));
306
307
        $this->assertInstanceOf(
308
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
309
            $uri
310
        );
311
312
        $this->assertSame('http://example.com/foo', $uri->getAbsoluteUri());
313
    }
314
315
    /**
316
     * @covers OAuth\Common\Http\Uri\UriFactory::createFromAbsolute
317
     */
318
    public function testCreateFromAbsolute()
319
    {
320
        $factory = new UriFactory();
321
322
        $uri = $factory->createFromAbsolute('http://example.com');
323
324
        $this->assertInstanceOf(
325
            '\\OAuth\\Common\\Http\\Uri\\UriInterface',
326
            $uri
327
        );
328
329
        $this->assertSame('http://example.com', $uri->getAbsoluteUri());
330
    }
331
}
332