ApplicationTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\Bundle\FrameworkBundle\Tests\Console;
  11. use Symfony\Bundle\FrameworkBundle\Console\Application;
  12. use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Input\ArrayInput;
  15. use Symfony\Component\Console\Output\NullOutput;
  16. use Symfony\Component\Console\Tester\ApplicationTester;
  17. class ApplicationTest extends TestCase
  18. {
  19. public function testBundleInterfaceImplementation()
  20. {
  21. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
  22. $kernel = $this->getKernel(array($bundle), true);
  23. $application = new Application($kernel);
  24. $application->doRun(new ArrayInput(array('list')), new NullOutput());
  25. }
  26. public function testBundleCommandsAreRegistered()
  27. {
  28. $bundle = $this->createBundleMock(array());
  29. $kernel = $this->getKernel(array($bundle), true);
  30. $application = new Application($kernel);
  31. $application->doRun(new ArrayInput(array('list')), new NullOutput());
  32. // Calling twice: registration should only be done once.
  33. $application->doRun(new ArrayInput(array('list')), new NullOutput());
  34. }
  35. public function testBundleCommandsAreRetrievable()
  36. {
  37. $bundle = $this->createBundleMock(array());
  38. $kernel = $this->getKernel(array($bundle));
  39. $application = new Application($kernel);
  40. $application->all();
  41. // Calling twice: registration should only be done once.
  42. $application->all();
  43. }
  44. public function testBundleSingleCommandIsRetrievable()
  45. {
  46. $command = new Command('example');
  47. $bundle = $this->createBundleMock(array($command));
  48. $kernel = $this->getKernel(array($bundle));
  49. $application = new Application($kernel);
  50. $this->assertSame($command, $application->get('example'));
  51. }
  52. public function testBundleCommandCanBeFound()
  53. {
  54. $command = new Command('example');
  55. $bundle = $this->createBundleMock(array($command));
  56. $kernel = $this->getKernel(array($bundle));
  57. $application = new Application($kernel);
  58. $this->assertSame($command, $application->find('example'));
  59. }
  60. public function testBundleCommandCanBeFoundByAlias()
  61. {
  62. $command = new Command('example');
  63. $command->setAliases(array('alias'));
  64. $bundle = $this->createBundleMock(array($command));
  65. $kernel = $this->getKernel(array($bundle));
  66. $application = new Application($kernel);
  67. $this->assertSame($command, $application->find('alias'));
  68. }
  69. public function testBundleCommandsHaveRightContainer()
  70. {
  71. $command = $this->getMockForAbstractClass('Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand', array('foo'), '', true, true, true, array('setContainer'));
  72. $command->setCode(function () {});
  73. $command->expects($this->exactly(2))->method('setContainer');
  74. $application = new Application($this->getKernel(array(), true));
  75. $application->setAutoExit(false);
  76. $application->setCatchExceptions(false);
  77. $application->add($command);
  78. $tester = new ApplicationTester($application);
  79. // set container is called here
  80. $tester->run(array('command' => 'foo'));
  81. // as the container might have change between two runs, setContainer must called again
  82. $tester->run(array('command' => 'foo'));
  83. }
  84. public function testBundleCommandCanOverriddeAPreExistingCommandWithTheSameName()
  85. {
  86. $command = new Command('example');
  87. $bundle = $this->createBundleMock(array($command));
  88. $kernel = $this->getKernel(array($bundle));
  89. $application = new Application($kernel);
  90. $newCommand = new Command('example');
  91. $application->add($newCommand);
  92. $this->assertSame($newCommand, $application->get('example'));
  93. }
  94. private function getKernel(array $bundles, $useDispatcher = false)
  95. {
  96. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  97. if ($useDispatcher) {
  98. $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
  99. $dispatcher
  100. ->expects($this->atLeastOnce())
  101. ->method('dispatch')
  102. ;
  103. $container
  104. ->expects($this->atLeastOnce())
  105. ->method('get')
  106. ->with($this->equalTo('event_dispatcher'))
  107. ->will($this->returnValue($dispatcher));
  108. }
  109. $container
  110. ->expects($this->once())
  111. ->method('hasParameter')
  112. ->with($this->equalTo('console.command.ids'))
  113. ->will($this->returnValue(true))
  114. ;
  115. $container
  116. ->expects($this->once())
  117. ->method('getParameter')
  118. ->with($this->equalTo('console.command.ids'))
  119. ->will($this->returnValue(array()))
  120. ;
  121. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
  122. $kernel
  123. ->expects($this->any())
  124. ->method('getBundles')
  125. ->will($this->returnValue($bundles))
  126. ;
  127. $kernel
  128. ->expects($this->any())
  129. ->method('getContainer')
  130. ->will($this->returnValue($container))
  131. ;
  132. return $kernel;
  133. }
  134. private function createBundleMock(array $commands)
  135. {
  136. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
  137. $bundle
  138. ->expects($this->once())
  139. ->method('registerCommands')
  140. ->will($this->returnCallback(function (Application $application) use ($commands) {
  141. $application->addCommands($commands);
  142. }))
  143. ;
  144. return $bundle;
  145. }
  146. }