Skip to content

Commit bbbc4af

Browse files
committed
Convert Request/Response multiple times
By doing this we make sure that we do not loose data.
1 parent 8592ca3 commit bbbc4af

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

Tests/Functional/CovertTest.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PsrHttpMessage\Tests\Functional;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Psr\Http\Message\RequestInterface;
16+
use Psr\Http\Message\ResponseInterface;
17+
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
18+
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
19+
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
20+
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
21+
use Symfony\Component\HttpFoundation\Cookie;
22+
use Symfony\Component\HttpFoundation\File\UploadedFile;
23+
use Symfony\Component\HttpFoundation\Request;
24+
use Symfony\Component\HttpFoundation\Response;
25+
use Zend\Diactoros\ServerRequest;
26+
use Zend\Diactoros\Response as ZendResponse;
27+
use Zend\Diactoros\Stream;
28+
use Zend\Diactoros\Uri;
29+
30+
/**
31+
* Test to convert a request/response back and forth to make sure we do not loose data.
32+
*
33+
* @author Tobias Nyholm <[email protected]>
34+
*/
35+
class CovertTest extends TestCase
36+
{
37+
private $tmpDir;
38+
39+
public function setup()
40+
{
41+
if (!class_exists('Zend\Diactoros\ServerRequestFactory')) {
42+
$this->markTestSkipped('Zend Diactoros is not installed.');
43+
}
44+
45+
$this->tmpDir = sys_get_temp_dir();
46+
}
47+
48+
/**
49+
* @dataProvider requestProvider
50+
*
51+
* @param Request|RequestInterface $request
52+
* @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $firstFactory
53+
* @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $secondFactory
54+
*/
55+
public function testConvertRequestMultipleTimes($request, $firstFactory, $secondFactory)
56+
{
57+
$temporaryRequest = $firstFactory->createRequest($request);
58+
$finalRequest = $secondFactory->createRequest($temporaryRequest);
59+
60+
$this->assertEquals($request, $finalRequest);
61+
}
62+
63+
public function requestProvider()
64+
{
65+
$sfRequest = new Request(
66+
array(
67+
'foo' => '1',
68+
'bar' => array('baz' => '42'),
69+
),
70+
array(
71+
'twitter' => array(
72+
'@dunglas' => 'Kévin Dunglas',
73+
'@coopTilleuls' => 'Les-Tilleuls.coop',
74+
),
75+
'baz' => '2',
76+
),
77+
array(
78+
'a2' => array('foo' => 'bar'),
79+
),
80+
array(
81+
'c1' => 'foo',
82+
'c2' => array('c3' => 'bar'),
83+
),
84+
array(
85+
'f1' => $this->createUploadedFile('F1', 'f1.txt', 'text/plain', UPLOAD_ERR_OK),
86+
'foo' => array('f2' => $this->createUploadedFile('F2', 'f2.txt', 'text/plain', UPLOAD_ERR_OK)),
87+
),
88+
array(
89+
'REQUEST_METHOD' => 'POST',
90+
'HTTP_HOST' => 'dunglas.fr',
91+
'HTTP_X_SYMFONY' => '2.8',
92+
'REQUEST_URI' => '/testCreateRequest?foo=1&bar[baz]=42',
93+
'QUERY_STRING' => 'foo=1&bar[baz]=42',
94+
),
95+
'Content'
96+
);
97+
98+
$zendRequest = new ServerRequest();
99+
$zendRequest = $zendRequest->withMethod("POST")
100+
->withUri(new Uri('http://tnyholm.se/foo/?bar=biz'));
101+
102+
103+
104+
$zendFactory = new DiactorosFactory();
105+
$symfonyFactory = new HttpFoundationFactory();
106+
return [
107+
[$sfRequest, $zendFactory, $symfonyFactory],
108+
[$zendRequest, $symfonyFactory, $zendFactory],
109+
];
110+
}
111+
112+
/**
113+
* @dataProvider responseProvider
114+
*
115+
* @param Response|ResponseInterface $response
116+
* @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $firstFactory
117+
* @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $secondFactory
118+
*/
119+
public function testConvertResponseMultipleTimes($response, $firstFactory, $secondFactory)
120+
{
121+
$temporaryResponse = $firstFactory->createResponse($response);
122+
$finalResponse = $secondFactory->createResponse($temporaryResponse);
123+
124+
$this->assertEquals($response, $finalResponse);
125+
}
126+
127+
public function responseProvider()
128+
{
129+
$sfResponse = new Response(
130+
'Response content.',
131+
202,
132+
array('X-Symfony' => array('3.4'))
133+
);
134+
$sfResponse->headers->setCookie(new Cookie('city', 'Lille', new \DateTime('Wed, 13 Jan 2021 22:23:01 GMT')));
135+
136+
$body = new Stream('php://memory');
137+
$status = 302;
138+
$headers = [
139+
'location' => [ 'http://example.com/' ],
140+
];
141+
$zendResponse = new ZendResponse($body, $status, $headers);
142+
143+
$zendFactory = new DiactorosFactory();
144+
$symfonyFactory = new HttpFoundationFactory();
145+
146+
return [
147+
[$sfResponse, $zendFactory, $symfonyFactory],
148+
[$zendResponse, $symfonyFactory, $zendFactory],
149+
];
150+
}
151+
152+
private function createUploadedFile($content, $originalName, $mimeType, $error)
153+
{
154+
$path = tempnam($this->tmpDir, uniqid());
155+
file_put_contents($path, $content);
156+
157+
if (class_exists('Symfony\Component\HttpFoundation\HeaderUtils')) {
158+
// Symfony 4.1+
159+
return new UploadedFile($path, $originalName, $mimeType, $error, true);
160+
}
161+
return new UploadedFile($path, $originalName, $mimeType, filesize($path), $error, true);
162+
}
163+
}

0 commit comments

Comments
 (0)