Completed
Pull Request — master (#130)
by
unknown
01:45
created

anonymous//tests/ServerRequestFactoryTest.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 10
c 1
b 0
f 0
wmc 1
1
<?php
2
3
4
namespace Yiisoft\Yii\Web\Tests;
5
6
use Nyholm\Psr7\ServerRequest;
7
use Nyholm\Psr7\UploadedFile;
8
use Nyholm\Psr7\Uri;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Http\Message\ServerRequestFactoryInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Message\StreamFactoryInterface;
13
use Psr\Http\Message\StreamInterface;
14
use Psr\Http\Message\UploadedFileFactoryInterface;
15
use Psr\Http\Message\UploadedFileInterface;
16
use Psr\Http\Message\UriFactoryInterface;
17
use Psr\Http\Message\UriInterface;
18
use Yiisoft\Yii\Web\ServerRequestFactory;
19
20
class ServerRequestFactoryTest extends TestCase
21
{
22
23
    protected function getNewServerRequestFactory(): ServerRequestFactoryInterface
24
    {
25
        return new class implements ServerRequestFactoryInterface
26
        {
27
28
            public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
29
            {
30
                return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
31
            }
32
        };
33
    }
34
35
    protected function getNewUriFactory(): UriFactoryInterface
36
    {
37
        return new class implements UriFactoryInterface
38
        {
39
40
            public function createUri(string $uri = ''): UriInterface
41
            {
42
                return new Uri($uri);
43
            }
44
        };
45
    }
46
47
    protected function getNewUploadedFileFactory(): UploadedFileFactoryInterface
48
    {
49
        return new class implements UploadedFileFactoryInterface
50
        {
51
            public function createUploadedFile(
52
                StreamInterface $stream,
53
                int $size = null,
54
                int $error = \UPLOAD_ERR_OK,
55
                string $clientFilename = null,
56
                string $clientMediaType = null
57
            ): UploadedFileInterface {
58
                return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
59
            }
60
        };
61
    }
62
63
    protected function getNewStreamFactory(): StreamFactoryInterface
64
    {
65
        return new class implements StreamFactoryInterface
66
        {
67
            public function createStream(string $content = ''): StreamInterface
68
            {
69
                // TODO: Implement createStream() method.
70
            }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Psr\Http\Message\StreamInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
71
72
            public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
73
            {
74
                // TODO: Implement createStreamFromFile() method.
75
            }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Psr\Http\Message\StreamInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
76
77
            public function createStreamFromResource($resource): StreamInterface
78
            {
79
                // TODO: Implement createStreamFromResource() method.
80
            }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Psr\Http\Message\StreamInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
81
        };
82
    }
83
84
    public function hostParsingDataProvider(): array
85
    {
86
        return [
87
            'host' => [
88
                ['HTTP_HOST' => 'test'],
89
                'test',
90
                null,
91
            ],
92
            'hostWithPort' => [
93
                ['HTTP_HOST' => 'test:88'],
94
                'test',
95
                88,
96
            ],
97
            'ipv4' => [
98
                ['HTTP_HOST' => '127.0.0.1'],
99
                '127.0.0.1',
100
                null,
101
            ],
102
            'ipv4WithPort' => [
103
                ['HTTP_HOST' => '127.0.0.1:443'],
104
                '127.0.0.1',
105
                443,
106
            ],
107
            'ipv6' => [
108
                ['HTTP_HOST' => '[::1]'],
109
                '[::1]',
110
                null,
111
            ],
112
            'ipv6WithPort' => [
113
                ['HTTP_HOST' => '[::1]:443'],
114
                '[::1]',
115
                443,
116
            ],
117
            'serverName' => [
118
                ['SERVER_NAME' => 'test'],
119
                'test',
120
                null,
121
            ],
122
            'hostAndServerName' => [
123
                ['SERVER_NAME' => 'override', 'HTTP_HOST' => 'test'],
124
                'test',
125
                null,
126
            ],
127
            'none' => [
128
                [],
129
                '',
130
                null,
131
            ],
132
        ];
133
    }
134
135
    /**
136
     * @dataProvider hostParsingDataProvider
137
     */
138
    public function testHostParsing(array $serverParams, ?string $expectedHost, ?int $expectedPort)
139
    {
140
        $serverRequestFactory = new ServerRequestFactory($this->getNewServerRequestFactory(), $this->getNewUriFactory(),
141
            $this->getNewUploadedFileFactory(), $this->getNewStreamFactory());
142
        if (!isset($serverParams['REQUEST_METHOD'])) {
143
            $serverParams['REQUEST_METHOD'] = 'GET';
144
        }
145
        $serverRequest = $serverRequestFactory->createFromParameters($serverParams);
146
        $this->assertSame($expectedHost, $serverRequest->getUri()->getHost());
147
        $this->assertSame($expectedPort, $serverRequest->getUri()->getPort());
148
    }
149
}
150