Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HttpKernel] added some more unit tests
- Loading branch information
Showing
5 changed files
with
344 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 192 additions & 0 deletions
192
tests/Symfony/Tests/Component/HttpKernel/BaseHttpKernelTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
tests/Symfony/Tests/Component/HttpKernel/HttpKernelTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/Symfony/Tests/Component/HttpKernel/ResponseListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |