4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Bridge\PsrHttpMessage\Tests\Factory;
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;
24 * @author Kévin Dunglas <dunglas@gmail.com>
26 class DiactorosFactoryTest extends TestCase
31 public function setup()
33 if (!class_exists('Zend\Diactoros\ServerRequestFactory')) {
34 $this->markTestSkipped('Zend Diactoros is not installed.');
37 $this->factory = new DiactorosFactory();
38 $this->tmpDir = sys_get_temp_dir();
41 public function testCreateRequest()
43 $stdClass = new \stdClass();
44 $request = new Request(
47 'bar' => array('baz' => '42'),
51 '@dunglas' => 'Kévin Dunglas',
52 '@coopTilleuls' => 'Les-Tilleuls.coop',
58 'a2' => array('foo' => 'bar'),
62 'c2' => array('c3' => 'bar'),
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)),
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',
78 $psrRequest = $this->factory->createRequest($request);
80 $this->assertEquals('Content', $psrRequest->getBody()->__toString());
82 $queryParams = $psrRequest->getQueryParams();
83 $this->assertEquals('1', $queryParams['foo']);
84 $this->assertEquals('42', $queryParams['bar']['baz']);
86 $requestTarget = $psrRequest->getRequestTarget();
87 $this->assertEquals('/testCreateRequest?foo=1&bar[baz]=42', $requestTarget);
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']);
94 $attributes = $psrRequest->getAttributes();
95 $this->assertEquals($stdClass, $attributes['a1']);
96 $this->assertEquals('bar', $attributes['a2']['foo']);
98 $cookies = $psrRequest->getCookieParams();
99 $this->assertEquals('foo', $cookies['c1']);
100 $this->assertEquals('bar', $cookies['c2']['c3']);
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());
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());
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'));
120 public function testGetContentCanBeCalledAfterRequestCreation()
122 $header = array('HTTP_HOST' => 'dunglas.fr');
123 $request = new Request(array(), array(), array(), array(), array(), $header, 'Content');
125 $psrRequest = $this->factory->createRequest($request);
127 $this->assertEquals('Content', $psrRequest->getBody()->__toString());
128 $this->assertEquals('Content', $request->getContent());
131 private function createUploadedFile($content, $originalName, $mimeType, $error)
133 $path = tempnam($this->tmpDir, uniqid());
134 file_put_contents($path, $content);
136 return new UploadedFile($path, $originalName, $mimeType, filesize($path), $error, true);
139 public function testCreateResponse()
141 $response = new Response(
144 array('X-Symfony' => array('2.8'))
146 $response->headers->setCookie(new Cookie('city', 'Lille', new \DateTime('Wed, 13 Jan 2021 22:23:01 GMT')));
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'));
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]);
159 public function testCreateResponseFromStreamed()
161 $response = new StreamedResponse(function () {
169 $psrResponse = $this->factory->createResponse($response);
171 $this->assertEquals("Line 1\nLine 2\n", $psrResponse->getBody()->__toString());
174 public function testCreateResponseFromBinaryFile()
176 $path = tempnam($this->tmpDir, uniqid());
177 file_put_contents($path, 'Binary');
179 $response = new BinaryFileResponse($path);
181 $psrResponse = $this->factory->createResponse($response);
183 $this->assertEquals('Binary', $psrResponse->getBody()->__toString());
186 public function testUploadErrNoFile()
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());
194 $request = new Request(array(), array(), array(), array(),
197 'f2' => array('name' => null, 'type' => null, 'tmp_name' => null, 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0),
200 'REQUEST_METHOD' => 'POST',
201 'HTTP_HOST' => 'dunglas.fr',
202 'HTTP_X_SYMFONY' => '2.8',
207 $psrRequest = $this->factory->createRequest($request);
209 $uploadedFiles = $psrRequest->getUploadedFiles();
211 $this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f1']->getError());
212 $this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f2']->getError());