Skip to content

Commit

Permalink
[HttpKernel] Add unit tests for Kernel. Also slightly modify Kernel t…
Browse files Browse the repository at this point in the history
…o make it more testable.
  • Loading branch information
ornicar authored and fabpot committed Feb 6, 2011
1 parent c251a36 commit 9ba2943
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 11 deletions.
22 changes: 16 additions & 6 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -97,7 +97,7 @@ public function boot()
// init container
$this->initializeContainer();

foreach ($this->bundles as $bundle) {
foreach ($this->getBundles() as $bundle) {
$bundle->setContainer($this->container);
$bundle->boot();
}
Expand All @@ -114,7 +114,7 @@ public function shutdown()
{
$this->booted = false;

foreach ($this->bundles as $bundle) {
foreach ($this->getBundles() as $bundle) {
$bundle->shutdown();
$bundle->setContainer(null);
}
Expand All @@ -131,7 +131,17 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
$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');
}

/**
Expand Down Expand Up @@ -343,7 +353,7 @@ protected function initializeBundles()
$this->bundles = array();
$topMostBundles = array();
$directChildren = array();

foreach ($this->registerBundles() as $bundle) {
$name = $bundle->getName();
if (isset($this->bundles[$name])) {
Expand All @@ -358,7 +368,7 @@ protected function initializeBundles()
$directChildren[$parentName] = $name;
} else {
$topMostBundles[$name] = $bundle;
}
}
}

// look for orphans
Expand All @@ -377,7 +387,7 @@ protected function initializeBundles()
array_unshift($bundleMap, $this->bundles[$name]);
$hierarchy[] = $name;
}

foreach ($hierarchy as $bundle) {
$this->bundleMap[$bundle] = $bundleMap;
array_pop($bundleMap);
Expand Down
197 changes: 192 additions & 5 deletions tests/Symfony/Tests/Component/HttpKernel/KernelTest.php
Expand Up @@ -14,9 +14,191 @@
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
*/
Expand Down Expand Up @@ -241,14 +423,14 @@ public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWith
{
$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)
Expand Down Expand Up @@ -276,13 +458,13 @@ protected function getBundle($dir = null, $parent = null, $className = null, $bu
->method('getPath')
->will($this->returnValue(strtr($dir, '\\', '/')))
;

$bundle
->expects($this->any())
->method('getParent')
->will($this->returnValue($parent))
;

return $bundle;
}

Expand Down Expand Up @@ -333,9 +515,14 @@ public function initializeBundles()
{
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
}
}

0 comments on commit 9ba2943

Please sign in to comment.