namespace Symfony\Component\HttpKernel\Tests\DataCollector;
use PHPUnit\Framework\TestCase;
+use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\EventDispatcher\EventDispatcher;
-use Symfony\Component\VarDumper\Cloner\Data;
-use Symfony\Component\VarDumper\Cloner\VarCloner;
class RequestDataCollectorTest extends TestCase
{
$c = new RequestDataCollector();
$c->collect($request = $this->createRequest(), $this->createResponse());
+ $c->lateCollect();
- $cloner = new VarCloner();
$attributes = $c->getRequestAttributes();
$this->assertSame('request', $c->getName());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $attributes);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestRequest());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestQuery());
+ $this->assertInstanceOf(ParameterBag::class, $c->getResponseCookies());
$this->assertSame('html', $c->getFormat());
$this->assertEquals('foobar', $c->getRoute());
- $this->assertEquals(array('name' => $cloner->cloneVar(array('name' => 'foo'))->seek('name')), $c->getRouteParams());
+ $this->assertEquals(array('name' => 'foo'), $c->getRouteParams());
$this->assertSame(array(), $c->getSessionAttributes());
$this->assertSame('en', $c->getLocale());
- $this->assertEquals($cloner->cloneVar($request->attributes->get('resource')), $attributes->get('resource'));
- $this->assertEquals($cloner->cloneVar($request->attributes->get('object')), $attributes->get('object'));
+ $this->assertContains(__FILE__, $attributes->get('resource'));
+ $this->assertSame('stdClass', $attributes->get('object')->getType());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getResponseHeaders());
$this->assertSame('OK', $c->getStatusText());
$c = new RequestDataCollector();
$c->collect($request, $this->createResponse());
+ $c->lateCollect();
$this->assertEquals(array(), $c->getRouteParams());
}
- public function testKernelResponseDoesNotStartSession()
- {
- $kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
- $request = new Request();
- $session = new Session(new MockArraySessionStorage());
- $request->setSession($session);
- $response = new Response();
-
- $c = new RequestDataCollector();
- $c->onKernelResponse(new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response));
-
- $this->assertFalse($session->isStarted());
- }
-
/**
* @dataProvider provideControllerCallables
*/
$response = $this->createResponse();
$this->injectController($c, $callable, $request);
$c->collect($request, $response);
+ $c->lateCollect();
- $this->assertSame($expected, $c->getController(), sprintf('Testing: %s', $name));
+ $this->assertSame($expected, $c->getController()->getValue(true), sprintf('Testing: %s', $name));
}
public function provideControllerCallables()
$this->assertSame('n/a', $c->getController());
}
+ public function testItAddsRedirectedAttributesWhenRequestContainsSpecificCookie()
+ {
+ $request = $this->createRequest();
+ $request->cookies->add(array(
+ 'sf_redirect' => '{}',
+ ));
+
+ $kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
+
+ $c = new RequestDataCollector();
+ $c->onKernelResponse(new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $this->createResponse()));
+
+ $this->assertTrue($request->attributes->get('_redirected'));
+ }
+
+ public function testItSetsARedirectCookieIfTheResponseIsARedirection()
+ {
+ $c = new RequestDataCollector();
+
+ $response = $this->createResponse();
+ $response->setStatusCode(302);
+ $response->headers->set('Location', '/somewhere-else');
+
+ $c->collect($request = $this->createRequest(), $response);
+ $c->lateCollect();
+
+ $cookie = $this->getCookieByName($response, 'sf_redirect');
+
+ $this->assertNotEmpty($cookie->getValue());
+ }
+
+ public function testItCollectsTheRedirectionAndClearTheCookie()
+ {
+ $c = new RequestDataCollector();
+
+ $request = $this->createRequest();
+ $request->attributes->set('_redirected', true);
+ $request->cookies->add(array(
+ 'sf_redirect' => '{"method": "POST"}',
+ ));
+
+ $c->collect($request, $response = $this->createResponse());
+ $c->lateCollect();
+
+ $this->assertEquals('POST', $c->getRedirect()['method']);
+
+ $cookie = $this->getCookieByName($response, 'sf_redirect');
+ $this->assertNull($cookie->getValue());
+ }
+
protected function createRequest($routeParams = array('name' => 'foo'))
{
$request = Request::create('http://test.com/foo?bar=baz');
{
throw new \LogicException('Unexpected method call');
}
+
+ private function getCookieByName(Response $response, $name)
+ {
+ foreach ($response->headers->getCookies() as $cookie) {
+ if ($cookie->getName() == $name) {
+ return $cookie;
+ }
+ }
+
+ throw new \InvalidArgumentException(sprintf('Cookie named "%s" is not in response', $name));
+ }
}