CovertTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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\Functional;
  11. use Nyholm\Psr7\Factory\Psr17Factory;
  12. use Nyholm\Psr7\Response as Psr7Response;
  13. use Nyholm\Psr7\ServerRequest as Psr7Request;
  14. use Nyholm\Psr7\Stream as Psr7Stream;
  15. use PHPUnit\Framework\TestCase;
  16. use Psr\Http\Message\ResponseInterface;
  17. use Psr\Http\Message\ServerRequestInterface;
  18. use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
  19. use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
  20. use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
  21. use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
  22. use Symfony\Component\HttpFoundation\Cookie;
  23. use Symfony\Component\HttpFoundation\File\UploadedFile;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. /**
  27. * Test to convert a request/response back and forth to make sure we do not loose data.
  28. *
  29. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  30. */
  31. class CovertTest extends TestCase
  32. {
  33. private $tmpDir;
  34. public function setUp(): void
  35. {
  36. if (!class_exists('Nyholm\Psr7\ServerRequest')) {
  37. $this->markTestSkipped('nyholm/psr7 is not installed.');
  38. }
  39. $this->tmpDir = sys_get_temp_dir();
  40. }
  41. /**
  42. * @dataProvider requestProvider
  43. *
  44. * @param Request|ServerRequestInterface $request
  45. * @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $firstFactory
  46. * @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $secondFactory
  47. */
  48. public function testConvertRequestMultipleTimes($request, $firstFactory, $secondFactory)
  49. {
  50. $temporaryRequest = $firstFactory->createRequest($request);
  51. $finalRequest = $secondFactory->createRequest($temporaryRequest);
  52. if ($finalRequest instanceof Request) {
  53. $this->assertEquals($request->getBasePath(), $finalRequest->getBasePath());
  54. $this->assertEquals($request->getBaseUrl(), $finalRequest->getBaseUrl());
  55. $this->assertEquals($request->getContent(), $finalRequest->getContent());
  56. $this->assertEquals($request->getEncodings(), $finalRequest->getEncodings());
  57. $this->assertEquals($request->getETags(), $finalRequest->getETags());
  58. $this->assertEquals($request->getHost(), $finalRequest->getHost());
  59. $this->assertEquals($request->getHttpHost(), $finalRequest->getHttpHost());
  60. $this->assertEquals($request->getMethod(), $finalRequest->getMethod());
  61. $this->assertEquals($request->getPassword(), $finalRequest->getPassword());
  62. $this->assertEquals($request->getPathInfo(), $finalRequest->getPathInfo());
  63. $this->assertEquals($request->getPort(), $finalRequest->getPort());
  64. $this->assertEquals($request->getProtocolVersion(), $finalRequest->getProtocolVersion());
  65. $this->assertEquals($request->getQueryString(), $finalRequest->getQueryString());
  66. $this->assertEquals($request->getRequestUri(), $finalRequest->getRequestUri());
  67. $this->assertEquals($request->getScheme(), $finalRequest->getScheme());
  68. $this->assertEquals($request->getSchemeAndHttpHost(), $finalRequest->getSchemeAndHttpHost());
  69. $this->assertEquals($request->getScriptName(), $finalRequest->getScriptName());
  70. $this->assertEquals($request->getUri(), $finalRequest->getUri());
  71. $this->assertEquals($request->getUser(), $finalRequest->getUser());
  72. $this->assertEquals($request->getUserInfo(), $finalRequest->getUserInfo());
  73. } elseif ($finalRequest instanceof ServerRequestInterface) {
  74. $strToLower = function ($arr) {
  75. foreach ($arr as $key => $value) {
  76. yield strtolower($key) => $value;
  77. }
  78. };
  79. $this->assertEquals($request->getAttributes(), $finalRequest->getAttributes());
  80. $this->assertEquals($request->getCookieParams(), $finalRequest->getCookieParams());
  81. $this->assertEquals((array) $request->getParsedBody(), (array) $finalRequest->getParsedBody());
  82. $this->assertEquals($request->getQueryParams(), $finalRequest->getQueryParams());
  83. // PSR7 does not define a "withServerParams" so this is impossible to implement without knowing the PSR7 implementation.
  84. //$this->assertEquals($request->getServerParams(), $finalRequest->getServerParams());
  85. $this->assertEquals($request->getUploadedFiles(), $finalRequest->getUploadedFiles());
  86. $this->assertEquals($request->getMethod(), $finalRequest->getMethod());
  87. $this->assertEquals($request->getRequestTarget(), $finalRequest->getRequestTarget());
  88. $this->assertEquals((string) $request->getUri(), (string) $finalRequest->getUri());
  89. $this->assertEquals((string) $request->getBody(), (string) $finalRequest->getBody());
  90. $this->assertEquals($strToLower($request->getHeaders()), $strToLower($finalRequest->getHeaders()));
  91. $this->assertEquals($request->getProtocolVersion(), $finalRequest->getProtocolVersion());
  92. } else {
  93. $this->fail('$finalRequest must be an instance of PSR7 or a HTTPFoundation request');
  94. }
  95. }
  96. public function requestProvider()
  97. {
  98. $sfRequest = new Request(
  99. [
  100. 'foo' => '1',
  101. 'bar' => ['baz' => '42'],
  102. ],
  103. [
  104. 'twitter' => [
  105. '@dunglas' => 'Kévin Dunglas',
  106. '@coopTilleuls' => 'Les-Tilleuls.coop',
  107. ],
  108. 'baz' => '2',
  109. ],
  110. [
  111. 'a2' => ['foo' => 'bar'],
  112. ],
  113. [
  114. 'c1' => 'foo',
  115. 'c2' => ['c3' => 'bar'],
  116. ],
  117. [
  118. 'f1' => $this->createUploadedFile('F1', 'f1.txt', 'text/plain', UPLOAD_ERR_OK),
  119. 'foo' => ['f2' => $this->createUploadedFile('F2', 'f2.txt', 'text/plain', UPLOAD_ERR_OK)],
  120. ],
  121. [
  122. 'REQUEST_METHOD' => 'POST',
  123. 'HTTP_HOST' => 'dunglas.fr',
  124. 'SERVER_NAME' => 'dunglas.fr',
  125. 'SERVER_PORT' => null,
  126. 'HTTP_X_SYMFONY' => '2.8',
  127. 'REQUEST_URI' => '/testCreateRequest?bar[baz]=42&foo=1',
  128. 'QUERY_STRING' => 'foo=1&bar[baz]=42',
  129. ],
  130. 'Content'
  131. );
  132. $psr7Request = (new Psr7Request('POST', 'http://tnyholm.se/foo/?bar=biz'))
  133. ->withQueryParams(['bar' => 'biz']);
  134. $nyholmFactory = new Psr17Factory();
  135. $psr17Factory = new PsrHttpFactory($nyholmFactory, $nyholmFactory, $nyholmFactory, $nyholmFactory);
  136. $symfonyFactory = new HttpFoundationFactory();
  137. return [
  138. [$sfRequest, $psr17Factory, $symfonyFactory],
  139. [$psr7Request, $symfonyFactory, $psr17Factory],
  140. ];
  141. }
  142. /**
  143. * @dataProvider responseProvider
  144. *
  145. * @param Response|ResponseInterface $response
  146. * @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $firstFactory
  147. * @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $secondFactory
  148. */
  149. public function testConvertResponseMultipleTimes($response, $firstFactory, $secondFactory)
  150. {
  151. $temporaryResponse = $firstFactory->createResponse($response);
  152. $finalResponse = $secondFactory->createResponse($temporaryResponse);
  153. if ($finalResponse instanceof Response) {
  154. $this->assertEquals($response->getAge(), $finalResponse->getAge());
  155. $this->assertEquals($response->getCharset(), $finalResponse->getCharset());
  156. $this->assertEquals($response->getContent(), $finalResponse->getContent());
  157. $this->assertEquals($response->getDate(), $finalResponse->getDate());
  158. $this->assertEquals($response->getEtag(), $finalResponse->getEtag());
  159. $this->assertEquals($response->getExpires(), $finalResponse->getExpires());
  160. $this->assertEquals($response->getLastModified(), $finalResponse->getLastModified());
  161. $this->assertEquals($response->getMaxAge(), $finalResponse->getMaxAge());
  162. $this->assertEquals($response->getProtocolVersion(), $finalResponse->getProtocolVersion());
  163. $this->assertEquals($response->getStatusCode(), $finalResponse->getStatusCode());
  164. $this->assertEquals($response->getTtl(), $finalResponse->getTtl());
  165. } elseif ($finalResponse instanceof ResponseInterface) {
  166. $strToLower = function ($arr) {
  167. foreach ($arr as $key => $value) {
  168. yield strtolower($key) => $value;
  169. }
  170. };
  171. $this->assertEquals($response->getStatusCode(), $finalResponse->getStatusCode());
  172. $this->assertEquals($response->getReasonPhrase(), $finalResponse->getReasonPhrase());
  173. $this->assertEquals((string) $response->getBody(), (string) $finalResponse->getBody());
  174. $this->assertEquals($strToLower($response->getHeaders()), $strToLower($finalResponse->getHeaders()));
  175. $this->assertEquals($response->getProtocolVersion(), $finalResponse->getProtocolVersion());
  176. } else {
  177. $this->fail('$finalResponse must be an instance of PSR7 or a HTTPFoundation response');
  178. }
  179. }
  180. public function responseProvider()
  181. {
  182. $sfResponse = new Response(
  183. 'Response content.',
  184. 202,
  185. ['x-symfony' => ['3.4']]
  186. );
  187. if (method_exists(Cookie::class, 'create')) {
  188. $cookie = Cookie::create('city', 'Lille', new \DateTime('Wed, 13 Jan 2021 22:23:01 GMT'));
  189. } else {
  190. $cookie = new Cookie('city', 'Lille', new \DateTime('Wed, 13 Jan 2021 22:23:01 GMT'));
  191. }
  192. $sfResponse->headers->setCookie($cookie);
  193. $body = Psr7Stream::create();
  194. $status = 302;
  195. $headers = [
  196. 'location' => ['http://example.com/'],
  197. ];
  198. $zendResponse = new Psr7Response($status, $headers, $body);
  199. $nyholmFactory = new Psr17Factory();
  200. $psr17Factory = new PsrHttpFactory($nyholmFactory, $nyholmFactory, $nyholmFactory, $nyholmFactory);
  201. $symfonyFactory = new HttpFoundationFactory();
  202. return [
  203. [$sfResponse, $psr17Factory, $symfonyFactory],
  204. [$zendResponse, $symfonyFactory, $psr17Factory],
  205. ];
  206. }
  207. private function createUploadedFile($content, $originalName, $mimeType, $error)
  208. {
  209. $path = tempnam($this->tmpDir, uniqid());
  210. file_put_contents($path, $content);
  211. return new UploadedFile($path, $originalName, $mimeType, $error, true);
  212. }
  213. }