BundleTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\Bundle;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\HttpKernel\Bundle\Bundle;
  14. use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle\ExtensionNotValidBundle;
  15. use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\ExtensionPresentBundle;
  16. use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionAbsentBundle\ExtensionAbsentBundle;
  17. use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\FooCommand;
  18. class BundleTest extends TestCase
  19. {
  20. public function testGetContainerExtension()
  21. {
  22. $bundle = new ExtensionPresentBundle();
  23. $this->assertInstanceOf(
  24. 'Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\DependencyInjection\ExtensionPresentExtension',
  25. $bundle->getContainerExtension()
  26. );
  27. }
  28. public function testRegisterCommands()
  29. {
  30. $cmd = new FooCommand();
  31. $app = $this->getMockBuilder('Symfony\Component\Console\Application')->getMock();
  32. $app->expects($this->once())->method('add')->with($this->equalTo($cmd));
  33. $bundle = new ExtensionPresentBundle();
  34. $bundle->registerCommands($app);
  35. $bundle2 = new ExtensionAbsentBundle();
  36. $this->assertNull($bundle2->registerCommands($app));
  37. }
  38. /**
  39. * @expectedException \LogicException
  40. * @expectedExceptionMessage must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface
  41. */
  42. public function testGetContainerExtensionWithInvalidClass()
  43. {
  44. $bundle = new ExtensionNotValidBundle();
  45. $bundle->getContainerExtension();
  46. }
  47. public function testHttpKernelRegisterCommandsIgnoresCommandsThatAreRegisteredAsServices()
  48. {
  49. $container = new ContainerBuilder();
  50. $container->register('console.command.symfony_component_httpkernel_tests_fixtures_extensionpresentbundle_command_foocommand', 'Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\FooCommand');
  51. $application = $this->getMockBuilder('Symfony\Component\Console\Application')->getMock();
  52. // add() is never called when the found command classes are already registered as services
  53. $application->expects($this->never())->method('add');
  54. $bundle = new ExtensionPresentBundle();
  55. $bundle->setContainer($container);
  56. $bundle->registerCommands($application);
  57. }
  58. public function testBundleNameIsGuessedFromClass()
  59. {
  60. $bundle = new GuessedNameBundle();
  61. $this->assertSame('Symfony\Component\HttpKernel\Tests\Bundle', $bundle->getNamespace());
  62. $this->assertSame('GuessedNameBundle', $bundle->getName());
  63. }
  64. public function testBundleNameCanBeExplicitlyProvided()
  65. {
  66. $bundle = new NamedBundle();
  67. $this->assertSame('ExplicitlyNamedBundle', $bundle->getName());
  68. $this->assertSame('Symfony\Component\HttpKernel\Tests\Bundle', $bundle->getNamespace());
  69. $this->assertSame('ExplicitlyNamedBundle', $bundle->getName());
  70. }
  71. }
  72. class NamedBundle extends Bundle
  73. {
  74. public function __construct()
  75. {
  76. $this->name = 'ExplicitlyNamedBundle';
  77. }
  78. }
  79. class GuessedNameBundle extends Bundle
  80. {
  81. }