From d86aa74e3d68b8e5cf808bedcbf241fe0413649a Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 21 Apr 2011 22:34:53 +0200 Subject: [PATCH] [FrameworkBundle] removed the need to boot a Kernel in a unit test file --- .../Controller/ControllerNameParserTest.php | 52 +++++++++++++++---- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerNameParserTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerNameParserTest.php index 159e999d3f..158848d2df 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerNameParserTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerNameParserTest.php @@ -13,14 +13,27 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser; -use Symfony\Bundle\FrameworkBundle\Tests\Logger; -use Symfony\Bundle\FrameworkBundle\Tests\Kernel; - -require_once __DIR__.'/../Kernel.php'; -require_once __DIR__.'/../Logger.php'; +use Symfony\Component\ClassLoader\UniversalClassLoader; class ControllerNameParserTest extends TestCase { + protected $loader; + + public function setUp() + { + $this->loader = new UniversalClassLoader(); + $this->loader->registerNamespaces(array( + 'TestBundle' => __DIR__.'/../Fixtures', + 'TestApplication' => __DIR__.'/../Fixtures', + )); + $this->loader->register(); + } + + public function tearDown() + { + spl_autoload_unregister(array($this->loader, 'loadClass')); + } + public function testParse() { $parser = $this->createParser(); @@ -63,10 +76,31 @@ class ControllerNameParserTest extends TestCase private function createParser() { - $kernel = new Kernel(); - $kernel->boot(); - $logger = new Logger(); + $bundles = array( + 'SensioFooBundle' => array($this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'), $this->getBundle('TestBundle\Sensio\FooBundle', 'SensioFooBundle')), + 'SensioCmsFooBundle' => array($this->getBundle('TestBundle\Sensio\Cms\FooBundle', 'SensioCmsFooBundle')), + 'FooBundle' => array($this->getBundle('TestBundle\FooBundle', 'FooBundle')), + 'FabpotFooBundle' => array($this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'), $this->getBundle('TestBundle\Sensio\FooBundle', 'SensioFooBundle')), + ); - return new ControllerNameParser($kernel, $logger); + $kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface'); + $kernel + ->expects($this->any()) + ->method('getBundle') + ->will($this->returnCallback(function ($bundle) use ($bundles) { + return $bundles[$bundle]; + })) + ; + + return new ControllerNameParser($kernel); + } + + private function getBundle($namespace, $name) + { + $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface'); + $bundle->expects($this->any())->method('getName')->will($this->returnValue($name)); + $bundle->expects($this->any())->method('getNamespace')->will($this->returnValue($namespace)); + + return $bundle; } }