[HttpKernel] added some more unit tests
This commit is contained in:
parent
a19cdce1bc
commit
556bfcb804
@ -84,7 +84,8 @@ class BaseHttpKernel implements HttpKernelInterface
|
||||
}
|
||||
|
||||
// exception
|
||||
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e)));
|
||||
$event = new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e));
|
||||
$this->dispatcher->notifyUntil($event);
|
||||
if ($event->isProcessed()) {
|
||||
return $this->filterResponse($event->getReturnValue(), $request, 'A "core.exception" listener returned a non response object.', $type);
|
||||
}
|
||||
@ -109,7 +110,8 @@ class BaseHttpKernel implements HttpKernelInterface
|
||||
protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
|
||||
{
|
||||
// request
|
||||
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.request', array('request_type' => $type, 'request' => $request)));
|
||||
$event = new Event($this, 'core.request', array('request_type' => $type, 'request' => $request));
|
||||
$this->dispatcher->notifyUntil($event);
|
||||
if ($event->isProcessed()) {
|
||||
return $this->filterResponse($event->getReturnValue(), $request, 'A "core.request" listener returned a non response object.', $type);
|
||||
}
|
||||
@ -119,7 +121,8 @@ class BaseHttpKernel implements HttpKernelInterface
|
||||
throw new NotFoundHttpException('Unable to find the controller.');
|
||||
}
|
||||
|
||||
$event = $this->dispatcher->filter(new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request)), $controller);
|
||||
$event = new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request));
|
||||
$this->dispatcher->filter($event, $controller);
|
||||
$controller = $event->getReturnValue();
|
||||
|
||||
// controller must be a callable
|
||||
@ -134,7 +137,8 @@ class BaseHttpKernel implements HttpKernelInterface
|
||||
$retval = call_user_func_array($controller, $arguments);
|
||||
|
||||
// view
|
||||
$event = $this->dispatcher->filter(new Event($this, 'core.view', array('request_type' => $type, 'request' => $request)), $retval);
|
||||
$event = new Event($this, 'core.view', array('request_type' => $type, 'request' => $request));
|
||||
$this->dispatcher->filter($event, $retval);
|
||||
|
||||
return $this->filterResponse($event->getReturnValue(), $request, sprintf('The controller must return a response (instead of %s).', is_object($event->getReturnValue()) ? 'an object of class '.get_class($event->getReturnValue()) : is_array($event->getReturnValue()) ? 'an array' : str_replace("\n", '', var_export($event->getReturnValue(), true))), $type);
|
||||
}
|
||||
|
192
tests/Symfony/Tests/Component/HttpKernel/BaseHttpKernelTest.php
Normal file
192
tests/Symfony/Tests/Component/HttpKernel/BaseHttpKernelTest.php
Normal file
@ -0,0 +1,192 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Tests\Component\HttpKernel;
|
||||
|
||||
use Symfony\Component\HttpKernel\BaseHttpKernel;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
|
||||
class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testHandleChangingMasterRequest()
|
||||
{
|
||||
$kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver());
|
||||
|
||||
$kernel->handle();
|
||||
$this->assertInstanceof('Symfony\Component\HttpFoundation\Request', $kernel->getRequest());
|
||||
|
||||
$request = Request::create('/');
|
||||
$kernel->handle($request);
|
||||
$this->assertSame($request, $kernel->getRequest());
|
||||
|
||||
$subRequest = Request::create('/');
|
||||
$kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
|
||||
$this->assertSame($request, $kernel->getRequest());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException RuntimeException
|
||||
*/
|
||||
public function testHandleWhenControllerThrowsAnExceptionAndRawIsTrue()
|
||||
{
|
||||
$kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
|
||||
|
||||
$kernel->handle(null, HttpKernelInterface::MASTER_REQUEST, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException RuntimeException
|
||||
*/
|
||||
public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalseAndNoListenerIsRegistered()
|
||||
{
|
||||
$kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
|
||||
|
||||
$kernel->handle(null, HttpKernelInterface::MASTER_REQUEST, false);
|
||||
}
|
||||
|
||||
public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalse()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->connect('core.exception', function ($event)
|
||||
{
|
||||
$event->setReturnValue(new Response($event->getParameter('exception')->getMessage()));
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); }));
|
||||
|
||||
$this->assertEquals('foo', $kernel->handle()->getContent());
|
||||
}
|
||||
|
||||
public function testHandleWhenAListenerReturnsAResponse()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->connect('core.request', function ($event)
|
||||
{
|
||||
$event->setReturnValue(new Response('hello'));
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver());
|
||||
|
||||
$this->assertEquals('hello', $kernel->handle()->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||
*/
|
||||
public function testHandleWhenNoControllerIsFound()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver(false));
|
||||
|
||||
$kernel->handle();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException LogicException
|
||||
*/
|
||||
public function testHandleWhenNoControllerIsNotACallable()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver('foobar'));
|
||||
|
||||
$kernel->handle();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException RuntimeException
|
||||
*/
|
||||
public function testHandleWhenControllerDoesNotReturnAResponse()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
|
||||
|
||||
$kernel->handle();
|
||||
}
|
||||
|
||||
public function testHandleWhenControllerDoesNotReturnAResponseButAViewIsRegistered()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->connect('core.view', function ($event, $retval)
|
||||
{
|
||||
return new Response($retval);
|
||||
});
|
||||
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
|
||||
|
||||
$this->assertEquals('foo', $kernel->handle()->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException RuntimeException
|
||||
*/
|
||||
public function testHandleWhenAViewDoesNotReturnAResponse()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->connect('core.view', function ($event, $retval)
|
||||
{
|
||||
return $retval;
|
||||
});
|
||||
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
|
||||
|
||||
$kernel->handle();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException RuntimeException
|
||||
*/
|
||||
public function testHandleWhenAResponseListenerDoesNotReturnAResponse()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->connect('core.response', function ($event, $response)
|
||||
{
|
||||
return 'foo';
|
||||
});
|
||||
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver());
|
||||
|
||||
$kernel->handle();
|
||||
}
|
||||
|
||||
public function testHandleWithAResponseListener()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->connect('core.response', function ($event, $response)
|
||||
{
|
||||
return new Response('foo');
|
||||
});
|
||||
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver());
|
||||
|
||||
$this->assertEquals('foo', $kernel->handle()->getContent());
|
||||
}
|
||||
|
||||
protected function getResolver($controller = null)
|
||||
{
|
||||
if (null === $controller) {
|
||||
$controller = function () { return new Response('Hello'); };
|
||||
}
|
||||
$resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
|
||||
$resolver->expects($this->any())
|
||||
->method('getController')
|
||||
->will($this->returnValue($controller))
|
||||
;
|
||||
$resolver->expects($this->any())
|
||||
->method('getArguments')
|
||||
->will($this->returnValue(array()))
|
||||
;
|
||||
|
||||
return $resolver;
|
||||
}
|
||||
}
|
@ -32,6 +32,10 @@ class ControllerResolverTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertInstanceOf('Symfony\Tests\Component\HttpKernel\ControllerResolverTest', $controller[0], '->getController() returns a PHP callable');
|
||||
$this->assertEquals(array('Using controller "Symfony\Tests\Component\HttpKernel\ControllerResolverTest::testGetController"'), $logger->getLogs('info'));
|
||||
|
||||
$request->attributes->set('_controller', $lambda = function () {});
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertSame($lambda, $controller);
|
||||
|
||||
$request->attributes->set('_controller', 'foo');
|
||||
try {
|
||||
$resolver->getController($request);
|
||||
@ -78,6 +82,11 @@ class ControllerResolverTest extends \PHPUnit_Framework_TestCase
|
||||
$request->attributes->set('bar', 'bar');
|
||||
$this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes');
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$controller = function ($foo) {};
|
||||
$this->assertEquals(array('foo'), $resolver->getArguments($request, $controller));
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('foobar', 'foobar');
|
||||
|
69
tests/Symfony/Tests/Component/HttpKernel/HttpKernelTest.php
Normal file
69
tests/Symfony/Tests/Component/HttpKernel/HttpKernelTest.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Tests\Component\HttpKernel;
|
||||
|
||||
use Symfony\Component\HttpKernel\HttpKernel;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
|
||||
class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testHandleGetsTheRequestFromTheContainer()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
|
||||
$container->expects($this->any())
|
||||
->method('get')
|
||||
->will($this->returnValue($request))
|
||||
;
|
||||
|
||||
$kernel = new HttpKernel($container, new EventDispatcher(), $this->getResolver());
|
||||
|
||||
$kernel->handle();
|
||||
|
||||
$this->assertEquals($request, $kernel->getRequest());
|
||||
}
|
||||
|
||||
public function testHandleSetsTheRequestIfPassed()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
|
||||
$container->expects($this->exactly(2))
|
||||
->method('set')
|
||||
->with('request', $request)
|
||||
;
|
||||
|
||||
$kernel = new HttpKernel($container, new EventDispatcher(), $this->getResolver());
|
||||
|
||||
$kernel->handle($request);
|
||||
}
|
||||
|
||||
protected function getResolver($controller = null)
|
||||
{
|
||||
if (null === $controller) {
|
||||
$controller = function () { return new Response('Hello'); };
|
||||
}
|
||||
$resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
|
||||
$resolver->expects($this->any())
|
||||
->method('getController')
|
||||
->will($this->returnValue($controller))
|
||||
;
|
||||
$resolver->expects($this->any())
|
||||
->method('getArguments')
|
||||
->will($this->returnValue(array()))
|
||||
;
|
||||
|
||||
return $resolver;
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Tests\Component\HttpKernel;
|
||||
|
||||
use Symfony\Component\HttpKernel\ResponseListener;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
class ResponseListenerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testFilterDoesNothingForSubRequests()
|
||||
{
|
||||
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::SUB_REQUEST));
|
||||
$response = new Response('foo');
|
||||
|
||||
$this->assertEquals(array(), $this->getResponseListener()->filter($event, $response)->headers->all());
|
||||
}
|
||||
|
||||
public function testFilterDoesNothingIfContentTypeIsSet()
|
||||
{
|
||||
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST));
|
||||
$response = new Response('foo');
|
||||
$response->headers->set('Content-Type', 'text/plain');
|
||||
|
||||
$this->assertEquals(array('content-type' => array('text/plain')), $this->getResponseListener()->filter($event, $response)->headers->all());
|
||||
}
|
||||
|
||||
public function testFilterDoesNothingIfRequestFormatIsNotDefined()
|
||||
{
|
||||
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => Request::create('/')));
|
||||
$response = new Response('foo');
|
||||
|
||||
$this->assertEquals(array(), $this->getResponseListener()->filter($event, $response)->headers->all());
|
||||
}
|
||||
|
||||
public function testFilterSetContentType()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->setRequestFormat('json');
|
||||
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => $request));
|
||||
$response = new Response('foo');
|
||||
|
||||
$this->assertEquals(array('content-type' => array('application/json')), $this->getResponseListener()->filter($event, $response)->headers->all());
|
||||
}
|
||||
|
||||
protected function getResponseListener()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$listener = new ResponseListener();
|
||||
$listener->register($dispatcher);
|
||||
|
||||
return $listener;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user