869fd3203dc26c750a15ae021ddf42a5a9251c7a
[yaffs-website] / vendor / symfony / psr-http-message-bridge / Tests / Factory / DiactorosFactoryTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Bridge\PsrHttpMessage\Tests\Factory;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
16 use Symfony\Component\HttpFoundation\BinaryFileResponse;
17 use Symfony\Component\HttpFoundation\Cookie;
18 use Symfony\Component\HttpFoundation\File\UploadedFile;
19 use Symfony\Component\HttpFoundation\Request;
20 use Symfony\Component\HttpFoundation\Response;
21 use Symfony\Component\HttpFoundation\StreamedResponse;
22
23 /**
24  * @author Kévin Dunglas <dunglas@gmail.com>
25  */
26 class DiactorosFactoryTest extends TestCase
27 {
28     private $factory;
29     private $tmpDir;
30
31     public function setup()
32     {
33         if (!class_exists('Zend\Diactoros\ServerRequestFactory')) {
34             $this->markTestSkipped('Zend Diactoros is not installed.');
35         }
36
37         $this->factory = new DiactorosFactory();
38         $this->tmpDir = sys_get_temp_dir();
39     }
40
41     public function testCreateRequest()
42     {
43         $stdClass = new \stdClass();
44         $request = new Request(
45             array(
46                 'foo' => '1',
47                 'bar' => array('baz' => '42'),
48             ),
49             array(
50                 'twitter' => array(
51                     '@dunglas' => 'Kévin Dunglas',
52                     '@coopTilleuls' => 'Les-Tilleuls.coop',
53                 ),
54                 'baz' => '2',
55             ),
56             array(
57                 'a1' => $stdClass,
58                 'a2' => array('foo' => 'bar'),
59             ),
60             array(
61                 'c1' => 'foo',
62                 'c2' => array('c3' => 'bar'),
63             ),
64             array(
65                 'f1' => $this->createUploadedFile('F1', 'f1.txt', 'text/plain', UPLOAD_ERR_OK),
66                 'foo' => array('f2' => $this->createUploadedFile('F2', 'f2.txt', 'text/plain', UPLOAD_ERR_OK)),
67             ),
68             array(
69                 'REQUEST_METHOD' => 'POST',
70                 'HTTP_HOST' => 'dunglas.fr',
71                 'HTTP_X_SYMFONY' => '2.8',
72                 'REQUEST_URI' => '/testCreateRequest?foo=1&bar[baz]=42',
73                 'QUERY_STRING' => 'foo=1&bar[baz]=42',
74             ),
75             'Content'
76         );
77
78         $psrRequest = $this->factory->createRequest($request);
79
80         $this->assertEquals('Content', $psrRequest->getBody()->__toString());
81
82         $queryParams = $psrRequest->getQueryParams();
83         $this->assertEquals('1', $queryParams['foo']);
84         $this->assertEquals('42', $queryParams['bar']['baz']);
85
86         $requestTarget = $psrRequest->getRequestTarget();
87         $this->assertEquals('/testCreateRequest?foo=1&bar[baz]=42', $requestTarget);
88
89         $parsedBody = $psrRequest->getParsedBody();
90         $this->assertEquals('Kévin Dunglas', $parsedBody['twitter']['@dunglas']);
91         $this->assertEquals('Les-Tilleuls.coop', $parsedBody['twitter']['@coopTilleuls']);
92         $this->assertEquals('2', $parsedBody['baz']);
93
94         $attributes = $psrRequest->getAttributes();
95         $this->assertEquals($stdClass, $attributes['a1']);
96         $this->assertEquals('bar', $attributes['a2']['foo']);
97
98         $cookies = $psrRequest->getCookieParams();
99         $this->assertEquals('foo', $cookies['c1']);
100         $this->assertEquals('bar', $cookies['c2']['c3']);
101
102         $uploadedFiles = $psrRequest->getUploadedFiles();
103         $this->assertEquals('F1', $uploadedFiles['f1']->getStream()->__toString());
104         $this->assertEquals('f1.txt', $uploadedFiles['f1']->getClientFilename());
105         $this->assertEquals('text/plain', $uploadedFiles['f1']->getClientMediaType());
106         $this->assertEquals(UPLOAD_ERR_OK, $uploadedFiles['f1']->getError());
107
108         $this->assertEquals('F2', $uploadedFiles['foo']['f2']->getStream()->__toString());
109         $this->assertEquals('f2.txt', $uploadedFiles['foo']['f2']->getClientFilename());
110         $this->assertEquals('text/plain', $uploadedFiles['foo']['f2']->getClientMediaType());
111         $this->assertEquals(UPLOAD_ERR_OK, $uploadedFiles['foo']['f2']->getError());
112
113         $serverParams = $psrRequest->getServerParams();
114         $this->assertEquals('POST', $serverParams['REQUEST_METHOD']);
115         $this->assertEquals('2.8', $serverParams['HTTP_X_SYMFONY']);
116         $this->assertEquals('POST', $psrRequest->getMethod());
117         $this->assertEquals(array('2.8'), $psrRequest->getHeader('X-Symfony'));
118     }
119
120     public function testGetContentCanBeCalledAfterRequestCreation()
121     {
122         $header = array('HTTP_HOST' => 'dunglas.fr');
123         $request = new Request(array(), array(), array(), array(), array(), $header, 'Content');
124
125         $psrRequest = $this->factory->createRequest($request);
126
127         $this->assertEquals('Content', $psrRequest->getBody()->__toString());
128         $this->assertEquals('Content', $request->getContent());
129     }
130
131     private function createUploadedFile($content, $originalName, $mimeType, $error)
132     {
133         $path = tempnam($this->tmpDir, uniqid());
134         file_put_contents($path, $content);
135
136         return new UploadedFile($path, $originalName, $mimeType, filesize($path), $error, true);
137     }
138
139     public function testCreateResponse()
140     {
141         $response = new Response(
142             'Response content.',
143             202,
144             array('X-Symfony' => array('2.8'))
145         );
146         $response->headers->setCookie(new Cookie('city', 'Lille', new \DateTime('Wed, 13 Jan 2021 22:23:01 GMT')));
147
148         $psrResponse = $this->factory->createResponse($response);
149         $this->assertEquals('Response content.', $psrResponse->getBody()->__toString());
150         $this->assertEquals(202, $psrResponse->getStatusCode());
151         $this->assertEquals(array('2.8'), $psrResponse->getHeader('X-Symfony'));
152
153         $cookieHeader = $psrResponse->getHeader('Set-Cookie');
154         $this->assertInternalType('array', $cookieHeader);
155         $this->assertCount(1, $cookieHeader);
156         $this->assertRegExp('{city=Lille; expires=Wed, 13-Jan-2021 22:23:01 GMT;( max-age=\d+;)? path=/; httponly}', $cookieHeader[0]);
157     }
158
159     public function testCreateResponseFromStreamed()
160     {
161         $response = new StreamedResponse(function () {
162             echo "Line 1\n";
163             flush();
164
165             echo "Line 2\n";
166             flush();
167         });
168
169         $psrResponse = $this->factory->createResponse($response);
170
171         $this->assertEquals("Line 1\nLine 2\n", $psrResponse->getBody()->__toString());
172     }
173
174     public function testCreateResponseFromBinaryFile()
175     {
176         $path = tempnam($this->tmpDir, uniqid());
177         file_put_contents($path, 'Binary');
178
179         $response = new BinaryFileResponse($path);
180
181         $psrResponse = $this->factory->createResponse($response);
182
183         $this->assertEquals('Binary', $psrResponse->getBody()->__toString());
184     }
185
186     public function testUploadErrNoFile()
187     {
188         $file = new UploadedFile('', '', null, 0, UPLOAD_ERR_NO_FILE, true);
189         $this->assertEquals(0, $file->getSize());
190         $this->assertEquals(UPLOAD_ERR_NO_FILE, $file->getError());
191         $this->assertFalse($file->getSize(), 'SplFile::getSize() returns false on error');
192         $this->assertInternalType('integer', $file->getClientSize());
193
194         $request = new Request(array(), array(), array(), array(),
195           array(
196             'f1' => $file,
197             'f2' => array('name' => null, 'type' => null, 'tmp_name' => null, 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0),
198           ),
199           array(
200             'REQUEST_METHOD' => 'POST',
201             'HTTP_HOST' => 'dunglas.fr',
202             'HTTP_X_SYMFONY' => '2.8',
203           ),
204           'Content'
205         );
206
207         $psrRequest = $this->factory->createRequest($request);
208
209         $uploadedFiles = $psrRequest->getUploadedFiles();
210
211         $this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f1']->getError());
212         $this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f2']->getError());
213     }
214 }