[HttpKernel] Add unit tests for Kernel. Also slightly modify Kernel to make it more testable.
This commit is contained in:
parent
c251a36935
commit
9ba2943aff
@ -97,7 +97,7 @@ abstract class Kernel implements KernelInterface
|
||||
// init container
|
||||
$this->initializeContainer();
|
||||
|
||||
foreach ($this->bundles as $bundle) {
|
||||
foreach ($this->getBundles() as $bundle) {
|
||||
$bundle->setContainer($this->container);
|
||||
$bundle->boot();
|
||||
}
|
||||
@ -114,7 +114,7 @@ abstract class Kernel implements KernelInterface
|
||||
{
|
||||
$this->booted = false;
|
||||
|
||||
foreach ($this->bundles as $bundle) {
|
||||
foreach ($this->getBundles() as $bundle) {
|
||||
$bundle->shutdown();
|
||||
$bundle->setContainer(null);
|
||||
}
|
||||
@ -131,7 +131,17 @@ abstract class Kernel implements KernelInterface
|
||||
$this->boot();
|
||||
}
|
||||
|
||||
return $this->container->get('http_kernel')->handle($request, $type, $catch);
|
||||
return $this->getHttpKernel()->handle($request, $type, $catch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a http kernel from the container
|
||||
*
|
||||
* @return HttpKernel
|
||||
*/
|
||||
protected function getHttpKernel()
|
||||
{
|
||||
return $this->container->get('http_kernel');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -14,9 +14,191 @@ namespace Symfony\Tests\Component\HttpKernel;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
|
||||
use Symfony\Component\DependencyInjection\Loader\LoaderInterface;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class KernelTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
$env = 'test_env';
|
||||
$debug = true;
|
||||
$kernel = new KernelForTest($env, $debug);
|
||||
|
||||
$this->assertEquals($env, $kernel->getEnvironment());
|
||||
$this->assertEquals($debug, $kernel->isDebug());
|
||||
$this->assertFalse($kernel->isBooted());
|
||||
$this->assertLessThanOrEqual(microtime(true), $kernel->getStartTime());
|
||||
$this->assertNull($kernel->getContainer());
|
||||
}
|
||||
|
||||
public function testClone()
|
||||
{
|
||||
$env = 'test_env';
|
||||
$debug = true;
|
||||
$kernel = new KernelForTest($env, $debug);
|
||||
|
||||
$clone = clone $kernel;
|
||||
|
||||
$this->assertEquals($env, $clone->getEnvironment());
|
||||
$this->assertEquals($debug, $clone->isDebug());
|
||||
$this->assertFalse($clone->isBooted());
|
||||
$this->assertLessThanOrEqual(microtime(true), $clone->getStartTime());
|
||||
$this->assertNull($clone->getContainer());
|
||||
}
|
||||
|
||||
public function testBootInitializesBundlesAndContainer()
|
||||
{
|
||||
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('initializeBundles', 'initializeContainer', 'getBundles'))
|
||||
->getMock();
|
||||
$kernel->expects($this->once())
|
||||
->method('initializeBundles');
|
||||
$kernel->expects($this->once())
|
||||
->method('initializeContainer');
|
||||
$kernel->expects($this->once())
|
||||
->method('getBundles')
|
||||
->will($this->returnValue(array()));
|
||||
|
||||
$kernel->boot();
|
||||
}
|
||||
|
||||
public function testBootSetsTheContainerToTheBundles()
|
||||
{
|
||||
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$bundle->expects($this->once())
|
||||
->method('setContainer');
|
||||
|
||||
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('initializeBundles', 'initializeContainer', 'getBundles'))
|
||||
->getMock();
|
||||
$kernel->expects($this->once())
|
||||
->method('getBundles')
|
||||
->will($this->returnValue(array($bundle)));
|
||||
|
||||
$kernel->boot();
|
||||
}
|
||||
|
||||
public function testBootSetsTheBootedFlagToTrue()
|
||||
{
|
||||
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('initializeBundles', 'initializeContainer', 'getBundles'))
|
||||
->getMock();
|
||||
$kernel->expects($this->once())
|
||||
->method('getBundles')
|
||||
->will($this->returnValue(array()));
|
||||
|
||||
$kernel->boot();
|
||||
|
||||
$this->assertTrue($kernel->isBooted());
|
||||
}
|
||||
|
||||
public function testShutdownCallsShutdownOnAllBundles()
|
||||
{
|
||||
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$bundle->expects($this->once())
|
||||
->method('shutdown');
|
||||
|
||||
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('getBundles'))
|
||||
->getMock();
|
||||
$kernel->expects($this->once())
|
||||
->method('getBundles')
|
||||
->will($this->returnValue(array($bundle)));
|
||||
|
||||
$kernel->shutdown();
|
||||
}
|
||||
|
||||
public function testShutdownGivesNullContainerToAllBundles()
|
||||
{
|
||||
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$bundle->expects($this->once())
|
||||
->method('setContainer')
|
||||
->with(null);
|
||||
|
||||
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('getBundles'))
|
||||
->getMock();
|
||||
$kernel->expects($this->once())
|
||||
->method('getBundles')
|
||||
->will($this->returnValue(array($bundle)));
|
||||
|
||||
$kernel->shutdown();
|
||||
}
|
||||
|
||||
public function testHandleCallsHandleOnHttpKernel()
|
||||
{
|
||||
$type = HttpKernelInterface::MASTER_REQUEST;
|
||||
$catch = true;
|
||||
$request = new Request();
|
||||
|
||||
$httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$httpKernelMock
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($request, $type, $catch);
|
||||
|
||||
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('getHttpKernel'))
|
||||
->getMock();
|
||||
|
||||
$kernel->expects($this->once())
|
||||
->method('getHttpKernel')
|
||||
->will($this->returnValue($httpKernelMock));
|
||||
|
||||
$kernel->handle($request, $type, $catch);
|
||||
}
|
||||
|
||||
public function testStripComments()
|
||||
{
|
||||
if (!function_exists('token_get_all')) {
|
||||
$this->markTestSkipped();
|
||||
return;
|
||||
}
|
||||
$source = <<<EOF
|
||||
<?php
|
||||
|
||||
/**
|
||||
* some class comments to strip
|
||||
*/
|
||||
class TestClass
|
||||
{
|
||||
/**
|
||||
* some method comments to strip
|
||||
*/
|
||||
public function doStuff()
|
||||
{
|
||||
// inline comment
|
||||
}
|
||||
}
|
||||
EOF;
|
||||
$expected = <<<EOF
|
||||
<?php
|
||||
class TestClass
|
||||
{
|
||||
public function doStuff()
|
||||
{
|
||||
}
|
||||
}
|
||||
EOF;
|
||||
|
||||
$this->assertEquals($expected, Kernel::stripComments($source));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
@ -333,6 +515,11 @@ class KernelForTest extends Kernel
|
||||
{
|
||||
parent::initializeBundles();
|
||||
}
|
||||
|
||||
public function isBooted()
|
||||
{
|
||||
return $this->booted;
|
||||
}
|
||||
}
|
||||
|
||||
abstract class BundleForTest implements BundleInterface
|
||||
|
Reference in New Issue
Block a user