DiactorosFactoryTest.php 7.5 KB

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