Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / psr-http-message-bridge / Tests / Factory / AbstractHttpMessageFactoryTest.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\HttpMessageFactoryInterface;
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  * @author Antonio J. García Lagar <aj@garcialagar.es>
26  */
27 abstract class AbstractHttpMessageFactoryTest extends TestCase
28 {
29     private $factory;
30     private $tmpDir;
31
32     /**
33      * @return HttpMessageFactoryInterface
34      */
35     abstract protected function buildHttpMessageFactory();
36
37     public function setup()
38     {
39         $this->factory = $this->buildHttpMessageFactory();
40         $this->tmpDir = sys_get_temp_dir();
41     }
42
43     public function testCreateRequest()
44     {
45         $stdClass = new \stdClass();
46         $request = new Request(
47             array(
48                 'foo' => '1',
49                 'bar' => array('baz' => '42'),
50             ),
51             array(
52                 'twitter' => array(
53                     '@dunglas' => 'Kévin Dunglas',
54                     '@coopTilleuls' => 'Les-Tilleuls.coop',
55                 ),
56                 'baz' => '2',
57             ),
58             array(
59                 'a1' => $stdClass,
60                 'a2' => array('foo' => 'bar'),
61             ),
62             array(
63                 'c1' => 'foo',
64                 'c2' => array('c3' => 'bar'),
65             ),
66             array(
67                 'f1' => $this->createUploadedFile('F1', 'f1.txt', 'text/plain', UPLOAD_ERR_OK),
68                 'foo' => array('f2' => $this->createUploadedFile('F2', 'f2.txt', 'text/plain', UPLOAD_ERR_OK)),
69             ),
70             array(
71                 'REQUEST_METHOD' => 'POST',
72                 'HTTP_HOST' => 'dunglas.fr',
73                 'HTTP_X_SYMFONY' => '2.8',
74                 'REQUEST_URI' => '/testCreateRequest?foo=1&bar[baz]=42',
75                 'QUERY_STRING' => 'foo=1&bar[baz]=42',
76             ),
77             'Content'
78         );
79
80         $psrRequest = $this->factory->createRequest($request);
81
82         $this->assertEquals('Content', $psrRequest->getBody()->__toString());
83
84         $queryParams = $psrRequest->getQueryParams();
85         $this->assertEquals('1', $queryParams['foo']);
86         $this->assertEquals('42', $queryParams['bar']['baz']);
87
88         $requestTarget = $psrRequest->getRequestTarget();
89         $this->assertEquals('/testCreateRequest?foo=1&bar[baz]=42', urldecode($requestTarget));
90
91         $parsedBody = $psrRequest->getParsedBody();
92         $this->assertEquals('Kévin Dunglas', $parsedBody['twitter']['@dunglas']);
93         $this->assertEquals('Les-Tilleuls.coop', $parsedBody['twitter']['@coopTilleuls']);
94         $this->assertEquals('2', $parsedBody['baz']);
95
96         $attributes = $psrRequest->getAttributes();
97         $this->assertEquals($stdClass, $attributes['a1']);
98         $this->assertEquals('bar', $attributes['a2']['foo']);
99
100         $cookies = $psrRequest->getCookieParams();
101         $this->assertEquals('foo', $cookies['c1']);
102         $this->assertEquals('bar', $cookies['c2']['c3']);
103
104         $uploadedFiles = $psrRequest->getUploadedFiles();
105         $this->assertEquals('F1', $uploadedFiles['f1']->getStream()->__toString());
106         $this->assertEquals('f1.txt', $uploadedFiles['f1']->getClientFilename());
107         $this->assertEquals('text/plain', $uploadedFiles['f1']->getClientMediaType());
108         $this->assertEquals(UPLOAD_ERR_OK, $uploadedFiles['f1']->getError());
109
110         $this->assertEquals('F2', $uploadedFiles['foo']['f2']->getStream()->__toString());
111         $this->assertEquals('f2.txt', $uploadedFiles['foo']['f2']->getClientFilename());
112         $this->assertEquals('text/plain', $uploadedFiles['foo']['f2']->getClientMediaType());
113         $this->assertEquals(UPLOAD_ERR_OK, $uploadedFiles['foo']['f2']->getError());
114
115         $serverParams = $psrRequest->getServerParams();
116         $this->assertEquals('POST', $serverParams['REQUEST_METHOD']);
117         $this->assertEquals('2.8', $serverParams['HTTP_X_SYMFONY']);
118         $this->assertEquals('POST', $psrRequest->getMethod());
119         $this->assertEquals(array('2.8'), $psrRequest->getHeader('X-Symfony'));
120     }
121
122     public function testGetContentCanBeCalledAfterRequestCreation()
123     {
124         $header = array('HTTP_HOST' => 'dunglas.fr');
125         $request = new Request(array(), array(), array(), array(), array(), $header, 'Content');
126
127         $psrRequest = $this->factory->createRequest($request);
128
129         $this->assertEquals('Content', $psrRequest->getBody()->__toString());
130         $this->assertEquals('Content', $request->getContent());
131     }
132
133     private function createUploadedFile($content, $originalName, $mimeType, $error)
134     {
135         $path = tempnam($this->tmpDir, uniqid());
136         file_put_contents($path, $content);
137
138         if (class_exists('Symfony\Component\HttpFoundation\HeaderUtils')) {
139             // Symfony 4.1+
140             return new UploadedFile($path, $originalName, $mimeType, $error, true);
141         }
142         return new UploadedFile($path, $originalName, $mimeType, filesize($path), $error, true);
143     }
144
145     public function testCreateResponse()
146     {
147         $response = new Response(
148             'Response content.',
149             202,
150             array('X-Symfony' => array('3.4'))
151         );
152         $response->headers->setCookie(new Cookie('city', 'Lille', new \DateTime('Wed, 13 Jan 2021 22:23:01 GMT')));
153
154         $psrResponse = $this->factory->createResponse($response);
155         $this->assertEquals('Response content.', $psrResponse->getBody()->__toString());
156         $this->assertEquals(202, $psrResponse->getStatusCode());
157         $this->assertEquals(array('3.4'), $psrResponse->getHeader('X-Symfony'));
158
159         $cookieHeader = $psrResponse->getHeader('Set-Cookie');
160         $this->assertInternalType('array', $cookieHeader);
161         $this->assertCount(1, $cookieHeader);
162         $this->assertRegExp('{city=Lille; expires=Wed, 13-Jan-2021 22:23:01 GMT;( max-age=\d+;)? path=/; httponly}i', $cookieHeader[0]);
163     }
164
165     public function testCreateResponseFromStreamed()
166     {
167         $response = new StreamedResponse(function () {
168             echo "Line 1\n";
169             flush();
170
171             echo "Line 2\n";
172             flush();
173         });
174
175         $psrResponse = $this->factory->createResponse($response);
176
177         $this->assertEquals("Line 1\nLine 2\n", $psrResponse->getBody()->__toString());
178     }
179
180     public function testCreateResponseFromBinaryFile()
181     {
182         $path = tempnam($this->tmpDir, uniqid());
183         file_put_contents($path, 'Binary');
184
185         $response = new BinaryFileResponse($path);
186
187         $psrResponse = $this->factory->createResponse($response);
188
189         $this->assertEquals('Binary', $psrResponse->getBody()->__toString());
190     }
191
192     public function testUploadErrNoFile()
193     {
194         if (class_exists('Symfony\Component\HttpFoundation\HeaderUtils')) {
195             // Symfony 4.1+
196             $file = new UploadedFile('', '', null, UPLOAD_ERR_NO_FILE, true);
197         } else {
198             $file = new UploadedFile('', '', null, 0, UPLOAD_ERR_NO_FILE, true);
199         }
200         $this->assertEquals(0, $file->getSize());
201         $this->assertEquals(UPLOAD_ERR_NO_FILE, $file->getError());
202         $this->assertFalse($file->getSize(), 'SplFile::getSize() returns false on error');
203
204         $request = new Request(array(), array(), array(), array(),
205           array(
206             'f1' => $file,
207             'f2' => array('name' => null, 'type' => null, 'tmp_name' => null, 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0),
208           ),
209           array(
210             'REQUEST_METHOD' => 'POST',
211             'HTTP_HOST' => 'dunglas.fr',
212             'HTTP_X_SYMFONY' => '2.8',
213           ),
214           'Content'
215         );
216
217         $psrRequest = $this->factory->createRequest($request);
218
219         $uploadedFiles = $psrRequest->getUploadedFiles();
220
221         $this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f1']->getError());
222         $this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f2']->getError());
223     }
224 }