2012-08-01 03:25:05 +01:00
|
|
|
<?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;
|
2016-01-27 12:18:03 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
2012-08-01 03:25:05 +01:00
|
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
2017-08-09 17:38:20 +01:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2012-08-01 03:25:05 +01:00
|
|
|
use Symfony\Component\Console\Output\NullOutput;
|
2017-08-09 17:38:20 +01:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2013-07-29 18:21:16 +01:00
|
|
|
use Symfony\Component\Console\Tester\ApplicationTester;
|
2017-08-09 17:38:20 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
|
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
2012-08-01 03:25:05 +01:00
|
|
|
|
|
|
|
class ApplicationTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testBundleInterfaceImplementation()
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
|
2012-08-01 03:25:05 +01:00
|
|
|
|
2016-02-19 16:41:56 +00:00
|
|
|
$kernel = $this->getKernel(array($bundle), true);
|
2012-08-03 12:21:57 +01:00
|
|
|
|
|
|
|
$application = new Application($kernel);
|
|
|
|
$application->doRun(new ArrayInput(array('list')), new NullOutput());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBundleCommandsAreRegistered()
|
|
|
|
{
|
2016-11-07 19:38:43 +00:00
|
|
|
$bundle = $this->createBundleMock(array());
|
2012-08-03 12:21:57 +01:00
|
|
|
|
2016-02-19 16:41:56 +00:00
|
|
|
$kernel = $this->getKernel(array($bundle), true);
|
2012-08-03 12:21:57 +01:00
|
|
|
|
|
|
|
$application = new Application($kernel);
|
|
|
|
$application->doRun(new ArrayInput(array('list')), new NullOutput());
|
2016-01-27 12:18:03 +00:00
|
|
|
|
|
|
|
// Calling twice: registration should only be done once.
|
|
|
|
$application->doRun(new ArrayInput(array('list')), new NullOutput());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBundleCommandsAreRetrievable()
|
|
|
|
{
|
2016-11-07 19:38:43 +00:00
|
|
|
$bundle = $this->createBundleMock(array());
|
2016-01-27 12:18:03 +00:00
|
|
|
|
2016-02-19 16:41:56 +00:00
|
|
|
$kernel = $this->getKernel(array($bundle));
|
2016-01-27 12:18:03 +00:00
|
|
|
|
|
|
|
$application = new Application($kernel);
|
|
|
|
$application->all();
|
|
|
|
|
|
|
|
// Calling twice: registration should only be done once.
|
|
|
|
$application->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBundleSingleCommandIsRetrievable()
|
|
|
|
{
|
2016-11-07 19:38:43 +00:00
|
|
|
$command = new Command('example');
|
|
|
|
|
|
|
|
$bundle = $this->createBundleMock(array($command));
|
2016-01-27 12:18:03 +00:00
|
|
|
|
2016-02-19 16:41:56 +00:00
|
|
|
$kernel = $this->getKernel(array($bundle));
|
2016-01-27 12:18:03 +00:00
|
|
|
|
|
|
|
$application = new Application($kernel);
|
|
|
|
|
|
|
|
$this->assertSame($command, $application->get('example'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBundleCommandCanBeFound()
|
|
|
|
{
|
2016-11-07 19:38:43 +00:00
|
|
|
$command = new Command('example');
|
|
|
|
|
|
|
|
$bundle = $this->createBundleMock(array($command));
|
2016-01-27 12:18:03 +00:00
|
|
|
|
2016-02-19 16:41:56 +00:00
|
|
|
$kernel = $this->getKernel(array($bundle));
|
2016-01-27 12:18:03 +00:00
|
|
|
|
|
|
|
$application = new Application($kernel);
|
|
|
|
|
|
|
|
$this->assertSame($command, $application->find('example'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBundleCommandCanBeFoundByAlias()
|
|
|
|
{
|
2016-11-07 19:38:43 +00:00
|
|
|
$command = new Command('example');
|
|
|
|
$command->setAliases(array('alias'));
|
|
|
|
|
|
|
|
$bundle = $this->createBundleMock(array($command));
|
2016-01-27 12:18:03 +00:00
|
|
|
|
2016-02-19 16:41:56 +00:00
|
|
|
$kernel = $this->getKernel(array($bundle));
|
2016-01-27 12:18:03 +00:00
|
|
|
|
|
|
|
$application = new Application($kernel);
|
|
|
|
|
|
|
|
$this->assertSame($command, $application->find('alias'));
|
2012-08-03 12:21:57 +01:00
|
|
|
}
|
|
|
|
|
2013-07-29 18:21:16 +01:00
|
|
|
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');
|
|
|
|
|
2016-02-19 16:41:56 +00:00
|
|
|
$application = new Application($this->getKernel(array(), true));
|
2013-07-29 18:21:16 +01:00
|
|
|
$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'));
|
|
|
|
}
|
|
|
|
|
2017-05-24 22:07:05 +01:00
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
2017-08-09 17:38:20 +01:00
|
|
|
public function testRunOnlyWarnsOnUnregistrableCommand()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->register('event_dispatcher', EventDispatcher::class);
|
|
|
|
$container->register(ThrowingCommand::class, ThrowingCommand::class);
|
|
|
|
$container->setParameter('console.command.ids', array(ThrowingCommand::class => ThrowingCommand::class));
|
|
|
|
|
|
|
|
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
|
|
|
|
$kernel
|
|
|
|
->method('getBundles')
|
|
|
|
->willReturn(array($this->createBundleMock(
|
|
|
|
array((new Command('fine'))->setCode(function (InputInterface $input, OutputInterface $output) { $output->write('fine'); }))
|
|
|
|
)));
|
|
|
|
$kernel
|
|
|
|
->method('getContainer')
|
|
|
|
->willReturn($container);
|
|
|
|
|
|
|
|
$application = new Application($kernel);
|
|
|
|
$application->setAutoExit(false);
|
|
|
|
|
|
|
|
$tester = new ApplicationTester($application);
|
|
|
|
$tester->run(array('command' => 'fine'));
|
|
|
|
$output = $tester->getDisplay();
|
|
|
|
|
|
|
|
$this->assertSame(0, $tester->getStatusCode());
|
2017-10-07 16:55:15 +01:00
|
|
|
$this->assertContains('Some commands could not be registered:', $output);
|
2017-08-09 17:38:20 +01:00
|
|
|
$this->assertContains('throwing', $output);
|
|
|
|
$this->assertContains('fine', $output);
|
|
|
|
}
|
|
|
|
|
2018-02-23 18:15:37 +00:00
|
|
|
public function testRegistrationErrorsAreDisplayedOnCommandNotFound()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->register('event_dispatcher', EventDispatcher::class);
|
|
|
|
|
|
|
|
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
|
|
|
|
$kernel
|
|
|
|
->method('getBundles')
|
|
|
|
->willReturn(array($this->createBundleMock(
|
|
|
|
array((new Command(null))->setCode(function (InputInterface $input, OutputInterface $output) { $output->write('fine'); }))
|
|
|
|
)));
|
|
|
|
$kernel
|
|
|
|
->method('getContainer')
|
|
|
|
->willReturn($container);
|
|
|
|
|
|
|
|
$application = new Application($kernel);
|
|
|
|
$application->setAutoExit(false);
|
|
|
|
|
|
|
|
$tester = new ApplicationTester($application);
|
|
|
|
$tester->run(array('command' => 'fine'));
|
|
|
|
$output = $tester->getDisplay();
|
|
|
|
|
|
|
|
$this->assertSame(1, $tester->getStatusCode());
|
|
|
|
$this->assertContains('Some commands could not be registered:', $output);
|
|
|
|
$this->assertContains('Command "fine" is not defined.', $output);
|
|
|
|
}
|
|
|
|
|
2016-02-19 16:41:56 +00:00
|
|
|
private function getKernel(array $bundles, $useDispatcher = false)
|
2012-08-03 12:21:57 +01:00
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
|
2016-02-19 16:41:56 +00:00
|
|
|
|
|
|
|
if ($useDispatcher) {
|
2016-12-19 09:02:29 +00:00
|
|
|
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
|
2016-02-19 16:41:56 +00:00
|
|
|
$dispatcher
|
|
|
|
->expects($this->atLeastOnce())
|
|
|
|
->method('dispatch')
|
|
|
|
;
|
|
|
|
$container
|
|
|
|
->expects($this->atLeastOnce())
|
|
|
|
->method('get')
|
|
|
|
->with($this->equalTo('event_dispatcher'))
|
|
|
|
->will($this->returnValue($dispatcher));
|
|
|
|
}
|
|
|
|
|
2013-09-06 09:47:41 +01:00
|
|
|
$container
|
2017-09-03 00:52:02 +01:00
|
|
|
->expects($this->exactly(2))
|
2013-09-06 09:47:41 +01:00
|
|
|
->method('hasParameter')
|
2017-09-03 00:52:02 +01:00
|
|
|
->withConsecutive(array('console.command.ids'), array('console.lazy_command.ids'))
|
|
|
|
->willReturnOnConsecutiveCalls(true, true)
|
2013-09-06 09:47:41 +01:00
|
|
|
;
|
|
|
|
$container
|
2017-09-03 00:52:02 +01:00
|
|
|
->expects($this->exactly(2))
|
2013-09-06 09:47:41 +01:00
|
|
|
->method('getParameter')
|
2017-09-03 00:52:02 +01:00
|
|
|
->withConsecutive(array('console.lazy_command.ids'), array('console.command.ids'))
|
|
|
|
->willReturnOnConsecutiveCalls(array(), array())
|
2013-09-06 09:47:41 +01:00
|
|
|
;
|
2013-05-28 20:38:03 +01:00
|
|
|
|
2016-12-19 09:02:29 +00:00
|
|
|
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
|
2012-08-01 03:25:05 +01:00
|
|
|
$kernel
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getBundles')
|
2012-08-03 12:21:57 +01:00
|
|
|
->will($this->returnValue($bundles))
|
2012-08-01 03:25:05 +01:00
|
|
|
;
|
2013-05-28 20:38:03 +01:00
|
|
|
$kernel
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getContainer')
|
|
|
|
->will($this->returnValue($container))
|
|
|
|
;
|
2012-08-01 03:25:05 +01:00
|
|
|
|
2012-08-03 12:21:57 +01:00
|
|
|
return $kernel;
|
2012-08-01 03:25:05 +01:00
|
|
|
}
|
2016-11-07 19:38:43 +00:00
|
|
|
|
|
|
|
private function createBundleMock(array $commands)
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
|
2016-11-07 19:38:43 +00:00
|
|
|
$bundle
|
|
|
|
->expects($this->once())
|
|
|
|
->method('registerCommands')
|
|
|
|
->will($this->returnCallback(function (Application $application) use ($commands) {
|
|
|
|
$application->addCommands($commands);
|
|
|
|
}))
|
|
|
|
;
|
|
|
|
|
|
|
|
return $bundle;
|
|
|
|
}
|
2012-08-01 03:25:05 +01:00
|
|
|
}
|
2017-08-09 17:38:20 +01:00
|
|
|
|
|
|
|
class ThrowingCommand extends Command
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
throw new \Exception('throwing');
|
|
|
|
}
|
|
|
|
}
|