From 9ba2943aff2941c283065e0f09c05a572462fa50 Mon Sep 17 00:00:00 2001 From: ornicar Date: Sat, 5 Feb 2011 17:09:52 -0800 Subject: [PATCH] [HttpKernel] Add unit tests for Kernel. Also slightly modify Kernel to make it more testable. --- src/Symfony/Component/HttpKernel/Kernel.php | 22 +- .../Tests/Component/HttpKernel/KernelTest.php | 197 +++++++++++++++++- 2 files changed, 208 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index da64b790a7..5eb169931b 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -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'); } /** @@ -343,7 +353,7 @@ abstract class Kernel implements KernelInterface $this->bundles = array(); $topMostBundles = array(); $directChildren = array(); - + foreach ($this->registerBundles() as $bundle) { $name = $bundle->getName(); if (isset($this->bundles[$name])) { @@ -358,7 +368,7 @@ abstract class Kernel implements KernelInterface $directChildren[$parentName] = $name; } else { $topMostBundles[$name] = $bundle; - } + } } // look for orphans @@ -377,7 +387,7 @@ abstract class Kernel implements KernelInterface array_unshift($bundleMap, $this->bundles[$name]); $hierarchy[] = $name; } - + foreach ($hierarchy as $bundle) { $this->bundleMap[$bundle] = $bundleMap; array_pop($bundleMap); diff --git a/tests/Symfony/Tests/Component/HttpKernel/KernelTest.php b/tests/Symfony/Tests/Component/HttpKernel/KernelTest.php index a97eb74aeb..c6c11038d2 100644 --- a/tests/Symfony/Tests/Component/HttpKernel/KernelTest.php +++ b/tests/Symfony/Tests/Component/HttpKernel/KernelTest.php @@ -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 = <<assertEquals($expected, Kernel::stripComments($source)); + } + /** * @expectedException \InvalidArgumentException */ @@ -241,14 +423,14 @@ class KernelTest extends \PHPUnit_Framework_TestCase { $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName'); $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName'); - + $kernel = $this->getKernel(); $kernel ->expects($this->once()) ->method('registerBundles') ->will($this->returnValue(array($fooBundle, $barBundle))) ; - $kernel->initializeBundles(); + $kernel->initializeBundles(); } protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null) @@ -276,13 +458,13 @@ class KernelTest extends \PHPUnit_Framework_TestCase ->method('getPath') ->will($this->returnValue(strtr($dir, '\\', '/'))) ; - + $bundle ->expects($this->any()) ->method('getParent') ->will($this->returnValue($parent)) ; - + return $bundle; } @@ -333,9 +515,14 @@ class KernelForTest extends Kernel { parent::initializeBundles(); } + + public function isBooted() + { + return $this->booted; + } } abstract class BundleForTest implements BundleInterface { // We can not extend Symfony\Component\HttpKernel\Bundle\Bundle as we want to mock getName() which is final -} \ No newline at end of file +}