Nessuna descrizione

BundleTest.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\HttpKernel\Bundle\Bundle;
  13. use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle\ExtensionNotValidBundle;
  14. use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\ExtensionPresentBundle;
  15. use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionAbsentBundle\ExtensionAbsentBundle;
  16. use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\FooCommand;
  17. class BundleTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testGetContainerExtension()
  20. {
  21. $bundle = new ExtensionPresentBundle();
  22. $this->assertInstanceOf(
  23. 'Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\DependencyInjection\ExtensionPresentExtension',
  24. $bundle->getContainerExtension()
  25. );
  26. }
  27. public function testRegisterCommands()
  28. {
  29. $cmd = new FooCommand();
  30. $app = $this->getMockBuilder('Symfony\Component\Console\Application')->getMock();
  31. $app->expects($this->once())->method('add')->with($this->equalTo($cmd));
  32. $bundle = new ExtensionPresentBundle();
  33. $bundle->registerCommands($app);
  34. $bundle2 = new ExtensionAbsentBundle();
  35. $this->assertNull($bundle2->registerCommands($app));
  36. }
  37. /**
  38. * @expectedException \LogicException
  39. * @expectedExceptionMessage must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface
  40. */
  41. public function testGetContainerExtensionWithInvalidClass()
  42. {
  43. $bundle = new ExtensionNotValidBundle();
  44. $bundle->getContainerExtension();
  45. }
  46. public function testHttpKernelRegisterCommandsIgnoresCommandsThatAreRegisteredAsServices()
  47. {
  48. $container = new ContainerBuilder();
  49. $container->register('console.command.symfony_component_httpkernel_tests_fixtures_extensionpresentbundle_command_foocommand', 'Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\FooCommand');
  50. $application = $this->getMockBuilder('Symfony\Component\Console\Application')->getMock();
  51. // add() is never called when the found command classes are already registered as services
  52. $application->expects($this->never())->method('add');
  53. $bundle = new ExtensionPresentBundle();
  54. $bundle->setContainer($container);
  55. $bundle->registerCommands($application);
  56. }
  57. public function testBundleNameIsGuessedFromClass()
  58. {
  59. $bundle = new GuessedNameBundle();
  60. $this->assertSame('Symfony\Component\HttpKernel\Tests\Bundle', $bundle->getNamespace());
  61. $this->assertSame('GuessedNameBundle', $bundle->getName());
  62. }
  63. public function testBundleNameCanBeExplicitlyProvided()
  64. {
  65. $bundle = new NamedBundle();
  66. $this->assertSame('ExplicitlyNamedBundle', $bundle->getName());
  67. $this->assertSame('Symfony\Component\HttpKernel\Tests\Bundle', $bundle->getNamespace());
  68. $this->assertSame('ExplicitlyNamedBundle', $bundle->getName());
  69. }
  70. }
  71. class NamedBundle extends Bundle
  72. {
  73. public function __construct()
  74. {
  75. $this->name = 'ExplicitlyNamedBundle';
  76. }
  77. }
  78. class GuessedNameBundle extends Bundle
  79. {
  80. }