2010-03-18 07:48:03 +00:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2011-01-15 13:29:43 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-03-18 07:48:03 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\Console\Tests\Command ;
2010-03-18 07:48:03 +00:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Helper\FormatterHelper ;
use Symfony\Component\Console\Application ;
use Symfony\Component\Console\Input\InputDefinition ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Input\StringInput ;
use Symfony\Component\Console\Output\OutputInterface ;
use Symfony\Component\Console\Output\NullOutput ;
use Symfony\Component\Console\Tester\CommandTester ;
2010-03-18 07:48:03 +00:00
class CommandTest extends \PHPUnit_Framework_TestCase
{
2012-07-09 13:50:58 +01:00
protected static $fixturesPath ;
2010-03-18 07:48:03 +00:00
2012-07-09 13:50:58 +01:00
public static function setUpBeforeClass ()
2010-05-06 12:25:53 +01:00
{
2010-06-28 08:31:54 +01:00
self :: $fixturesPath = __DIR__ . '/../Fixtures/' ;
2010-05-06 12:25:53 +01:00
require_once self :: $fixturesPath . '/TestCommand.php' ;
}
public function testConstructor ()
{
$command = new Command ( 'foo:bar' );
2011-06-07 16:51:43 +01:00
$this -> assertEquals ( 'foo:bar' , $command -> getName (), '__construct() takes the command name as its first argument' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2013-02-05 21:55:33 +00:00
/**
* @ expectedException \LogicException
2014-09-23 06:29:11 +01:00
* @ expectedExceptionMessage The command defined in " Symfony \ Component \ Console \ Command \ Command " cannot have an empty name .
2013-02-05 21:55:33 +00:00
*/
public function testCommandNameCannotBeEmpty ()
{
new Command ();
}
2010-05-06 12:25:53 +01:00
public function testSetApplication ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ();
$command = new \TestCommand ();
$command -> setApplication ( $application );
$this -> assertEquals ( $application , $command -> getApplication (), '->setApplication() sets the current application' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testSetGetDefinition ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$command = new \TestCommand ();
$ret = $command -> setDefinition ( $definition = new InputDefinition ());
$this -> assertEquals ( $command , $ret , '->setDefinition() implements a fluent interface' );
$this -> assertEquals ( $definition , $command -> getDefinition (), '->setDefinition() sets the current InputDefinition instance' );
$command -> setDefinition ( array ( new InputArgument ( 'foo' ), new InputOption ( 'bar' )));
$this -> assertTrue ( $command -> getDefinition () -> hasArgument ( 'foo' ), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument' );
$this -> assertTrue ( $command -> getDefinition () -> hasOption ( 'bar' ), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument' );
$command -> setDefinition ( new InputDefinition ());
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testAddArgument ()
{
$command = new \TestCommand ();
$ret = $command -> addArgument ( 'foo' );
$this -> assertEquals ( $command , $ret , '->addArgument() implements a fluent interface' );
$this -> assertTrue ( $command -> getDefinition () -> hasArgument ( 'foo' ), '->addArgument() adds an argument to the command' );
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
public function testAddOption ()
{
$command = new \TestCommand ();
$ret = $command -> addOption ( 'foo' );
$this -> assertEquals ( $command , $ret , '->addOption() implements a fluent interface' );
$this -> assertTrue ( $command -> getDefinition () -> hasOption ( 'foo' ), '->addOption() adds an option to the command' );
}
2010-03-18 07:48:03 +00:00
2011-06-07 16:51:43 +01:00
public function testGetNamespaceGetNameSetName ()
2010-05-06 12:25:53 +01:00
{
$command = new \TestCommand ();
2011-06-07 16:51:43 +01:00
$this -> assertEquals ( 'namespace:name' , $command -> getName (), '->getName() returns the command name' );
2010-05-06 12:25:53 +01:00
$command -> setName ( 'foo' );
$this -> assertEquals ( 'foo' , $command -> getName (), '->setName() sets the command name' );
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
$ret = $command -> setName ( 'foobar:bar' );
$this -> assertEquals ( $command , $ret , '->setName() implements a fluent interface' );
2011-06-07 16:51:43 +01:00
$this -> assertEquals ( 'foobar:bar' , $command -> getName (), '->setName() sets the command name' );
2013-02-05 21:55:33 +00:00
}
/**
* @ dataProvider provideInvalidCommandNames
*/
public function testInvalidCommandNames ( $name )
{
$this -> setExpectedException ( 'InvalidArgumentException' , sprintf ( 'Command name "%s" is invalid.' , $name ));
$command = new \TestCommand ();
$command -> setName ( $name );
}
2010-03-18 07:48:03 +00:00
2013-02-05 21:55:33 +00:00
public function provideInvalidCommandNames ()
{
return array (
array ( '' ),
2014-09-21 19:53:12 +01:00
array ( 'foo:' ),
2013-02-05 21:55:33 +00:00
);
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
public function testGetSetDescription ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$command = new \TestCommand ();
$this -> assertEquals ( 'description' , $command -> getDescription (), '->getDescription() returns the description' );
$ret = $command -> setDescription ( 'description1' );
$this -> assertEquals ( $command , $ret , '->setDescription() implements a fluent interface' );
$this -> assertEquals ( 'description1' , $command -> getDescription (), '->setDescription() sets the description' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testGetSetHelp ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$command = new \TestCommand ();
$this -> assertEquals ( 'help' , $command -> getHelp (), '->getHelp() returns the help' );
$ret = $command -> setHelp ( 'help1' );
$this -> assertEquals ( $command , $ret , '->setHelp() implements a fluent interface' );
$this -> assertEquals ( 'help1' , $command -> getHelp (), '->setHelp() sets the help' );
2010-03-18 07:48:03 +00:00
}
2012-02-17 17:58:01 +00:00
public function testGetProcessedHelp ()
{
$command = new \TestCommand ();
$command -> setHelp ( 'The %command.name% command does... Example: php %command.full_name%.' );
$this -> assertContains ( 'The namespace:name command does...' , $command -> getProcessedHelp (), '->getProcessedHelp() replaces %command.name% correctly' );
$this -> assertNotContains ( '%command.full_name%' , $command -> getProcessedHelp (), '->getProcessedHelp() replaces %command.full_name%' );
}
2010-05-06 12:25:53 +01:00
public function testGetSetAliases ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$command = new \TestCommand ();
$this -> assertEquals ( array ( 'name' ), $command -> getAliases (), '->getAliases() returns the aliases' );
$ret = $command -> setAliases ( array ( 'name1' ));
$this -> assertEquals ( $command , $ret , '->setAliases() implements a fluent interface' );
$this -> assertEquals ( array ( 'name1' ), $command -> getAliases (), '->setAliases() sets the aliases' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testGetSynopsis ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$command = new \TestCommand ();
$command -> addOption ( 'foo' );
$command -> addArgument ( 'foo' );
$this -> assertEquals ( 'namespace:name [--foo] [foo]' , $command -> getSynopsis (), '->getSynopsis() returns the synopsis' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testGetHelper ()
{
$application = new Application ();
$command = new \TestCommand ();
$command -> setApplication ( $application );
$formatterHelper = new FormatterHelper ();
$this -> assertEquals ( $formatterHelper -> getName (), $command -> getHelper ( 'formatter' ) -> getName (), '->getHelper() returns the correct helper' );
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
public function testGet ()
{
$application = new Application ();
$command = new \TestCommand ();
$command -> setApplication ( $application );
$formatterHelper = new FormatterHelper ();
2011-03-11 11:53:42 +00:00
$this -> assertEquals ( $formatterHelper -> getName (), $command -> getHelper ( 'formatter' ) -> getName (), '->__get() returns the correct helper' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
public function testMergeApplicationDefinition ()
{
$application1 = new Application ();
$application1 -> getDefinition () -> addArguments ( array ( new InputArgument ( 'foo' )));
$application1 -> getDefinition () -> addOptions ( array ( new InputOption ( 'bar' )));
$command = new \TestCommand ();
$command -> setApplication ( $application1 );
$command -> setDefinition ( $definition = new InputDefinition ( array ( new InputArgument ( 'bar' ), new InputOption ( 'foo' ))));
2011-03-11 11:53:42 +00:00
$r = new \ReflectionObject ( $command );
$m = $r -> getMethod ( 'mergeApplicationDefinition' );
$m -> setAccessible ( true );
$m -> invoke ( $command );
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( $command -> getDefinition () -> hasArgument ( 'foo' ), '->mergeApplicationDefinition() merges the application arguments and the command arguments' );
$this -> assertTrue ( $command -> getDefinition () -> hasArgument ( 'bar' ), '->mergeApplicationDefinition() merges the application arguments and the command arguments' );
$this -> assertTrue ( $command -> getDefinition () -> hasOption ( 'foo' ), '->mergeApplicationDefinition() merges the application options and the command options' );
$this -> assertTrue ( $command -> getDefinition () -> hasOption ( 'bar' ), '->mergeApplicationDefinition() merges the application options and the command options' );
2010-04-09 00:54:36 +01:00
2011-03-11 11:53:42 +00:00
$m -> invoke ( $command );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 3 , $command -> getDefinition () -> getArgumentCount (), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options' );
}
2010-03-18 07:48:03 +00:00
2013-04-12 14:15:07 +01:00
public function testMergeApplicationDefinitionWithoutArgsThenWithArgsAddsArgs ()
{
$application1 = new Application ();
$application1 -> getDefinition () -> addArguments ( array ( new InputArgument ( 'foo' )));
$application1 -> getDefinition () -> addOptions ( array ( new InputOption ( 'bar' )));
$command = new \TestCommand ();
$command -> setApplication ( $application1 );
$command -> setDefinition ( $definition = new InputDefinition ( array ()));
$r = new \ReflectionObject ( $command );
$m = $r -> getMethod ( 'mergeApplicationDefinition' );
$m -> setAccessible ( true );
$m -> invoke ( $command , false );
2014-11-29 10:42:24 +00:00
$this -> assertTrue ( $command -> getDefinition () -> hasOption ( 'bar' ), '->mergeApplicationDefinition(false) merges the application and the command options' );
2013-04-12 14:15:07 +01:00
$this -> assertFalse ( $command -> getDefinition () -> hasArgument ( 'foo' ), '->mergeApplicationDefinition(false) does not merge the application arguments' );
$m -> invoke ( $command , true );
$this -> assertTrue ( $command -> getDefinition () -> hasArgument ( 'foo' ), '->mergeApplicationDefinition(true) merges the application arguments and the command arguments' );
$m -> invoke ( $command );
$this -> assertEquals ( 2 , $command -> getDefinition () -> getArgumentCount (), '->mergeApplicationDefinition() does not try to merge twice the application arguments' );
}
2013-02-05 21:55:33 +00:00
public function testRunInteractive ()
2010-05-06 12:25:53 +01:00
{
2013-02-05 21:55:33 +00:00
$tester = new CommandTester ( new \TestCommand ());
2010-03-18 07:48:03 +00:00
2011-04-03 21:30:40 +01:00
$tester -> execute ( array (), array ( 'interactive' => true ));
2013-02-05 21:55:33 +00:00
2011-04-03 21:30:40 +01:00
$this -> assertEquals ( 'interact called' . PHP_EOL . 'execute called' . PHP_EOL , $tester -> getDisplay (), '->run() calls the interact() method if the input is interactive' );
2013-02-05 21:55:33 +00:00
}
public function testRunNonInteractive ()
{
$tester = new CommandTester ( new \TestCommand ());
2011-04-03 21:30:40 +01:00
$tester -> execute ( array (), array ( 'interactive' => false ));
2013-02-05 21:55:33 +00:00
2011-04-03 21:30:40 +01:00
$this -> assertEquals ( 'execute called' . PHP_EOL , $tester -> getDisplay (), '->run() does not call the interact() method if the input is not interactive' );
2013-02-05 21:55:33 +00:00
}
2010-03-18 07:48:03 +00:00
2013-02-05 21:55:33 +00:00
/**
* @ expectedException \LogicException
* @ expectedExceptionMessage You must override the execute () method in the concrete command class .
*/
2014-11-29 10:42:24 +00:00
public function testExecuteMethodNeedsToBeOverridden ()
2013-02-05 21:55:33 +00:00
{
2010-05-06 12:25:53 +01:00
$command = new Command ( 'foo' );
2013-02-05 21:55:33 +00:00
$command -> run ( new StringInput ( '' ), new NullOutput ());
}
/**
* @ expectedException \InvalidArgumentException
* @ expectedExceptionMessage The " --bar " option does not exist .
*/
public function testRunWithInvalidOption ()
{
$command = new \TestCommand ();
$tester = new CommandTester ( $command );
$tester -> execute ( array ( '--bar' => true ));
2010-03-18 07:48:03 +00:00
}
2013-05-17 22:54:02 +01:00
public function testRunReturnsIntegerExitCode ()
{
$command = new \TestCommand ();
$exitCode = $command -> run ( new StringInput ( '' ), new NullOutput ());
$this -> assertSame ( 0 , $exitCode , '->run() returns integer exit code (treats null as 0)' );
$command = $this -> getMock ( 'TestCommand' , array ( 'execute' ));
$command -> expects ( $this -> once ())
-> method ( 'execute' )
-> will ( $this -> returnValue ( '2.3' ));
$exitCode = $command -> run ( new StringInput ( '' ), new NullOutput ());
$this -> assertSame ( 2 , $exitCode , '->run() returns integer exit code (casts numeric to int)' );
}
2012-09-24 09:41:20 +01:00
public function testRunReturnsAlwaysInteger ()
{
$command = new \TestCommand ();
$this -> assertSame ( 0 , $command -> run ( new StringInput ( '' ), new NullOutput ()));
}
2010-05-06 12:25:53 +01:00
public function testSetCode ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$command = new \TestCommand ();
2012-05-20 17:15:10 +01:00
$ret = $command -> setCode ( function ( InputInterface $input , OutputInterface $output ) {
2010-05-06 12:25:53 +01:00
$output -> writeln ( 'from the code...' );
});
$this -> assertEquals ( $command , $ret , '->setCode() implements a fluent interface' );
$tester = new CommandTester ( $command );
$tester -> execute ( array ());
$this -> assertEquals ( 'interact called' . PHP_EOL . 'from the code...' . PHP_EOL , $tester -> getDisplay ());
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2012-12-03 19:10:17 +00:00
public function testSetCodeWithNonClosureCallable ()
{
$command = new \TestCommand ();
$ret = $command -> setCode ( array ( $this , 'callableMethodCommand' ));
$this -> assertEquals ( $command , $ret , '->setCode() implements a fluent interface' );
$tester = new CommandTester ( $command );
$tester -> execute ( array ());
$this -> assertEquals ( 'interact called' . PHP_EOL . 'from the code...' . PHP_EOL , $tester -> getDisplay ());
}
/**
2013-02-05 21:55:33 +00:00
* @ expectedException \InvalidArgumentException
2012-12-03 19:10:17 +00:00
* @ expectedExceptionMessage Invalid callable provided to Command :: setCode .
*/
public function testSetCodeWithNonCallable ()
{
$command = new \TestCommand ();
$command -> setCode ( array ( $this , 'nonExistentMethod' ));
}
public function callableMethodCommand ( InputInterface $input , OutputInterface $output )
{
$output -> writeln ( 'from the code...' );
}
2015-01-02 16:09:32 +00:00
public function testLegacyAsText ()
2010-03-18 07:48:03 +00:00
{
2015-01-05 16:25:45 +00:00
$this -> iniSet ( 'error_reporting' , - 1 & ~ E_USER_DEPRECATED );
2015-01-02 16:09:32 +00:00
2010-05-06 12:25:53 +01:00
$command = new \TestCommand ();
$command -> setApplication ( new Application ());
$tester = new CommandTester ( $command );
2011-06-07 16:51:43 +01:00
$tester -> execute ( array ( 'command' => $command -> getName ()));
2010-05-06 12:25:53 +01:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/command_astext.txt' , $command -> asText (), '->asText() returns a text representation of the command' );
2010-03-18 07:48:03 +00:00
}
2015-01-02 16:09:32 +00:00
public function testLegacyAsXml ()
2010-03-18 07:48:03 +00:00
{
2015-01-05 16:25:45 +00:00
$this -> iniSet ( 'error_reporting' , - 1 & ~ E_USER_DEPRECATED );
2015-01-02 16:09:32 +00:00
2010-05-06 12:25:53 +01:00
$command = new \TestCommand ();
$command -> setApplication ( new Application ());
$tester = new CommandTester ( $command );
2011-06-07 16:51:43 +01:00
$tester -> execute ( array ( 'command' => $command -> getName ()));
2010-05-06 12:25:53 +01:00
$this -> assertXmlStringEqualsXmlFile ( self :: $fixturesPath . '/command_asxml.txt' , $command -> asXml (), '->asXml() returns an XML representation of the command' );
}
2010-03-18 07:48:03 +00:00
}