HttpFoundationFactoryTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 Psr\Http\Message\UploadedFileInterface;
  12. use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
  13. use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Response;
  14. use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\ServerRequest;
  15. use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Stream;
  16. use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\UploadedFile;
  17. use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Uri;
  18. /**
  19. * @author Kévin Dunglas <dunglas@gmail.com>
  20. */
  21. class HttpFoundationFactoryTest extends \PHPUnit_Framework_TestCase
  22. {
  23. /** @var HttpFoundationFactory */
  24. private $factory;
  25. /** @var string */
  26. private $tmpDir;
  27. public function setup()
  28. {
  29. $this->factory = new HttpFoundationFactory();
  30. $this->tmpDir = sys_get_temp_dir();
  31. }
  32. public function testCreateRequest()
  33. {
  34. $stdClass = new \stdClass();
  35. $serverRequest = new ServerRequest(
  36. '1.1',
  37. array(
  38. 'X-Dunglas-API-Platform' => '1.0',
  39. 'X-data' => array('a', 'b'),
  40. ),
  41. new Stream('The body'),
  42. '/about/kevin',
  43. 'GET',
  44. 'http://les-tilleuls.coop/about/kevin',
  45. array('country' => 'France'),
  46. array('city' => 'Lille'),
  47. array('url' => 'http://les-tilleuls.coop'),
  48. array(
  49. 'doc1' => $this->createUploadedFile('Doc 1', UPLOAD_ERR_OK, 'doc1.txt', 'text/plain'),
  50. 'nested' => array(
  51. 'docs' => array(
  52. $this->createUploadedFile('Doc 2', UPLOAD_ERR_OK, 'doc2.txt', 'text/plain'),
  53. $this->createUploadedFile('Doc 3', UPLOAD_ERR_OK, 'doc3.txt', 'text/plain'),
  54. ),
  55. ),
  56. ),
  57. array('url' => 'http://dunglas.fr'),
  58. array('custom' => $stdClass)
  59. );
  60. $symfonyRequest = $this->factory->createRequest($serverRequest);
  61. $files = $symfonyRequest->files->all();
  62. $this->assertEquals('http://les-tilleuls.coop', $symfonyRequest->query->get('url'));
  63. $this->assertEquals('doc1.txt', $files['doc1']->getClientOriginalName());
  64. $this->assertEquals('doc2.txt', $files['nested']['docs'][0]->getClientOriginalName());
  65. $this->assertEquals('doc3.txt', $files['nested']['docs'][1]->getClientOriginalName());
  66. $this->assertEquals('http://dunglas.fr', $symfonyRequest->request->get('url'));
  67. $this->assertEquals($stdClass, $symfonyRequest->attributes->get('custom'));
  68. $this->assertEquals('Lille', $symfonyRequest->cookies->get('city'));
  69. $this->assertEquals('France', $symfonyRequest->server->get('country'));
  70. $this->assertEquals('The body', $symfonyRequest->getContent());
  71. $this->assertEquals('1.0', $symfonyRequest->headers->get('X-Dunglas-API-Platform'));
  72. $this->assertEquals(array('a', 'b'), $symfonyRequest->headers->get('X-data', null, false));
  73. }
  74. public function testCreateRequestWithNullParsedBody()
  75. {
  76. $serverRequest = new ServerRequest(
  77. '1.1',
  78. array(),
  79. new Stream(),
  80. '/',
  81. 'GET',
  82. null,
  83. array(),
  84. array(),
  85. array(),
  86. array(),
  87. null,
  88. array()
  89. );
  90. $this->assertCount(0, $this->factory->createRequest($serverRequest)->request);
  91. }
  92. public function testCreateRequestWithObjectParsedBody()
  93. {
  94. $serverRequest = new ServerRequest(
  95. '1.1',
  96. array(),
  97. new Stream(),
  98. '/',
  99. 'GET',
  100. null,
  101. array(),
  102. array(),
  103. array(),
  104. array(),
  105. new \stdClass(),
  106. array()
  107. );
  108. $this->assertCount(0, $this->factory->createRequest($serverRequest)->request);
  109. }
  110. public function testCreateRequestWithUri()
  111. {
  112. $serverRequest = new ServerRequest(
  113. '1.1',
  114. array(),
  115. new Stream(),
  116. '/',
  117. 'GET',
  118. new Uri('http://les-tilleuls.coop/about/kevin'),
  119. array(),
  120. array(),
  121. array(),
  122. array(),
  123. null,
  124. array()
  125. );
  126. $this->assertEquals('/about/kevin', $this->factory->createRequest($serverRequest)->getPathInfo());
  127. }
  128. public function testCreateUploadedFile()
  129. {
  130. $uploadedFile = $this->createUploadedFile('An uploaded file.', UPLOAD_ERR_OK, 'myfile.txt', 'text/plain');
  131. $symfonyUploadedFile = $this->callCreateUploadedFile($uploadedFile);
  132. $uniqid = uniqid();
  133. $symfonyUploadedFile->move($this->tmpDir, $uniqid);
  134. $this->assertEquals($uploadedFile->getSize(), $symfonyUploadedFile->getClientSize());
  135. $this->assertEquals(UPLOAD_ERR_OK, $symfonyUploadedFile->getError());
  136. $this->assertEquals('myfile.txt', $symfonyUploadedFile->getClientOriginalName());
  137. $this->assertEquals('txt', $symfonyUploadedFile->getClientOriginalExtension());
  138. $this->assertEquals('text/plain', $symfonyUploadedFile->getClientMimeType());
  139. $this->assertEquals('An uploaded file.', file_get_contents($this->tmpDir.'/'.$uniqid));
  140. }
  141. /**
  142. * @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileException
  143. * @expectedExceptionMessage The file "e" could not be written on disk.
  144. */
  145. public function testCreateUploadedFileWithError()
  146. {
  147. $uploadedFile = $this->createUploadedFile('Error.', UPLOAD_ERR_CANT_WRITE, 'e', 'text/plain');
  148. $symfonyUploadedFile = $this->callCreateUploadedFile($uploadedFile);
  149. $this->assertEquals(UPLOAD_ERR_CANT_WRITE, $symfonyUploadedFile->getError());
  150. $symfonyUploadedFile->move($this->tmpDir, 'shouldFail.txt');
  151. }
  152. private function createUploadedFile($content, $error, $clientFileName, $clientMediaType)
  153. {
  154. $filePath = tempnam($this->tmpDir, uniqid());
  155. file_put_contents($filePath, $content);
  156. return new UploadedFile($filePath, filesize($filePath), $error, $clientFileName, $clientMediaType);
  157. }
  158. private function callCreateUploadedFile(UploadedFileInterface $uploadedFile)
  159. {
  160. $reflection = new \ReflectionClass($this->factory);
  161. $createUploadedFile = $reflection->getMethod('createUploadedFile');
  162. $createUploadedFile->setAccessible(true);
  163. return $createUploadedFile->invokeArgs($this->factory, array($uploadedFile));
  164. }
  165. public function testCreateResponse()
  166. {
  167. $response = new Response(
  168. '1.0',
  169. array(
  170. 'X-Symfony' => array('2.8'),
  171. 'Set-Cookie' => array(
  172. 'theme=light',
  173. 'test',
  174. 'ABC=AeD; Domain=dunglas.fr; Path=/kevin; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Secure; HttpOnly',
  175. ),
  176. ),
  177. new Stream('The response body'),
  178. 200
  179. );
  180. $symfonyResponse = $this->factory->createResponse($response);
  181. $this->assertEquals('1.0', $symfonyResponse->getProtocolVersion());
  182. $this->assertEquals('2.8', $symfonyResponse->headers->get('X-Symfony'));
  183. $cookies = $symfonyResponse->headers->getCookies();
  184. $this->assertEquals('theme', $cookies[0]->getName());
  185. $this->assertEquals('light', $cookies[0]->getValue());
  186. $this->assertEquals(0, $cookies[0]->getExpiresTime());
  187. $this->assertNull($cookies[0]->getDomain());
  188. $this->assertEquals('/', $cookies[0]->getPath());
  189. $this->assertFalse($cookies[0]->isSecure());
  190. $this->assertFalse($cookies[0]->isHttpOnly());
  191. $this->assertEquals('test', $cookies[1]->getName());
  192. $this->assertNull($cookies[1]->getValue());
  193. $this->assertEquals('ABC', $cookies[2]->getName());
  194. $this->assertEquals('AeD', $cookies[2]->getValue());
  195. $this->assertEquals(strtotime('Wed, 13 Jan 2021 22:23:01 GMT'), $cookies[2]->getExpiresTime());
  196. $this->assertEquals('dunglas.fr', $cookies[2]->getDomain());
  197. $this->assertEquals('/kevin', $cookies[2]->getPath());
  198. $this->assertTrue($cookies[2]->isSecure());
  199. $this->assertTrue($cookies[2]->isHttpOnly());
  200. $this->assertEquals('The response body', $symfonyResponse->getContent());
  201. $this->assertEquals(200, $symfonyResponse->getStatusCode());
  202. }
  203. }