123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Bundle\FrameworkBundle\Tests\Console;
- use Symfony\Bundle\FrameworkBundle\Console\Application;
- use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\ArrayInput;
- use Symfony\Component\Console\Output\NullOutput;
- use Symfony\Component\Console\Tester\ApplicationTester;
- class ApplicationTest extends TestCase
- {
- public function testBundleInterfaceImplementation()
- {
- $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
- $kernel = $this->getKernel(array($bundle), true);
- $application = new Application($kernel);
- $application->doRun(new ArrayInput(array('list')), new NullOutput());
- }
- public function testBundleCommandsAreRegistered()
- {
- $bundle = $this->createBundleMock(array());
- $kernel = $this->getKernel(array($bundle), true);
- $application = new Application($kernel);
- $application->doRun(new ArrayInput(array('list')), new NullOutput());
- // Calling twice: registration should only be done once.
- $application->doRun(new ArrayInput(array('list')), new NullOutput());
- }
- public function testBundleCommandsAreRetrievable()
- {
- $bundle = $this->createBundleMock(array());
- $kernel = $this->getKernel(array($bundle));
- $application = new Application($kernel);
- $application->all();
- // Calling twice: registration should only be done once.
- $application->all();
- }
- public function testBundleSingleCommandIsRetrievable()
- {
- $command = new Command('example');
- $bundle = $this->createBundleMock(array($command));
- $kernel = $this->getKernel(array($bundle));
- $application = new Application($kernel);
- $this->assertSame($command, $application->get('example'));
- }
- public function testBundleCommandCanBeFound()
- {
- $command = new Command('example');
- $bundle = $this->createBundleMock(array($command));
- $kernel = $this->getKernel(array($bundle));
- $application = new Application($kernel);
- $this->assertSame($command, $application->find('example'));
- }
- public function testBundleCommandCanBeFoundByAlias()
- {
- $command = new Command('example');
- $command->setAliases(array('alias'));
- $bundle = $this->createBundleMock(array($command));
- $kernel = $this->getKernel(array($bundle));
- $application = new Application($kernel);
- $this->assertSame($command, $application->find('alias'));
- }
- public function testBundleCommandsHaveRightContainer()
- {
- $command = $this->getMockForAbstractClass('Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand', array('foo'), '', true, true, true, array('setContainer'));
- $command->setCode(function () {});
- $command->expects($this->exactly(2))->method('setContainer');
- $application = new Application($this->getKernel(array(), true));
- $application->setAutoExit(false);
- $application->setCatchExceptions(false);
- $application->add($command);
- $tester = new ApplicationTester($application);
- // set container is called here
- $tester->run(array('command' => 'foo'));
- // as the container might have change between two runs, setContainer must called again
- $tester->run(array('command' => 'foo'));
- }
- public function testBundleCommandCanOverriddeAPreExistingCommandWithTheSameName()
- {
- $command = new Command('example');
- $bundle = $this->createBundleMock(array($command));
- $kernel = $this->getKernel(array($bundle));
- $application = new Application($kernel);
- $newCommand = new Command('example');
- $application->add($newCommand);
- $this->assertSame($newCommand, $application->get('example'));
- }
- private function getKernel(array $bundles, $useDispatcher = false)
- {
- $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
- if ($useDispatcher) {
- $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
- $dispatcher
- ->expects($this->atLeastOnce())
- ->method('dispatch')
- ;
- $container
- ->expects($this->atLeastOnce())
- ->method('get')
- ->with($this->equalTo('event_dispatcher'))
- ->will($this->returnValue($dispatcher));
- }
- $container
- ->expects($this->once())
- ->method('hasParameter')
- ->with($this->equalTo('console.command.ids'))
- ->will($this->returnValue(true))
- ;
- $container
- ->expects($this->once())
- ->method('getParameter')
- ->with($this->equalTo('console.command.ids'))
- ->will($this->returnValue(array()))
- ;
- $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
- $kernel
- ->expects($this->any())
- ->method('getBundles')
- ->will($this->returnValue($bundles))
- ;
- $kernel
- ->expects($this->any())
- ->method('getContainer')
- ->will($this->returnValue($container))
- ;
- return $kernel;
- }
- private function createBundleMock(array $commands)
- {
- $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
- $bundle
- ->expects($this->once())
- ->method('registerCommands')
- ->will($this->returnCallback(function (Application $application) use ($commands) {
- $application->addCommands($commands);
- }))
- ;
- return $bundle;
- }
- }
|