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