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\ExtensionAbsentBundle\ExtensionAbsentBundle;
18 use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle\ExtensionNotValidBundle;
19 use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\FooCommand;
20 use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\ExtensionPresentBundle;
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()
36 * @expectedDeprecation Auto-registration of the command "Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\FooCommand" is deprecated since Symfony 3.4 and won't be supported in 4.0. Use PSR-4 based service discovery instead.
38 public function testRegisterCommands()
40 $cmd = new FooCommand();
41 $app = $this->getMockBuilder('Symfony\Component\Console\Application')->getMock();
42 $app->expects($this->once())->method('add')->with($this->equalTo($cmd));
44 $bundle = new ExtensionPresentBundle();
45 $bundle->registerCommands($app);
47 $bundle2 = new ExtensionAbsentBundle();
49 $this->assertNull($bundle2->registerCommands($app));
53 * @expectedException \LogicException
54 * @expectedExceptionMessage must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface
56 public function testGetContainerExtensionWithInvalidClass()
58 $bundle = new ExtensionNotValidBundle();
59 $bundle->getContainerExtension();
62 public function testHttpKernelRegisterCommandsIgnoresCommandsThatAreRegisteredAsServices()
64 $container = new ContainerBuilder();
65 $container->register('console.command.symfony_component_httpkernel_tests_fixtures_extensionpresentbundle_command_foocommand', 'Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\FooCommand');
67 $application = $this->getMockBuilder('Symfony\Component\Console\Application')->getMock();
68 // add() is never called when the found command classes are already registered as services
69 $application->expects($this->never())->method('add');
71 $bundle = new ExtensionPresentBundle();
72 $bundle->setContainer($container);
73 $bundle->registerCommands($application);
76 public function testBundleNameIsGuessedFromClass()
78 $bundle = new GuessedNameBundle();
80 $this->assertSame('Symfony\Component\HttpKernel\Tests\Bundle', $bundle->getNamespace());
81 $this->assertSame('GuessedNameBundle', $bundle->getName());
84 public function testBundleNameCanBeExplicitlyProvided()
86 $bundle = new NamedBundle();
88 $this->assertSame('ExplicitlyNamedBundle', $bundle->getName());
89 $this->assertSame('Symfony\Component\HttpKernel\Tests\Bundle', $bundle->getNamespace());
90 $this->assertSame('ExplicitlyNamedBundle', $bundle->getName());
94 class NamedBundle extends Bundle
96 public function __construct()
98 $this->name = 'ExplicitlyNamedBundle';
102 class GuessedNameBundle extends Bundle