Completed
Push — master ( 65bd53...58fd2e )
by Tobias
05:18 queued 03:25
created

ServerRequestCreatorTest::testFromGlobals()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 8.349
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests\Nyholm\Psr7Server;
6
7
use Nyholm\NSA;
8
use Nyholm\Psr7\Factory\ServerRequestFactory;
9
use Nyholm\Psr7\UploadedFile;
10
use Nyholm\Psr7\Uri;
11
use Nyholm\Psr7Server\ServerRequestCreator;
12
use PHPUnit\Framework\TestCase;
13
14
class ServerRequestCreatorTest extends TestCase
15
{
16
    private $creator;
17
18
    protected function setUp()/* The :void return type declaration that should be here would cause a BC issue */
19
    {
20
        parent::setUp();
21
        $psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
22
        $serverRequestFactory = new \Nyholm\Psr7\Factory\ServerRequestFactory();
23
24
        $this->creator = new ServerRequestCreator(
25
            $serverRequestFactory,
26
            $psr17Factory,
27
            $psr17Factory,
28
            $psr17Factory
29
        );
30
    }
31
32
33
    public function dataNormalizeFiles()
34
    {
35
        return [
36
            'Single file' => [
37
                [
38
                    'file' => [
39
                        'name' => 'MyFile.txt',
40
                        'type' => 'text/plain',
41
                        'tmp_name' => '/tmp/php/php1h4j1o',
42
                        'error' => '0',
43
                        'size' => '123',
44
                    ],
45
                ],
46
                [
47
                    'file' => new UploadedFile(
48
                        '/tmp/php/php1h4j1o',
49
                        123,
50
                        UPLOAD_ERR_OK,
51
                        'MyFile.txt',
52
                        'text/plain'
53
                    ),
54
                ],
55
            ],
56
            'Empty file' => [
57
                [
58
                    'image_file' => [
59
                        'name' => '',
60
                        'type' => '',
61
                        'tmp_name' => '',
62
                        'error' => '4',
63
                        'size' => '0',
64
                    ],
65
                ],
66
                [
67
                    'image_file' => new UploadedFile(
68
                        '',
69
                        0,
70
                        UPLOAD_ERR_NO_FILE,
71
                        '',
72
                        ''
73
                    ),
74
                ],
75
            ],
76
            'Already Converted' => [
77
                [
78
                    'file' => new UploadedFile(
79
                        '/tmp/php/php1h4j1o',
80
                        123,
81
                        UPLOAD_ERR_OK,
82
                        'MyFile.txt',
83
                        'text/plain'
84
                    ),
85
                ],
86
                [
87
                    'file' => new UploadedFile(
88
                        '/tmp/php/php1h4j1o',
89
                        123,
90
                        UPLOAD_ERR_OK,
91
                        'MyFile.txt',
92
                        'text/plain'
93
                    ),
94
                ],
95
            ],
96
            'Already Converted array' => [
97
                [
98
                    'file' => [
99
                        new UploadedFile(
100
                            '/tmp/php/php1h4j1o',
101
                            123,
102
                            UPLOAD_ERR_OK,
103
                            'MyFile.txt',
104
                            'text/plain'
105
                        ),
106
                        new UploadedFile(
107
                            '',
108
                            0,
109
                            UPLOAD_ERR_NO_FILE,
110
                            '',
111
                            ''
112
                        ),
113
                    ],
114
                ],
115
                [
116
                    'file' => [
117
                        new UploadedFile(
118
                            '/tmp/php/php1h4j1o',
119
                            123,
120
                            UPLOAD_ERR_OK,
121
                            'MyFile.txt',
122
                            'text/plain'
123
                        ),
124
                        new UploadedFile(
125
                            '',
126
                            0,
127
                            UPLOAD_ERR_NO_FILE,
128
                            '',
129
                            ''
130
                        ),
131
                    ],
132
                ],
133
            ],
134
            'Multiple files' => [
135
                [
136
                    'text_file' => [
137
                        'name' => 'MyFile.txt',
138
                        'type' => 'text/plain',
139
                        'tmp_name' => '/tmp/php/php1h4j1o',
140
                        'error' => '0',
141
                        'size' => '123',
142
                    ],
143
                    'image_file' => [
144
                        'name' => '',
145
                        'type' => '',
146
                        'tmp_name' => '',
147
                        'error' => '4',
148
                        'size' => '0',
149
                    ],
150
                ],
151
                [
152
                    'text_file' => new UploadedFile(
153
                        '/tmp/php/php1h4j1o',
154
                        123,
155
                        UPLOAD_ERR_OK,
156
                        'MyFile.txt',
157
                        'text/plain'
158
                    ),
159
                    'image_file' => new UploadedFile(
160
                        '',
161
                        0,
162
                        UPLOAD_ERR_NO_FILE,
163
                        '',
164
                        ''
165
                    ),
166
                ],
167
            ],
168
            'Nested files' => [
169
                [
170
                    'file' => [
171
                        'name' => [
172
                            0 => 'MyFile.txt',
173
                            1 => 'Image.png',
174
                        ],
175
                        'type' => [
176
                            0 => 'text/plain',
177
                            1 => 'image/png',
178
                        ],
179
                        'tmp_name' => [
180
                            0 => '/tmp/php/hp9hskjhf',
181
                            1 => '/tmp/php/php1h4j1o',
182
                        ],
183
                        'error' => [
184
                            0 => '0',
185
                            1 => '0',
186
                        ],
187
                        'size' => [
188
                            0 => '123',
189
                            1 => '7349',
190
                        ],
191
                    ],
192
                    'nested' => [
193
                        'name' => [
194
                            'other' => 'Flag.txt',
195
                            'test' => [
196
                                0 => 'Stuff.txt',
197
                                1 => '',
198
                            ],
199
                        ],
200
                        'type' => [
201
                            'other' => 'text/plain',
202
                            'test' => [
203
                                0 => 'text/plain',
204
                                1 => '',
205
                            ],
206
                        ],
207
                        'tmp_name' => [
208
                            'other' => '/tmp/php/hp9hskjhf',
209
                            'test' => [
210
                                0 => '/tmp/php/asifu2gp3',
211
                                1 => '',
212
                            ],
213
                        ],
214
                        'error' => [
215
                            'other' => '0',
216
                            'test' => [
217
                                0 => '0',
218
                                1 => '4',
219
                            ],
220
                        ],
221
                        'size' => [
222
                            'other' => '421',
223
                            'test' => [
224
                                0 => '32',
225
                                1 => '0',
226
                            ],
227
                        ],
228
                    ],
229
                ],
230
                [
231
                    'file' => [
232
                        0 => new UploadedFile(
233
                            '/tmp/php/hp9hskjhf',
234
                            123,
235
                            UPLOAD_ERR_OK,
236
                            'MyFile.txt',
237
                            'text/plain'
238
                        ),
239
                        1 => new UploadedFile(
240
                            '/tmp/php/php1h4j1o',
241
                            7349,
242
                            UPLOAD_ERR_OK,
243
                            'Image.png',
244
                            'image/png'
245
                        ),
246
                    ],
247
                    'nested' => [
248
                        'other' => new UploadedFile(
249
                            '/tmp/php/hp9hskjhf',
250
                            421,
251
                            UPLOAD_ERR_OK,
252
                            'Flag.txt',
253
                            'text/plain'
254
                        ),
255
                        'test' => [
256
                            0 => new UploadedFile(
257
                                '/tmp/php/asifu2gp3',
258
                                32,
259
                                UPLOAD_ERR_OK,
260
                                'Stuff.txt',
261
                                'text/plain'
262
                            ),
263
                            1 => new UploadedFile(
264
                                '',
265
                                0,
266
                                UPLOAD_ERR_NO_FILE,
267
                                '',
268
                                ''
269
                            ),
270
                        ],
271
                    ],
272
                ],
273
            ],
274
        ];
275
    }
276
277
    /**
278
     * @dataProvider dataNormalizeFiles
279
     */
280
    public function testNormalizeFiles($files, $expected)
281
    {
282
        $result = (new ServerRequestFactory())
283
            ->createServerRequestFromArrays(['REQUEST_METHOD' => 'POST'], [], [], [], [], $files)
284
            ->getUploadedFiles();
285
286
        $this->assertEquals($expected, $result);
287
    }
288
289
    public function testNormalizeFilesRaisesException()
290
    {
291
        $this->expectException(\InvalidArgumentException::class);
292
        $this->expectExceptionMessage('Invalid value in files specification');
293
294
        (new ServerRequestFactory())->createServerRequestFromArrays(['REQUEST_METHOD' => 'POST'], [], [], [], [], ['test' => 'something']);
295
    }
296
297
    public function testFromGlobals()
298
    {
299
        $server = [
300
            'PHP_SELF' => '/blog/article.php',
301
            'GATEWAY_INTERFACE' => 'CGI/1.1',
302
            'SERVER_ADDR' => 'Server IP: 217.112.82.20',
303
            'SERVER_NAME' => 'www.blakesimpson.co.uk',
304
            'SERVER_SOFTWARE' => 'Apache/2.2.15 (Win32) JRun/4.0 PHP/5.2.13',
305
            'SERVER_PROTOCOL' => 'HTTP/1.0',
306
            'REQUEST_METHOD' => 'POST',
307
            'REQUEST_TIME' => 'Request start time: 1280149029',
308
            'QUERY_STRING' => 'id=10&user=foo',
309
            'DOCUMENT_ROOT' => '/path/to/your/server/root/',
310
            'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
311
            'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
312
            'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
313
            'HTTP_ACCEPT_LANGUAGE' => 'en-gb,en;q=0.5',
314
            'HTTP_CONNECTION' => 'keep-alive',
315
            'HTTP_HOST' => 'www.blakesimpson.co.uk',
316
            'HTTP_REFERER' => 'http://previous.url.com',
317
            'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 ( .NET CLR 3.5.30729)',
318
            'HTTPS' => '1',
319
            'REMOTE_ADDR' => '193.60.168.69',
320
            'REMOTE_HOST' => 'Client server\'s host name',
321
            'REMOTE_PORT' => '5390',
322
            'SCRIPT_FILENAME' => '/path/to/this/script.php',
323
            'SERVER_ADMIN' => '[email protected]',
324
            'SERVER_PORT' => '80',
325
            'SERVER_SIGNATURE' => 'Version signature: 5.123',
326
            'SCRIPT_NAME' => '/blog/article.php',
327
            'REQUEST_URI' => '/blog/article.php?id=10&user=foo',
328
        ];
329
330
        $cookie = [
331
            'logged-in' => 'yes!',
332
        ];
333
334
        $post = [
335
            'name' => 'Pesho',
336
            'email' => '[email protected]',
337
        ];
338
339
        $get = [
340
            'id' => 10,
341
            'user' => 'foo',
342
        ];
343
344
        $files = [
345
            'file' => [
346
                'name' => 'MyFile.txt',
347
                'type' => 'text/plain',
348
                'tmp_name' => '/tmp/php/php1h4j1o',
349
                'error' => UPLOAD_ERR_OK,
350
                'size' => 123,
351
            ],
352
        ];
353
354
        $server = (new ServerRequestFactory())->createServerRequestFromArrays($server, [], $cookie, $get, $post, $files);
355
356
        $this->assertEquals('POST', $server->getMethod());
357
        $this->assertEquals(['Host' => ['www.blakesimpson.co.uk']], $server->getHeaders());
358
        $this->assertEquals('', (string) $server->getBody());
359
        $this->assertEquals('1.0', $server->getProtocolVersion());
360
        $this->assertEquals($cookie, $server->getCookieParams());
361
        $this->assertEquals($post, $server->getParsedBody());
362
        $this->assertEquals($get, $server->getQueryParams());
363
364
        $this->assertEquals(
365
            new Uri('http://www.blakesimpson.co.uk/blog/article.php?id=10&user=foo'),
366
            $server->getUri()
367
        );
368
369
        $expectedFiles = [
370
            'file' => new UploadedFile(
371
                '/tmp/php/php1h4j1o',
372
                123,
373
                UPLOAD_ERR_OK,
374
                'MyFile.txt',
375
                'text/plain'
376
            ),
377
        ];
378
379
        $this->assertEquals($expectedFiles, $server->getUploadedFiles());
380
    }
381
382
383
    public function dataGetUriFromGlobals()
384
    {
385
        $server = [
386
            'PHP_SELF' => '/blog/article.php',
387
            'GATEWAY_INTERFACE' => 'CGI/1.1',
388
            'SERVER_ADDR' => 'Server IP: 217.112.82.20',
389
            'SERVER_NAME' => 'www.blakesimpson.co.uk',
390
            'SERVER_SOFTWARE' => 'Apache/2.2.15 (Win32) JRun/4.0 PHP/5.2.13',
391
            'SERVER_PROTOCOL' => 'HTTP/1.0',
392
            'REQUEST_METHOD' => 'POST',
393
            'REQUEST_TIME' => 'Request start time: 1280149029',
394
            'QUERY_STRING' => 'id=10&user=foo',
395
            'DOCUMENT_ROOT' => '/path/to/your/server/root/',
396
            'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
397
            'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
398
            'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
399
            'HTTP_ACCEPT_LANGUAGE' => 'en-gb,en;q=0.5',
400
            'HTTP_CONNECTION' => 'keep-alive',
401
            'HTTP_HOST' => 'www.blakesimpson.co.uk',
402
            'HTTP_REFERER' => 'http://previous.url.com',
403
            'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 ( .NET CLR 3.5.30729)',
404
            'HTTPS' => '1',
405
            'REMOTE_ADDR' => '193.60.168.69',
406
            'REMOTE_HOST' => 'Client server\'s host name',
407
            'REMOTE_PORT' => '5390',
408
            'SCRIPT_FILENAME' => '/path/to/this/script.php',
409
            'SERVER_ADMIN' => '[email protected]',
410
            'SERVER_PORT' => '80',
411
            'SERVER_SIGNATURE' => 'Version signature: 5.123',
412
            'SCRIPT_NAME' => '/blog/article.php',
413
            'REQUEST_URI' => '/blog/article.php?id=10&user=foo',
414
        ];
415
416
        return [
417
            'Normal request' => [
418
                'http://www.blakesimpson.co.uk/blog/article.php?id=10&user=foo',
419
                $server,
420
            ],
421
            'Secure request' => [
422
                'https://www.blakesimpson.co.uk/blog/article.php?id=10&user=foo',
423
                array_merge($server, ['HTTPS' => 'on', 'SERVER_PORT' => '443']),
424
            ],
425
            'HTTP_HOST missing' => [
426
                'http://www.blakesimpson.co.uk/blog/article.php?id=10&user=foo',
427
                array_merge($server, ['HTTP_HOST' => null]),
428
            ],
429
            'No query String' => [
430
                'http://www.blakesimpson.co.uk/blog/article.php',
431
                array_merge($server, ['REQUEST_URI' => '/blog/article.php', 'QUERY_STRING' => '']),
432
            ],
433
            'Different port' => [
434
                'http://www.blakesimpson.co.uk:8324/blog/article.php?id=10&user=foo',
435
                array_merge($server, ['SERVER_PORT' => '8324']),
436
            ],
437
            'Empty server variable' => [
438
                '',
439
                [],
440
            ],
441
        ];
442
    }
443
444
    /**
445
     * @dataProvider dataGetUriFromGlobals
446
     */
447
    public function testGetUriFromGlobals($expected, $serverParams)
448
    {
449
        $this->assertEquals(new Uri($expected), NSA::invokeMethod($this->creator, 'createUriFromArray', $serverParams));
450
    }
451
}
452