4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\Bundle;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\HttpKernel\Bundle\Bundle;
17 use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle\ExtensionNotValidBundle;
18 use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\ExtensionPresentBundle;
19 use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionAbsentBundle\ExtensionAbsentBundle;
20 use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\FooCommand;
22 class BundleTest extends TestCase
24 public function testGetContainerExtension()
26 $bundle = new ExtensionPresentBundle();
28 $this->assertInstanceOf(
29 'Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\DependencyInjection\ExtensionPresentExtension',
30 $bundle->getContainerExtension()
34 public function testRegisterCommands()
36 $cmd = new FooCommand();
37 $app = $this->getMockBuilder('Symfony\Component\Console\Application')->getMock();
38 $app->expects($this->once())->method('add')->with($this->equalTo($cmd));
40 $bundle = new ExtensionPresentBundle();
41 $bundle->registerCommands($app);
43 $bundle2 = new ExtensionAbsentBundle();
45 $this->assertNull($bundle2->registerCommands($app));
49 * @expectedException \LogicException
50 * @expectedExceptionMessage must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface
52 public function testGetContainerExtensionWithInvalidClass()
54 $bundle = new ExtensionNotValidBundle();
55 $bundle->getContainerExtension();
58 public function testHttpKernelRegisterCommandsIgnoresCommandsThatAreRegisteredAsServices()
60 $container = new ContainerBuilder();
61 $container->register('console.command.symfony_component_httpkernel_tests_fixtures_extensionpresentbundle_command_foocommand', 'Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\FooCommand');
63 $application = $this->getMockBuilder('Symfony\Component\Console\Application')->getMock();
64 // add() is never called when the found command classes are already registered as services
65 $application->expects($this->never())->method('add');
67 $bundle = new ExtensionPresentBundle();
68 $bundle->setContainer($container);
69 $bundle->registerCommands($application);
72 public function testBundleNameIsGuessedFromClass()
74 $bundle = new GuessedNameBundle();
76 $this->assertSame('Symfony\Component\HttpKernel\Tests\Bundle', $bundle->getNamespace());
77 $this->assertSame('GuessedNameBundle', $bundle->getName());
80 public function testBundleNameCanBeExplicitlyProvided()
82 $bundle = new NamedBundle();
84 $this->assertSame('ExplicitlyNamedBundle', $bundle->getName());
85 $this->assertSame('Symfony\Component\HttpKernel\Tests\Bundle', $bundle->getNamespace());
86 $this->assertSame('ExplicitlyNamedBundle', $bundle->getName());
90 class NamedBundle extends Bundle
92 public function __construct()
94 $this->name = 'ExplicitlyNamedBundle';
98 class GuessedNameBundle extends Bundle