DiactorosFactoryTest.php 7.9 KB

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