Skip to content

Commit

Permalink
[HttpKernel] fixed serialization of the request data collector
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 3, 2014
1 parent 98c3fe7 commit 6102f99
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
Expand Up @@ -48,13 +48,20 @@ public function collect(Request $request, Response $response, \Exception $except
$responseHeaders['Set-Cookie'] = $cookies;
}

// attributes are serialized and as they can be anything, they need to be converted to strings.
$attributes = array();
foreach ($request->attributes->all() as $key => $value) {
if ('_route' === $key && is_object($value)) {
$value = $value->getPath();
$attributes[$key] = $this->varToString($value->getPath());
} elseif ('_route_params' === $key) {
// we need to keep route params as an array (see getRouteParams())
foreach ($value as $k => $v) {
$value[$k] = $this->varToString($v);
}
$attributes[$key] = $value;
} else {
$attributes[$key] = $this->varToString($value);
}

$attributes[$key] = $value;
}

$content = null;
Expand Down
Expand Up @@ -22,27 +22,28 @@

class RequestDataCollectorTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provider
*/
public function testCollect(Request $request, Response $response)
public function testCollect()
{
$c = new RequestDataCollector();

$c->collect($request, $response);
$c->collect($this->createRequest(), $this->createResponse());

$attributes = $c->getRequestAttributes();

$this->assertSame('request', $c->getName());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\HeaderBag', $c->getRequestHeaders());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestServer());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestCookies());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestAttributes());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $attributes);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestRequest());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestQuery());
$this->assertSame('html', $c->getFormat());
$this->assertSame('foobar', $c->getRoute());
$this->assertSame(array('name' => 'foo'), $c->getRouteParams());
$this->assertSame(array(), $c->getSessionAttributes());
$this->assertSame('en', $c->getLocale());
$this->assertSame('Resource(stream)', $attributes->get('resource'));
$this->assertSame('Object(stdClass)', $attributes->get('object'));

$this->assertInstanceOf('Symfony\Component\HttpFoundation\HeaderBag', $c->getResponseHeaders());
$this->assertSame('OK', $c->getStatusText());
Expand All @@ -52,10 +53,8 @@ public function testCollect(Request $request, Response $response)

/**
* Test various types of controller callables.
*
* @dataProvider provider
*/
public function testControllerInspection(Request $request, Response $response)
public function testControllerInspection()
{
// make sure we always match the line number
$r1 = new \ReflectionMethod($this, 'testControllerInspection');
Expand Down Expand Up @@ -136,35 +135,37 @@ function () { return 'foo'; },
);

$c = new RequestDataCollector();

$request = $this->createRequest();
$response = $this->createResponse();
foreach ($controllerTests as $controllerTest) {
$this->injectController($c, $controllerTest[1], $request);
$c->collect($request, $response);
$this->assertSame($controllerTest[2], $c->getController(), sprintf('Testing: %s', $controllerTest[0]));
}
}

public function provider()
protected function createRequest()
{
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
return array(array(null, null));
}

$request = Request::create('http://test.com/foo?bar=baz');
$request->attributes->set('foo', 'bar');
$request->attributes->set('_route', 'foobar');
$request->attributes->set('_route_params', array('name' => 'foo'));
$request->attributes->set('resource', fopen(__FILE__, 'r'));
$request->attributes->set('object', new \stdClass());

return $request;
}

protected function createResponse()
{
$response = new Response();
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/json');
$response->headers->setCookie(new Cookie('foo','bar',1,'/foo','localhost',true,true));
$response->headers->setCookie(new Cookie('bar','foo',new \DateTime('@946684800')));
$response->headers->setCookie(new Cookie('bazz','foo','2000-12-12'));

return array(
array($request, $response)
);
return $response;
}

/**
Expand Down

0 comments on commit 6102f99

Please sign in to comment.