Passed
Pull Request — master (#257)
by Wilmer
10:49
created

ServerRequestFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 291
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 183
c 3
b 1
f 0
dl 0
loc 291
rs 10
wmc 5
1
<?php
2
3
4
namespace Yiisoft\Yii\Web\Tests;
5
6
use RuntimeException;
7
use Nyholm\Psr7\Factory\Psr17Factory;
8
use PHPUnit\Framework\TestCase;
9
use Yiisoft\Yii\Web\ServerRequestFactory;
10
11
class ServerRequestFactoryTest extends TestCase
12
{
13
    /**
14
     * @dataProvider hostParsingDataProvider
15
     */
16
    public function testHostParsingFromParameters(array $serverParams, array $expectParams): void
17
    {
18
        $serverRequest = $this->getServerRequestFactory()->createFromParameters($serverParams);
19
        $this->assertSame($expectParams['host'], $serverRequest->getUri()->getHost());
20
        $this->assertSame($expectParams['port'], $serverRequest->getUri()->getPort());
21
        $this->assertSame($expectParams['method'], $serverRequest->getMethod());
22
        $this->assertSame($expectParams['protocol'], $serverRequest->getProtocolVersion());
23
        $this->assertSame($expectParams['scheme'], $serverRequest->getUri()->getScheme());
24
        $this->assertSame($expectParams['path'], $serverRequest->getUri()->getPath());
25
        $this->assertSame($expectParams['query'], $serverRequest->getUri()->getQuery());
26
    }
27
28
    /**
29
     * @dataProvider hostParsingDataProvider
30
     * @backupGlobals enabled
31
     */
32
    public function testHostParsingFromGlobals(array $serverParams, array $expectParams): void
33
    {
34
        $_SERVER = $serverParams;
35
        $serverRequest = $this->getServerRequestFactory()->createFromGlobals();
36
        $this->assertSame($expectParams['host'], $serverRequest->getUri()->getHost());
37
        $this->assertSame($expectParams['port'], $serverRequest->getUri()->getPort());
38
        $this->assertSame($expectParams['method'], $serverRequest->getMethod());
39
        $this->assertSame($expectParams['protocol'], $serverRequest->getProtocolVersion());
40
        $this->assertSame($expectParams['scheme'], $serverRequest->getUri()->getScheme());
41
        $this->assertSame($expectParams['path'], $serverRequest->getUri()->getPath());
42
        $this->assertSame($expectParams['query'], $serverRequest->getUri()->getQuery());
43
    }
44
45
    public function testInvalidMethodException(): void
46
    {
47
        $this->expectException(RuntimeException::class);
48
        $this->expectExceptionMessage('Unable to determine HTTP request method.');
49
        $this->getServerRequestFactory()->createFromParameters([]);
50
    }
51
52
    public function hostParsingDataProvider(): array
53
    {
54
        return [
55
            'host' => [
56
                [
57
                    'HTTP_HOST' => 'test',
58
                    'REQUEST_METHOD' => 'GET',
59
                ],
60
                [
61
                    'method' => 'GET',
62
                    'host' => 'test',
63
                    'port' => null,
64
                    'protocol' => '1.1',
65
                    'scheme' => 'http',
66
                    'path' => '',
67
                    'query' => '',
68
                ]
69
            ],
70
            'hostWithPort' => [
71
                [
72
                    'HTTP_HOST' => 'test:88',
73
                    'REQUEST_METHOD' => 'GET',
74
                ],
75
                [
76
                    'method' => 'GET',
77
                    'host' => 'test',
78
                    'port' => 88,
79
                    'protocol' => '1.1',
80
                    'scheme' => 'http',
81
                    'path' => '',
82
                    'query' => '',
83
84
                ]
85
            ],
86
            'ipv4' => [
87
                [
88
                    'HTTP_HOST' => '127.0.0.1',
89
                    'REQUEST_METHOD' => 'GET',
90
                    'HTTPS' => true
91
                ],
92
                [
93
                    'method' => 'GET',
94
                    'host' => '127.0.0.1',
95
                    'port' => null,
96
                    'protocol' => '1.1',
97
                    'scheme' => 'https',
98
                    'path' => '',
99
                    'query' => '',
100
                ]
101
            ],
102
            'ipv4WithPort' => [
103
                [
104
                    'HTTP_HOST' => '127.0.0.1:443',
105
                    'REQUEST_METHOD' => 'GET'
106
                ],
107
                [
108
                    'method' => 'GET',
109
                    'host' => '127.0.0.1',
110
                    'port' => 443,
111
                    'protocol' => '1.1',
112
                    'scheme' => 'http',
113
                    'path' => '',
114
                    'query' => '',
115
                ]
116
            ],
117
            'ipv6' => [
118
                [
119
                    'HTTP_HOST' => '[::1]',
120
                    'REQUEST_METHOD' => 'GET'
121
                ],
122
                [
123
                    'method' => 'GET',
124
                    'host' => '[::1]',
125
                    'port' => null,
126
                    'protocol' => '1.1',
127
                    'scheme' => 'http',
128
                    'path' => '',
129
                    'query' => '',
130
                ]
131
            ],
132
            'ipv6WithPort' => [
133
                [
134
                    'HTTP_HOST' => '[::1]:443',
135
                    'REQUEST_METHOD' => 'GET'
136
                ],
137
                [
138
                    'method' => 'GET',
139
                    'host' => '[::1]',
140
                    'port' => 443,
141
                    'protocol' => '1.1',
142
                    'scheme' => 'http',
143
                    'path' => '',
144
                    'query' => '',
145
                ]
146
            ],
147
            'serverName' => [
148
                [
149
                    'SERVER_NAME' => 'test',
150
                    'REQUEST_METHOD' => 'GET'
151
                ],
152
                [
153
                    'method' => 'GET',
154
                    'host' => 'test',
155
                    'port' => null,
156
                    'protocol' => '1.1',
157
                    'scheme' => 'http',
158
                    'path' => '',
159
                    'query' => '',
160
                ]
161
            ],
162
            'hostAndServerName' => [
163
                [
164
                    'SERVER_NAME' => 'override',
165
                    'HTTP_HOST' => 'test',
166
                    'REQUEST_METHOD' => 'GET',
167
                    'SERVER_PORT' => 81
168
                ],
169
                [
170
                    'method' => 'GET',
171
                    'host' => 'test',
172
                    'port' => 81,
173
                    'protocol' => '1.1',
174
                    'scheme' => 'http',
175
                    'path' => '',
176
                    'query' => '',
177
                ]
178
            ],
179
            'none' => [
180
                [
181
                    'REQUEST_METHOD' => 'GET'
182
                ],
183
                [
184
                    'method' => 'GET',
185
                    'host' => '',
186
                    'port' => null,
187
                    'protocol' => '1.1',
188
                    'scheme' => 'http',
189
                    'path' => '',
190
                    'query' => '',
191
                ]
192
            ],
193
            'path' => [
194
                [
195
                    'REQUEST_METHOD' => 'GET',
196
                    'REQUEST_URI' => '/path/to/folder?param=1'
197
                ],
198
                [
199
                    'method' => 'GET',
200
                    'host' => '',
201
                    'port' => null,
202
                    'protocol' => '1.1',
203
                    'scheme' => 'http',
204
                    'path' => '/path/to/folder',
205
                    'query' => '',
206
                ]
207
            ],
208
            'query' => [
209
                [
210
                    'REQUEST_METHOD' => 'GET',
211
                    'QUERY_STRING' => 'path/to/folder?param=1'
212
                ],
213
                [
214
                    'method' => 'GET',
215
                    'host' => '',
216
                    'port' => null,
217
                    'protocol' => '1.1',
218
                    'scheme' => 'http',
219
                    'path' => '',
220
                    'query' => 'path/to/folder?param=1',
221
                ]
222
            ],
223
            'protocol' => [
224
                [
225
                    'REQUEST_METHOD' => 'GET',
226
                    'SERVER_PROTOCOL' => 'HTTP/1.0'
227
                ],
228
                [
229
                    'method' => 'GET',
230
                    'host' => '',
231
                    'port' => null,
232
                    'protocol' => '1.0',
233
                    'scheme' => 'http',
234
                    'path' => '',
235
                    'query' => '',
236
                ]
237
            ],
238
            'post' => [
239
                [
240
                    'REQUEST_METHOD' => 'POST',
241
                ],
242
                [
243
                    'method' => 'POST',
244
                    'host' => '',
245
                    'port' => null,
246
                    'protocol' => '1.1',
247
                    'scheme' => 'http',
248
                    'path' => '',
249
                    'query' => '',
250
                ]
251
            ],
252
            'delete' => [
253
                [
254
                    'REQUEST_METHOD' => 'DELETE',
255
                ],
256
                [
257
                    'method' => 'DELETE',
258
                    'host' => '',
259
                    'port' => null,
260
                    'protocol' => '1.1',
261
                    'scheme' => 'http',
262
                    'path' => '',
263
                    'query' => '',
264
                ]
265
            ],
266
            'put' => [
267
                [
268
                    'REQUEST_METHOD' => 'PUT',
269
                ],
270
                [
271
                    'method' => 'PUT',
272
                    'host' => '',
273
                    'port' => null,
274
                    'protocol' => '1.1',
275
                    'scheme' => 'http',
276
                    'path' => '',
277
                    'query' => '',
278
                ]
279
            ],
280
            'https' => [
281
                [
282
                    'REQUEST_METHOD' => 'PUT',
283
                    'HTTPS' => 'on'
284
                ],
285
                [
286
                    'method' => 'PUT',
287
                    'host' => '',
288
                    'port' => null,
289
                    'protocol' => '1.1',
290
                    'scheme' => 'https',
291
                    'path' => '',
292
                    'query' => '',
293
                ]
294
            ],
295
        ];
296
    }
297
298
    private function getServerRequestFactory(): ServerRequestFactory
299
    {
300
        $factory = new Psr17Factory();
301
        return new ServerRequestFactory($factory, $factory, $factory, $factory);
302
    }
303
}
304