HttpFoundationFactoryTest.php
9.6 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\PsrHttpMessage\Tests\Factory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UploadedFileInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Response;
use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\ServerRequest;
use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Stream;
use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\UploadedFile;
use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Uri;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile as HttpFoundationUploadedFile;
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class HttpFoundationFactoryTest extends TestCase
{
/** @var HttpFoundationFactory */
private $factory;
/** @var string */
private $tmpDir;
public function setUp(): void
{
$this->factory = new HttpFoundationFactory();
$this->tmpDir = sys_get_temp_dir();
}
public function testCreateRequest()
{
$stdClass = new \stdClass();
$serverRequest = new ServerRequest(
'1.1',
[
'X-Dunglas-API-Platform' => '1.0',
'X-data' => ['a', 'b'],
],
new Stream('The body'),
'/about/kevin',
'GET',
'http://les-tilleuls.coop/about/kevin',
['country' => 'France'],
['city' => 'Lille'],
['url' => 'http://les-tilleuls.coop'],
[
'doc1' => $this->createUploadedFile('Doc 1', UPLOAD_ERR_OK, 'doc1.txt', 'text/plain'),
'nested' => [
'docs' => [
$this->createUploadedFile('Doc 2', UPLOAD_ERR_OK, 'doc2.txt', 'text/plain'),
$this->createUploadedFile('Doc 3', UPLOAD_ERR_OK, 'doc3.txt', 'text/plain'),
],
],
],
['url' => 'http://dunglas.fr'],
['custom' => $stdClass]
);
$symfonyRequest = $this->factory->createRequest($serverRequest);
$files = $symfonyRequest->files->all();
$this->assertEquals('http://les-tilleuls.coop', $symfonyRequest->query->get('url'));
$this->assertEquals('doc1.txt', $files['doc1']->getClientOriginalName());
$this->assertEquals('doc2.txt', $files['nested']['docs'][0]->getClientOriginalName());
$this->assertEquals('doc3.txt', $files['nested']['docs'][1]->getClientOriginalName());
$this->assertEquals('http://dunglas.fr', $symfonyRequest->request->get('url'));
$this->assertEquals($stdClass, $symfonyRequest->attributes->get('custom'));
$this->assertEquals('Lille', $symfonyRequest->cookies->get('city'));
$this->assertEquals('France', $symfonyRequest->server->get('country'));
$this->assertEquals('The body', $symfonyRequest->getContent());
$this->assertEquals('1.0', $symfonyRequest->headers->get('X-Dunglas-API-Platform'));
$this->assertEquals(['a', 'b'], $symfonyRequest->headers->all('X-data'));
}
public function testCreateRequestWithStreamedBody()
{
$serverRequest = new ServerRequest(
'1.1',
[],
new Stream('The body'),
'/',
'GET',
null,
[],
[],
[],
[],
null,
[]
);
$symfonyRequest = $this->factory->createRequest($serverRequest, true);
$this->assertEquals('The body', $symfonyRequest->getContent());
}
public function testCreateRequestWithNullParsedBody()
{
$serverRequest = new ServerRequest(
'1.1',
[],
new Stream(),
'/',
'GET',
null,
[],
[],
[],
[],
null,
[]
);
$this->assertCount(0, $this->factory->createRequest($serverRequest)->request);
}
public function testCreateRequestWithObjectParsedBody()
{
$serverRequest = new ServerRequest(
'1.1',
[],
new Stream(),
'/',
'GET',
null,
[],
[],
[],
[],
new \stdClass(),
[]
);
$this->assertCount(0, $this->factory->createRequest($serverRequest)->request);
}
public function testCreateRequestWithUri()
{
$serverRequest = new ServerRequest(
'1.1',
[],
new Stream(),
'/',
'GET',
new Uri('http://les-tilleuls.coop/about/kevin'),
[],
[],
[],
[],
null,
[]
);
$this->assertEquals('/about/kevin', $this->factory->createRequest($serverRequest)->getPathInfo());
}
public function testCreateUploadedFile()
{
$uploadedFile = $this->createUploadedFile('An uploaded file.', UPLOAD_ERR_OK, 'myfile.txt', 'text/plain');
$symfonyUploadedFile = $this->callCreateUploadedFile($uploadedFile);
$size = $symfonyUploadedFile->getSize();
$uniqid = uniqid();
$symfonyUploadedFile->move($this->tmpDir, $uniqid);
$this->assertEquals($uploadedFile->getSize(), $size);
$this->assertEquals(UPLOAD_ERR_OK, $symfonyUploadedFile->getError());
$this->assertEquals('myfile.txt', $symfonyUploadedFile->getClientOriginalName());
$this->assertEquals('txt', $symfonyUploadedFile->getClientOriginalExtension());
$this->assertEquals('text/plain', $symfonyUploadedFile->getClientMimeType());
$this->assertEquals('An uploaded file.', file_get_contents($this->tmpDir.'/'.$uniqid));
}
public function testCreateUploadedFileWithError()
{
$this->expectException(FileException::class);
$this->expectExceptionMessage('The file "e" could not be written on disk.');
$uploadedFile = $this->createUploadedFile('Error.', UPLOAD_ERR_CANT_WRITE, 'e', 'text/plain');
$symfonyUploadedFile = $this->callCreateUploadedFile($uploadedFile);
$this->assertEquals(UPLOAD_ERR_CANT_WRITE, $symfonyUploadedFile->getError());
$symfonyUploadedFile->move($this->tmpDir, 'shouldFail.txt');
}
private function createUploadedFile($content, $error, $clientFileName, $clientMediaType): UploadedFile
{
$filePath = tempnam($this->tmpDir, uniqid());
file_put_contents($filePath, $content);
return new UploadedFile($filePath, filesize($filePath), $error, $clientFileName, $clientMediaType);
}
private function callCreateUploadedFile(UploadedFileInterface $uploadedFile): HttpFoundationUploadedFile
{
$reflection = new \ReflectionClass($this->factory);
$createUploadedFile = $reflection->getMethod('createUploadedFile');
$createUploadedFile->setAccessible(true);
return $createUploadedFile->invokeArgs($this->factory, [$uploadedFile]);
}
public function testCreateResponse()
{
$response = new Response(
'1.0',
[
'X-Symfony' => ['2.8'],
'Set-Cookie' => [
'theme=light',
'test',
'ABC=AeD; Domain=dunglas.fr; Path=/kevin; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Secure; HttpOnly; SameSite=Strict',
],
],
new Stream('The response body'),
200
);
$symfonyResponse = $this->factory->createResponse($response);
$this->assertEquals('1.0', $symfonyResponse->getProtocolVersion());
$this->assertEquals('2.8', $symfonyResponse->headers->get('X-Symfony'));
$cookies = $symfonyResponse->headers->getCookies();
$this->assertEquals('theme', $cookies[0]->getName());
$this->assertEquals('light', $cookies[0]->getValue());
$this->assertEquals(0, $cookies[0]->getExpiresTime());
$this->assertNull($cookies[0]->getDomain());
$this->assertEquals('/', $cookies[0]->getPath());
$this->assertFalse($cookies[0]->isSecure());
$this->assertFalse($cookies[0]->isHttpOnly());
$this->assertEquals('test', $cookies[1]->getName());
$this->assertNull($cookies[1]->getValue());
$this->assertEquals('ABC', $cookies[2]->getName());
$this->assertEquals('AeD', $cookies[2]->getValue());
$this->assertEquals(strtotime('Wed, 13 Jan 2021 22:23:01 GMT'), $cookies[2]->getExpiresTime());
$this->assertEquals('dunglas.fr', $cookies[2]->getDomain());
$this->assertEquals('/kevin', $cookies[2]->getPath());
$this->assertTrue($cookies[2]->isSecure());
$this->assertTrue($cookies[2]->isHttpOnly());
if (\defined('Symfony\Component\HttpFoundation\Cookie::SAMESITE_STRICT')) {
$this->assertEquals(Cookie::SAMESITE_STRICT, $cookies[2]->getSameSite());
}
$this->assertEquals('The response body', $symfonyResponse->getContent());
$this->assertEquals(200, $symfonyResponse->getStatusCode());
$symfonyResponse = $this->factory->createResponse($response, true);
ob_start();
$symfonyResponse->sendContent();
$sentContent = ob_get_clean();
$this->assertEquals('The response body', $sentContent);
$this->assertEquals(200, $symfonyResponse->getStatusCode());
}
}