Completed
Pull Request — master (#130)
by Alexander
03:02
created

ServerRequestFactoryTest::testHostParsing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 3
dl 0
loc 14
rs 9.9666
c 2
b 0
f 0
1
<?php
2
3
4
namespace Yiisoft\Yii\Web\Tests;
5
6
use Nyholm\Psr7\Factory\Psr17Factory;
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Yii\Web\ServerRequestFactory;
9
10
class ServerRequestFactoryTest extends TestCase
11
{
12
    public function hostParsingDataProvider(): array
13
    {
14
        return [
15
            'host' => [
16
                ['HTTP_HOST' => 'test'],
17
                'test',
18
                null,
19
            ],
20
            'hostWithPort' => [
21
                ['HTTP_HOST' => 'test:88'],
22
                'test',
23
                88,
24
            ],
25
            'ipv4' => [
26
                ['HTTP_HOST' => '127.0.0.1'],
27
                '127.0.0.1',
28
                null,
29
            ],
30
            'ipv4WithPort' => [
31
                ['HTTP_HOST' => '127.0.0.1:443'],
32
                '127.0.0.1',
33
                443,
34
            ],
35
            'ipv6' => [
36
                ['HTTP_HOST' => '[::1]'],
37
                '[::1]',
38
                null,
39
            ],
40
            'ipv6WithPort' => [
41
                ['HTTP_HOST' => '[::1]:443'],
42
                '[::1]',
43
                443,
44
            ],
45
            'serverName' => [
46
                ['SERVER_NAME' => 'test'],
47
                'test',
48
                null,
49
            ],
50
            'hostAndServerName' => [
51
                ['SERVER_NAME' => 'override', 'HTTP_HOST' => 'test'],
52
                'test',
53
                null,
54
            ],
55
            'none' => [
56
                [],
57
                '',
58
                null,
59
            ],
60
        ];
61
    }
62
63
    /**
64
     * @dataProvider hostParsingDataProvider
65
     */
66
    public function testHostParsing(array $serverParams, ?string $expectedHost, ?int $expectedPort): void
67
    {
68
        $factory = new Psr17Factory();
69
70
        $serverRequestFactory = new ServerRequestFactory(
71
            $factory, $factory,
72
            $factory, $factory
73
        );
74
        if (!isset($serverParams['REQUEST_METHOD'])) {
75
            $serverParams['REQUEST_METHOD'] = 'GET';
76
        }
77
        $serverRequest = $serverRequestFactory->createFromParameters($serverParams);
78
        $this->assertSame($expectedHost, $serverRequest->getUri()->getHost());
79
        $this->assertSame($expectedPort, $serverRequest->getUri()->getPort());
80
    }
81
}
82