8b62df7247
The three notification methods do not return the Event instance anymore. notify() does not return anything notifyUntil() returns the returned value of the event that has processed the event filter() returns the filtered value Upgrading your listeners: Listeners for notify() and filter() events: nothing to change Listeners for notifyUntil() events: Before: $event->setReturnValue('foo'); return true; After: $event->setProcessed(); return 'foo'; If you notify events, the processing also need to be changed: For filter() notifications: the filtered value is now available as the returned value of the filter() method. For notifyUntil() notifications: Before: $event = $dispatcher->notifyUntil($event); if ($event->isProcessed()) { $ret = $event->getReturnValue(); // do something with $ret } After: $ret = $dispatcher->notifyUntil($event); if ($event->isProcessed()) { // do something with $ret }
176 lines
5.5 KiB
PHP
176 lines
5.5 KiB
PHP
<?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
|
|
{
|
|
/**
|
|
* @expectedException RuntimeException
|
|
*/
|
|
public function testHandleWhenControllerThrowsAnExceptionAndRawIsTrue()
|
|
{
|
|
$kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
|
|
|
|
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
|
|
}
|
|
|
|
/**
|
|
* @expectedException RuntimeException
|
|
*/
|
|
public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalseAndNoListenerIsRegistered()
|
|
{
|
|
$kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
|
|
|
|
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
|
|
}
|
|
|
|
public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalse()
|
|
{
|
|
$dispatcher = new EventDispatcher();
|
|
$dispatcher->connect('core.exception', function ($event)
|
|
{
|
|
$event->setProcessed();
|
|
|
|
return new Response($event->get('exception')->getMessage());
|
|
});
|
|
|
|
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); }));
|
|
|
|
$this->assertEquals('foo', $kernel->handle(new Request())->getContent());
|
|
}
|
|
|
|
public function testHandleWhenAListenerReturnsAResponse()
|
|
{
|
|
$dispatcher = new EventDispatcher();
|
|
$dispatcher->connect('core.request', function ($event)
|
|
{
|
|
$event->setProcessed();
|
|
|
|
return new Response('hello');
|
|
});
|
|
|
|
$kernel = new HttpKernel($dispatcher, $this->getResolver());
|
|
|
|
$this->assertEquals('hello', $kernel->handle(new Request())->getContent());
|
|
}
|
|
|
|
/**
|
|
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
|
*/
|
|
public function testHandleWhenNoControllerIsFound()
|
|
{
|
|
$dispatcher = new EventDispatcher();
|
|
$kernel = new HttpKernel($dispatcher, $this->getResolver(false));
|
|
|
|
$kernel->handle(new Request());
|
|
}
|
|
|
|
/**
|
|
* @expectedException LogicException
|
|
*/
|
|
public function testHandleWhenNoControllerIsNotACallable()
|
|
{
|
|
$dispatcher = new EventDispatcher();
|
|
$kernel = new HttpKernel($dispatcher, $this->getResolver('foobar'));
|
|
|
|
$kernel->handle(new Request());
|
|
}
|
|
|
|
/**
|
|
* @expectedException RuntimeException
|
|
*/
|
|
public function testHandleWhenControllerDoesNotReturnAResponse()
|
|
{
|
|
$dispatcher = new EventDispatcher();
|
|
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
|
|
|
|
$kernel->handle(new Request());
|
|
}
|
|
|
|
public function testHandleWhenControllerDoesNotReturnAResponseButAViewIsRegistered()
|
|
{
|
|
$dispatcher = new EventDispatcher();
|
|
$dispatcher->connect('core.view', function ($event, $retval)
|
|
{
|
|
return new Response($retval);
|
|
});
|
|
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
|
|
|
|
$this->assertEquals('foo', $kernel->handle(new Request())->getContent());
|
|
}
|
|
|
|
/**
|
|
* @expectedException RuntimeException
|
|
*/
|
|
public function testHandleWhenAViewDoesNotReturnAResponse()
|
|
{
|
|
$dispatcher = new EventDispatcher();
|
|
$dispatcher->connect('core.view', function ($event, $retval)
|
|
{
|
|
return $retval;
|
|
});
|
|
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
|
|
|
|
$kernel->handle(new Request());
|
|
}
|
|
|
|
/**
|
|
* @expectedException RuntimeException
|
|
*/
|
|
public function testHandleWhenAResponseListenerDoesNotReturnAResponse()
|
|
{
|
|
$dispatcher = new EventDispatcher();
|
|
$dispatcher->connect('core.response', function ($event, $response)
|
|
{
|
|
return 'foo';
|
|
});
|
|
$kernel = new HttpKernel($dispatcher, $this->getResolver());
|
|
|
|
$kernel->handle(new Request());
|
|
}
|
|
|
|
public function testHandleWithAResponseListener()
|
|
{
|
|
$dispatcher = new EventDispatcher();
|
|
$dispatcher->connect('core.response', function ($event, $response)
|
|
{
|
|
return new Response('foo');
|
|
});
|
|
$kernel = new HttpKernel($dispatcher, $this->getResolver());
|
|
|
|
$this->assertEquals('foo', $kernel->handle(new Request())->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;
|
|
}
|
|
}
|