File: ServerRequestFactoryTest.php

package info (click to toggle)
php-slim-psr7 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 656 kB
  • sloc: php: 5,274; makefile: 17; sh: 11; xml: 10
file content (224 lines) | stat: -rw-r--r-- 7,135 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php

/**
 * Slim Framework (https://slimframework.com)
 *
 * @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License)
 */

declare(strict_types=1);

namespace Slim\Tests\Psr7\Factory;

use PHPUnit\Framework\TestCase as ServerRequestFactoryTestCase;
//use Interop\Http\Factory\ServerRequestFactoryTestCase;
use InvalidArgumentException;
use Psr\Http\Message\UriInterface;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Slim\Psr7\Environment;
use Slim\Psr7\Factory\ServerRequestFactory;
use Slim\Psr7\Factory\UriFactory;
use Slim\Psr7\UploadedFile;
use stdClass;

use function microtime;
use function time;

/**
 * @group excluded
 */
#[\PHPUnit\Framework\Attributes\Group('excluded')]
class ServerRequestFactoryTest extends ServerRequestFactoryTestCase
{
    protected function setAccessible(ReflectionProperty|ReflectionMethod $property, bool $accessible = true): void
    {
        // only if PHP version < 8.1
        if (PHP_VERSION_ID > 80100) {
            return;
        }
        $property->setAccessible($accessible);
    }

    protected function createServerRequestFactory(): ServerRequestFactory
    {
        return new ServerRequestFactory();
    }

    protected function createUri($uri): UriInterface
    {
        return (new UriFactory())->createUri($uri);
    }

    public function testGetProtocolVersion()
    {
        $env = Environment::mock(['SERVER_PROTOCOL' => 'HTTP/1.0']);
        $request = $this->createServerRequestFactory()->createServerRequest('GET', '', $env);

        $this->assertEquals('1.0', $request->getProtocolVersion());
    }

    public function testCreateFromGlobals()
    {
        $GLOBALS['getallheaders_return'] = [
            'ACCEPT' => 'application/json',
            'ACCEPT-CHARSET' => 'utf-8',
            'ACCEPT-LANGUAGE' => 'en-US',
            'CONTENT-TYPE' => 'multipart/form-data',
            'HOST' => 'example.com',
            'USER-AGENT' => 'Slim Framework',
        ];

        $_SERVER = Environment::mock([
            'HTTP_HOST' => 'example.com',
            'PHP_AUTH_PW' => 'sekrit',
            'PHP_AUTH_USER' => 'josh',
            'QUERY_STRING' => 'abc=123',
            'REMOTE_ADDR' => '127.0.0.1',
            'REQUEST_METHOD' => 'GET',
            'REQUEST_TIME' => time(),
            'REQUEST_TIME_FLOAT' => microtime(true),
            'REQUEST_URI' => '/foo/bar',
            'SCRIPT_NAME' => '/index.php',
            'SERVER_NAME' => 'localhost',
            'SERVER_PORT' => 8080,
            'SERVER_PROTOCOL' => 'HTTP/1.1',
        ]);

        $request = ServerRequestFactory::createFromGlobals();

        unset($GLOBALS['getallheaders_return']);

        $this->assertEquals('GET', $request->getMethod());
        $this->assertEquals('1.1', $request->getProtocolVersion());

        $this->assertEquals('application/json', $request->getHeaderLine('Accept'));
        $this->assertEquals('utf-8', $request->getHeaderLine('Accept-Charset'));
        $this->assertEquals('en-US', $request->getHeaderLine('Accept-Language'));
        $this->assertEquals('multipart/form-data', $request->getHeaderLine('Content-Type'));

        $uri = $request->getUri();
        $this->assertEquals('josh:sekrit', $uri->getUserInfo());
        $this->assertEquals('example.com', $uri->getHost());
        $this->assertEquals('8080', $uri->getPort());
        $this->assertEquals('/foo/bar', $uri->getPath());
        $this->assertEquals('abc=123', $uri->getQuery());
        $this->assertEquals('', $uri->getFragment());
    }

    public function testCreateFromGlobalsWithParsedBody()
    {
        $_SERVER = Environment::mock([
            'HTTP_CONTENT_TYPE' => 'multipart/form-data',
            'REQUEST_METHOD' => 'POST',
        ]);

        $_POST = [
            'def' => '456',
        ];

        $request = ServerRequestFactory::createFromGlobals();

        // $_POST should be placed into the parsed body
        $this->assertEquals($_POST, $request->getParsedBody());
    }

    public function testCreateFromGlobalsBodyPointsToPhpInput()
    {
        $request = ServerRequestFactory::createFromGlobals();

        $this->assertEquals('php://input', $request->getBody()->getMetadata('uri'));
    }

    public function testCreateFromGlobalsSetsACache()
    {
        $request = ServerRequestFactory::createFromGlobals();

        // ensure that the Stream's $cache property has been set for this php://input stream
        $stream = $request->getBody();
        $class = new ReflectionClass($stream);
        $property = $class->getProperty('cache');
        $this->setAccessible($property);
        $cacheStreamValue = $property->getValue($stream);
        $this->assertNotNull($cacheStreamValue);
    }

    public function testCreateFromGlobalsWithUploadedFiles()
    {
        $_SERVER = Environment::mock([
            'HTTP_CONTENT_TYPE' => 'multipart/form-data',
            'REQUEST_METHOD' => 'POST',
        ]);

        $_FILES = [
            'uploaded_file' => [
                'name' => [
                    0 => 'foo.jpg',
                    1 => 'bar.jpg',
                ],

                'type' => [
                    0 => 'image/jpeg',
                    1 => 'image/jpeg',
                ],

                'tmp_name' => [
                    0 => '/tmp/phpUA3XUw',
                    1 => '/tmp/phpXUFS0x',
                ],

                'error' => [
                    0 => 0,
                    1 => 0,
                ],

                'size' => [
                    0 => 358708,
                    1 => 236162,
                ],
            ]
        ];

        $request = ServerRequestFactory::createFromGlobals();

        // $_FILES should be mapped to an array of UploadedFile objects
        $uploadedFiles = $request->getUploadedFiles();
        $this->assertCount(1, $uploadedFiles);
        $this->assertArrayHasKey('uploaded_file', $uploadedFiles);
        $this->assertInstanceOf(UploadedFile::class, $uploadedFiles['uploaded_file'][0]);
        $this->assertInstanceOf(UploadedFile::class, $uploadedFiles['uploaded_file'][1]);
    }

    public function testCreateFromGlobalsParsesBodyWithFragmentedContentType()
    {
        $_SERVER = Environment::mock([
            'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded;charset=utf-8',
            'REQUEST_METHOD' => 'POST',
        ]);

        $_POST = [
            'def' => '456',
        ];

        $request = ServerRequestFactory::createFromGlobals();

        $this->assertEquals($_POST, $request->getParsedBody());
    }

    public function testCreateServerRequestWithNullAsUri()
    {
        $this->expectException(InvalidArgumentException::class);

        $env = Environment::mock();
        $this->createServerRequestFactory()->createServerRequest('GET', null, $env);
    }

    public function testCreateServerRequestWithInvalidUriObject()
    {
        $this->expectException(InvalidArgumentException::class);

        $env = Environment::mock();
        $this->createServerRequestFactory()->createServerRequest('GET', new stdClass(), $env);
    }
}