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 ;
2010-03-18 07:48:03 +00:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\Console\Application ;
2012-10-01 14:15:04 +01:00
use Symfony\Component\Console\Helper\HelperSet ;
use Symfony\Component\Console\Helper\FormatterHelper ;
2012-02-12 15:37:55 +00:00
use Symfony\Component\Console\Input\ArrayInput ;
2013-03-24 09:08:12 +00:00
use Symfony\Component\Console\Input\InputInterface ;
2011-11-07 16:22:50 +00:00
use Symfony\Component\Console\Input\InputArgument ;
2012-10-01 14:15:04 +01:00
use Symfony\Component\Console\Input\InputDefinition ;
2011-11-07 16:22:50 +00:00
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Output\NullOutput ;
2012-02-12 15:37:55 +00:00
use Symfony\Component\Console\Output\Output ;
2013-03-24 09:08:12 +00:00
use Symfony\Component\Console\Output\OutputInterface ;
2012-02-12 15:37:55 +00:00
use Symfony\Component\Console\Tester\ApplicationTester ;
2013-03-24 09:08:12 +00:00
use Symfony\Component\Console\Event\ConsoleCommandEvent ;
2013-05-27 11:36:30 +01:00
use Symfony\Component\Console\Event\ConsoleExceptionEvent ;
2013-03-24 09:08:12 +00:00
use Symfony\Component\Console\Event\ConsoleTerminateEvent ;
use Symfony\Component\EventDispatcher\EventDispatcher ;
2010-03-18 07:48:03 +00:00
class ApplicationTest extends \PHPUnit_Framework_TestCase
{
2012-07-09 13:50:58 +01:00
protected static $fixturesPath ;
2010-05-06 12:25:53 +01:00
2012-07-09 13:50:58 +01:00
public static function setUpBeforeClass ()
2010-03-18 07:48:03 +00:00
{
2010-06-28 08:31:54 +01:00
self :: $fixturesPath = realpath ( __DIR__ . '/Fixtures/' );
2010-05-06 12:25:53 +01:00
require_once self :: $fixturesPath . '/FooCommand.php' ;
require_once self :: $fixturesPath . '/Foo1Command.php' ;
require_once self :: $fixturesPath . '/Foo2Command.php' ;
2011-01-11 07:49:57 +00:00
require_once self :: $fixturesPath . '/Foo3Command.php' ;
2012-10-09 22:27:04 +01:00
require_once self :: $fixturesPath . '/Foo4Command.php' ;
2013-10-01 11:23:25 +01:00
require_once self :: $fixturesPath . '/Foo5Command.php' ;
2013-08-07 07:55:13 +01:00
require_once self :: $fixturesPath . '/FoobarCommand.php' ;
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2012-02-17 17:58:01 +00:00
protected function normalizeLineBreaks ( $text )
2010-11-16 17:34:48 +00:00
{
return str_replace ( PHP_EOL , " \n " , $text );
}
2012-02-17 17:58:01 +00:00
/**
* Replaces the dynamic placeholders of the command help text with a static version .
* The placeholder % command . full_name % includes the script path that is not predictable
* and can not be tested against .
*/
protected function ensureStaticCommandHelp ( Application $application )
{
foreach ( $application -> all () as $command ) {
$command -> setHelp ( str_replace ( '%command.full_name%' , 'app/console %command.name%' , $command -> getHelp ()));
}
}
2010-05-06 12:25:53 +01:00
public function testConstructor ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ( 'foo' , 'bar' );
$this -> assertEquals ( 'foo' , $application -> getName (), '__construct() takes the application name as its first argument' );
2013-06-27 00:04:51 +01:00
$this -> assertEquals ( 'bar' , $application -> getVersion (), '__construct() takes the application version as its second argument' );
2010-11-23 08:42:19 +00:00
$this -> assertEquals ( array ( 'help' , 'list' ), array_keys ( $application -> all ()), '__construct() registered the help and list commands by default' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testSetGetName ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ();
$application -> setName ( 'foo' );
$this -> assertEquals ( 'foo' , $application -> getName (), '->setName() sets the name of the application' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testSetGetVersion ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ();
$application -> setVersion ( 'bar' );
$this -> assertEquals ( 'bar' , $application -> getVersion (), '->setVersion() sets the version of the application' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testGetLongVersion ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ( 'foo' , 'bar' );
$this -> assertEquals ( '<info>foo</info> version <comment>bar</comment>' , $application -> getLongVersion (), '->getLongVersion() returns the long version of the application' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testHelp ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ();
2012-02-17 17:58:01 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_gethelp.txt' , $this -> normalizeLineBreaks ( $application -> getHelp ()), '->setHelp() returns a help message' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2010-11-23 08:42:19 +00:00
public function testAll ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ();
2010-11-23 08:42:19 +00:00
$commands = $application -> all ();
$this -> assertEquals ( 'Symfony\\Component\\Console\\Command\\HelpCommand' , get_class ( $commands [ 'help' ]), '->all() returns the registered commands' );
2010-05-06 12:25:53 +01:00
2010-11-23 08:42:19 +00:00
$application -> add ( new \FooCommand ());
$commands = $application -> all ( 'foo' );
$this -> assertEquals ( 1 , count ( $commands ), '->all() takes a namespace as its first argument' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testRegister ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ();
$command = $application -> register ( 'foo' );
$this -> assertEquals ( 'foo' , $command -> getName (), '->register() registers a new command' );
2010-03-18 07:48:03 +00:00
}
2010-11-23 08:42:19 +00:00
public function testAdd ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ();
2010-11-23 08:42:19 +00:00
$application -> add ( $foo = new \FooCommand ());
$commands = $application -> all ();
$this -> assertEquals ( $foo , $commands [ 'foo:bar' ], '->add() registers a command' );
2010-05-06 12:25:53 +01:00
$application = new Application ();
$application -> addCommands ( array ( $foo = new \FooCommand (), $foo1 = new \Foo1Command ()));
2010-11-23 08:42:19 +00:00
$commands = $application -> all ();
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array ( $foo , $foo1 ), array ( $commands [ 'foo:bar' ], $commands [ 'foo:bar1' ]), '->addCommands() registers an array of commands' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2013-10-01 11:23:25 +01:00
/**
* @ expectedException InvalidArgumentException
* @ expectedExceptionMessage You must call the parent constructor in " Foo5Command::__construct() "
*/
public function testAddCommandWithEmptyContructor ()
{
$application = new Application ();
$application -> add ( $foo = new \Foo5Command ());
}
2010-11-23 08:42:19 +00:00
public function testHasGet ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ();
2010-11-23 08:42:19 +00:00
$this -> assertTrue ( $application -> has ( 'list' ), '->has() returns true if a named command is registered' );
$this -> assertFalse ( $application -> has ( 'afoobar' ), '->has() returns false if a named command is not registered' );
2010-05-06 12:25:53 +01:00
2010-11-23 08:42:19 +00:00
$application -> add ( $foo = new \FooCommand ());
$this -> assertTrue ( $application -> has ( 'afoobar' ), '->has() returns true if an alias is registered' );
$this -> assertEquals ( $foo , $application -> get ( 'foo:bar' ), '->get() returns a command by name' );
$this -> assertEquals ( $foo , $application -> get ( 'afoobar' ), '->get() returns a command by alias' );
2010-05-06 12:25:53 +01:00
2011-03-11 11:53:42 +00:00
$application = new Application ();
2010-11-23 08:42:19 +00:00
$application -> add ( $foo = new \FooCommand ());
2011-03-11 11:53:42 +00:00
// simulate --help
$r = new \ReflectionObject ( $application );
$p = $r -> getProperty ( 'wantHelps' );
$p -> setAccessible ( true );
$p -> setValue ( $application , true );
2010-11-23 08:42:19 +00:00
$command = $application -> get ( 'foo:bar' );
2013-02-05 21:55:33 +00:00
$this -> assertInstanceOf ( 'Symfony\Component\Console\Command\HelpCommand' , $command , '->get() returns the help command if --help is provided as the input' );
}
2013-05-09 09:40:45 +01:00
public function testSilentHelp ()
{
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
$tester = new ApplicationTester ( $application );
$tester -> run ( array ( '-h' => true , '-q' => true ), array ( 'decorated' => false ));
$this -> assertEmpty ( $tester -> getDisplay ( true ));
}
2013-02-05 21:55:33 +00:00
/**
* @ expectedException \InvalidArgumentException
* @ expectedExceptionMessage The command " foofoo " does not exist .
*/
public function testGetInvalidCommand ()
{
$application = new Application ();
$application -> get ( 'foofoo' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testGetNamespaces ()
2010-03-18 07:48:03 +00:00
{
2011-03-11 11:53:42 +00:00
$application = new Application ();
2010-11-23 08:42:19 +00:00
$application -> add ( new \FooCommand ());
$application -> add ( new \Foo1Command ());
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array ( 'foo' ), $application -> getNamespaces (), '->getNamespaces() returns an array of unique used namespaces' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testFindNamespace ()
2010-03-18 07:48:03 +00:00
{
2011-03-11 11:53:42 +00:00
$application = new Application ();
2010-11-23 08:42:19 +00:00
$application -> add ( new \FooCommand ());
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'foo' , $application -> findNamespace ( 'foo' ), '->findNamespace() returns the given namespace if it exists' );
$this -> assertEquals ( 'foo' , $application -> findNamespace ( 'f' ), '->findNamespace() finds a namespace given an abbreviation' );
2010-11-23 08:42:19 +00:00
$application -> add ( new \Foo2Command ());
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'foo' , $application -> findNamespace ( 'foo' ), '->findNamespace() returns the given namespace if it exists' );
2013-02-05 21:55:33 +00:00
}
2010-05-06 12:25:53 +01:00
2013-02-05 21:55:33 +00:00
/**
* @ expectedException \InvalidArgumentException
* @ expectedExceptionMessage The namespace " f " is ambiguous ( foo , foo1 ) .
*/
public function testFindAmbiguousNamespace ()
{
$application = new Application ();
$application -> add ( new \FooCommand ());
$application -> add ( new \Foo2Command ());
$application -> findNamespace ( 'f' );
}
/**
* @ expectedException \InvalidArgumentException
* @ expectedExceptionMessage There are no commands defined in the " bar " namespace .
*/
public function testFindInvalidNamespace ()
{
$application = new Application ();
$application -> findNamespace ( 'bar' );
2010-03-18 07:48:03 +00:00
}
2013-08-07 07:55:13 +01:00
/**
* @ expectedException \InvalidArgumentException
* @ expectedExceptionMessage Command " foo1 " is not defined
*/
public function testFindUniqueNameButNamespaceName ()
{
$application = new Application ();
$application -> add ( new \FooCommand ());
$application -> add ( new \Foo1Command ());
$application -> add ( new \Foo2Command ());
$application -> find ( $commandName = 'foo1' );
}
2010-11-23 08:42:19 +00:00
public function testFind ()
2010-05-06 12:25:53 +01:00
{
2011-03-11 11:53:42 +00:00
$application = new Application ();
2010-11-23 08:42:19 +00:00
$application -> add ( new \FooCommand ());
2010-05-06 12:25:53 +01:00
2013-02-05 21:55:33 +00:00
$this -> assertInstanceOf ( 'FooCommand' , $application -> find ( 'foo:bar' ), '->find() returns a command if its name exists' );
$this -> assertInstanceOf ( 'Symfony\Component\Console\Command\HelpCommand' , $application -> find ( 'h' ), '->find() returns a command if its name exists' );
$this -> assertInstanceOf ( 'FooCommand' , $application -> find ( 'f:bar' ), '->find() returns a command if the abbreviation for the namespace exists' );
$this -> assertInstanceOf ( 'FooCommand' , $application -> find ( 'f:b' ), '->find() returns a command if the abbreviation for the namespace and the command name exist' );
$this -> assertInstanceOf ( 'FooCommand' , $application -> find ( 'a' ), '->find() returns a command if the abbreviation exists for an alias' );
}
/**
* @ dataProvider provideAmbiguousAbbreviations
*/
public function testFindWithAmbiguousAbbreviations ( $abbreviation , $expectedExceptionMessage )
{
$this -> setExpectedException ( 'InvalidArgumentException' , $expectedExceptionMessage );
$application = new Application ();
$application -> add ( new \FooCommand ());
2010-11-23 08:42:19 +00:00
$application -> add ( new \Foo1Command ());
$application -> add ( new \Foo2Command ());
2010-05-06 12:25:53 +01:00
2013-02-05 21:55:33 +00:00
$application -> find ( $abbreviation );
}
2010-05-06 12:25:53 +01:00
2013-02-05 21:55:33 +00:00
public function provideAmbiguousAbbreviations ()
{
return array (
array ( 'f' , 'Command "f" is not defined.' ),
array ( 'a' , 'Command "a" is ambiguous (afoobar, afoobar1 and 1 more).' ),
2013-08-07 07:55:13 +01:00
array ( 'foo:b' , 'Command "foo:b" is ambiguous (foo:bar, foo:bar1 and 1 more).' )
2013-02-05 21:55:33 +00:00
);
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2013-04-26 23:56:13 +01:00
public function testFindCommandEqualNamespace ()
{
$application = new Application ();
$application -> add ( new \Foo3Command ());
$application -> add ( new \Foo4Command ());
$this -> assertInstanceOf ( 'Foo3Command' , $application -> find ( 'foo3:bar' ), '->find() returns the good command even if a namespace has same name' );
$this -> assertInstanceOf ( 'Foo4Command' , $application -> find ( 'foo3:bar:toh' ), '->find() returns a command even if its namespace equals another command name' );
}
2013-08-07 07:55:13 +01:00
public function testFindCommandWithAmbiguousNamespacesButUniqueName ()
{
$application = new Application ();
$application -> add ( new \FooCommand ());
$application -> add ( new \FoobarCommand ());
$this -> assertInstanceOf ( 'FoobarCommand' , $application -> find ( 'f:f' ));
}
public function testFindCommandWithMissingNamespace ()
{
$application = new Application ();
$application -> add ( new \Foo4Command ());
$this -> assertInstanceOf ( 'Foo4Command' , $application -> find ( 'f::t' ));
}
2013-02-05 21:55:33 +00:00
/**
* @ dataProvider provideInvalidCommandNamesSingle
* @ expectedException \InvalidArgumentException
* @ expectedExceptionMessage Did you mean this
*/
public function testFindAlternativeExceptionMessageSingle ( $name )
2012-08-07 15:15:32 +01:00
{
$application = new Application ();
2013-08-07 07:55:13 +01:00
$application -> add ( new \Foo3Command ());
2013-02-05 21:55:33 +00:00
$application -> find ( $name );
}
2012-08-07 15:15:32 +01:00
2013-02-05 21:55:33 +00:00
public function provideInvalidCommandNamesSingle ()
{
return array (
2013-08-07 07:55:13 +01:00
array ( 'foo3:baR' ),
array ( 'foO3:bar' )
2013-02-05 21:55:33 +00:00
);
}
2012-08-07 15:15:32 +01:00
2013-02-05 21:55:33 +00:00
public function testFindAlternativeExceptionMessageMultiple ()
{
$application = new Application ();
$application -> add ( new \FooCommand ());
2012-08-07 15:15:32 +01:00
$application -> add ( new \Foo1Command ());
$application -> add ( new \Foo2Command ());
// Command + plural
try {
$application -> find ( 'foo:baR' );
$this -> fail ( '->find() throws an \InvalidArgumentException if command does not exist, with alternatives' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->find() throws an \InvalidArgumentException if command does not exist, with alternatives' );
$this -> assertRegExp ( '/Did you mean one of these/' , $e -> getMessage (), '->find() throws an \InvalidArgumentException if command does not exist, with alternatives' );
2013-08-07 07:55:13 +01:00
$this -> assertRegExp ( '/foo1:bar/' , $e -> getMessage ());
$this -> assertRegExp ( '/foo:bar/' , $e -> getMessage ());
2012-08-07 15:15:32 +01:00
}
// Namespace + plural
try {
$application -> find ( 'foo2:bar' );
$this -> fail ( '->find() throws an \InvalidArgumentException if command does not exist, with alternatives' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->find() throws an \InvalidArgumentException if command does not exist, with alternatives' );
$this -> assertRegExp ( '/Did you mean one of these/' , $e -> getMessage (), '->find() throws an \InvalidArgumentException if command does not exist, with alternatives' );
2013-08-07 07:55:13 +01:00
$this -> assertRegExp ( '/foo1/' , $e -> getMessage ());
2012-08-07 15:15:32 +01:00
}
2012-10-09 22:27:04 +01:00
$application -> add ( new \Foo3Command ());
$application -> add ( new \Foo4Command ());
// Subnamespace + plural
try {
$a = $application -> find ( 'foo3:' );
$this -> fail ( '->find() should throw an \InvalidArgumentException if a command is ambiguous because of a subnamespace, with alternatives' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e );
$this -> assertRegExp ( '/foo3:bar/' , $e -> getMessage ());
$this -> assertRegExp ( '/foo3:bar:toh/' , $e -> getMessage ());
}
2012-08-07 15:15:32 +01:00
}
2012-02-11 01:58:39 +00:00
public function testFindAlternativeCommands ()
{
$application = new Application ();
$application -> add ( new \FooCommand ());
$application -> add ( new \Foo1Command ());
$application -> add ( new \Foo2Command ());
try {
2012-07-28 23:02:29 +01:00
$application -> find ( $commandName = 'Unknown command' );
2012-02-11 01:58:39 +00:00
$this -> fail ( '->find() throws an \InvalidArgumentException if command does not exist' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->find() throws an \InvalidArgumentException if command does not exist' );
$this -> assertEquals ( sprintf ( 'Command "%s" is not defined.' , $commandName ), $e -> getMessage (), '->find() throws an \InvalidArgumentException if command does not exist, without alternatives' );
}
2013-08-07 07:55:13 +01:00
// Test if "bar1" command throw an "\InvalidArgumentException" and does not contain
// "foo:bar" as alternative because "bar1" is too far from "foo:bar"
2012-02-11 01:58:39 +00:00
try {
2013-08-07 07:55:13 +01:00
$application -> find ( $commandName = 'bar1' );
2012-02-11 01:58:39 +00:00
$this -> fail ( '->find() throws an \InvalidArgumentException if command does not exist' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->find() throws an \InvalidArgumentException if command does not exist' );
$this -> assertRegExp ( sprintf ( '/Command "%s" is not defined./' , $commandName ), $e -> getMessage (), '->find() throws an \InvalidArgumentException if command does not exist, with alternatives' );
2013-08-07 07:55:13 +01:00
$this -> assertRegExp ( '/afoobar1/' , $e -> getMessage (), '->find() throws an \InvalidArgumentException if command does not exist, with alternative : "afoobar1"' );
2012-02-11 01:58:39 +00:00
$this -> assertRegExp ( '/foo:bar1/' , $e -> getMessage (), '->find() throws an \InvalidArgumentException if command does not exist, with alternative : "foo:bar1"' );
2013-08-07 07:55:13 +01:00
$this -> assertNotRegExp ( '/foo:bar(?>!1)/' , $e -> getMessage (), '->find() throws an \InvalidArgumentException if command does not exist, without "foo:bar" alternative' );
2012-02-11 01:58:39 +00:00
}
}
2012-02-11 19:38:08 +00:00
public function testFindAlternativeNamespace ()
{
$application = new Application ();
$application -> add ( new \FooCommand ());
$application -> add ( new \Foo1Command ());
$application -> add ( new \Foo2Command ());
$application -> add ( new \foo3Command ());
try {
2012-07-28 23:02:29 +01:00
$application -> find ( 'Unknown-namespace:Unknown-command' );
2012-02-11 19:38:08 +00:00
$this -> fail ( '->find() throws an \InvalidArgumentException if namespace does not exist' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->find() throws an \InvalidArgumentException if namespace does not exist' );
2012-07-28 23:02:29 +01:00
$this -> assertEquals ( 'There are no commands defined in the "Unknown-namespace" namespace.' , $e -> getMessage (), '->find() throws an \InvalidArgumentException if namespace does not exist, without alternatives' );
2012-02-11 19:38:08 +00:00
}
try {
$application -> find ( 'foo2:command' );
$this -> fail ( '->find() throws an \InvalidArgumentException if namespace does not exist' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->find() throws an \InvalidArgumentException if namespace does not exist' );
$this -> assertRegExp ( '/There are no commands defined in the "foo2" namespace./' , $e -> getMessage (), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative' );
$this -> assertRegExp ( '/foo/' , $e -> getMessage (), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative : "foo"' );
$this -> assertRegExp ( '/foo1/' , $e -> getMessage (), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative : "foo1"' );
$this -> assertRegExp ( '/foo3/' , $e -> getMessage (), '->find() throws an \InvalidArgumentException if namespace does not exist, with alternative : "foo3"' );
}
}
2013-04-07 15:34:20 +01:00
public function testFindNamespaceDoesNotFailOnDeepSimilarNamespaces ()
{
$application = $this -> getMock ( 'Symfony\Component\Console\Application' , array ( 'getNamespaces' ));
$application -> expects ( $this -> once ())
-> method ( 'getNamespaces' )
-> will ( $this -> returnValue ( array ( 'foo:sublong' , 'bar:sub' )));
$this -> assertEquals ( 'foo:sublong' , $application -> findNamespace ( 'f:sub' ));
}
2010-05-06 12:25:53 +01:00
public function testSetCatchExceptions ()
{
2012-04-06 17:24:40 +01:00
$application = $this -> getMock ( 'Symfony\Component\Console\Application' , array ( 'getTerminalWidth' ));
2010-05-06 12:25:53 +01:00
$application -> setAutoExit ( false );
2012-04-06 17:24:40 +01:00
$application -> expects ( $this -> any ())
-> method ( 'getTerminalWidth' )
-> will ( $this -> returnValue ( 120 ));
2010-05-06 12:25:53 +01:00
$tester = new ApplicationTester ( $application );
$application -> setCatchExceptions ( true );
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( 'command' => 'foo' ), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_renderexception1.txt' , $tester -> getDisplay ( true ), '->setCatchExceptions() sets the catch exception flag' );
2010-05-06 12:25:53 +01:00
$application -> setCatchExceptions ( false );
2010-05-07 15:09:11 +01:00
try {
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( 'command' => 'foo' ), array ( 'decorated' => false ));
2010-05-06 12:25:53 +01:00
$this -> fail ( '->setCatchExceptions() sets the catch exception flag' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\Exception' , $e , '->setCatchExceptions() sets the catch exception flag' );
$this -> assertEquals ( 'Command "foo" is not defined.' , $e -> getMessage (), '->setCatchExceptions() sets the catch exception flag' );
}
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
public function testAsText ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ();
2010-11-23 08:42:19 +00:00
$application -> add ( new \FooCommand );
2012-02-17 17:58:01 +00:00
$this -> ensureStaticCommandHelp ( $application );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_astext1.txt' , $this -> normalizeLineBreaks ( $application -> asText ()), '->asText() returns a text representation of the application' );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_astext2.txt' , $this -> normalizeLineBreaks ( $application -> asText ( 'foo' )), '->asText() returns a text representation of the application' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testAsXml ()
{
$application = new Application ();
2010-11-23 08:42:19 +00:00
$application -> add ( new \FooCommand );
2012-02-17 17:58:01 +00:00
$this -> ensureStaticCommandHelp ( $application );
2013-04-11 20:38:29 +01:00
$this -> assertXmlStringEqualsXmlFile ( self :: $fixturesPath . '/application_asxml1.txt' , $application -> asXml (), '->asXml() returns an XML representation of the application' );
$this -> assertXmlStringEqualsXmlFile ( self :: $fixturesPath . '/application_asxml2.txt' , $application -> asXml ( 'foo' ), '->asXml() returns an XML representation of the application' );
2010-05-06 12:25:53 +01:00
}
public function testRenderException ()
{
2012-04-06 17:24:40 +01:00
$application = $this -> getMock ( 'Symfony\Component\Console\Application' , array ( 'getTerminalWidth' ));
2010-05-06 12:25:53 +01:00
$application -> setAutoExit ( false );
2012-04-06 17:24:40 +01:00
$application -> expects ( $this -> any ())
-> method ( 'getTerminalWidth' )
-> will ( $this -> returnValue ( 120 ));
2010-05-06 12:25:53 +01:00
$tester = new ApplicationTester ( $application );
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( 'command' => 'foo' ), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_renderexception1.txt' , $tester -> getDisplay ( true ), '->renderException() renders a pretty exception' );
2010-05-06 12:25:53 +01:00
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( 'command' => 'foo' ), array ( 'decorated' => false , 'verbosity' => Output :: VERBOSITY_VERBOSE ));
2012-02-17 17:58:01 +00:00
$this -> assertContains ( 'Exception trace' , $tester -> getDisplay (), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose' );
2010-05-06 12:25:53 +01:00
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( 'command' => 'list' , '--foo' => true ), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_renderexception2.txt' , $tester -> getDisplay ( true ), '->renderException() renders the command synopsis when an exception occurs in the context of a command' );
2011-01-11 07:49:57 +00:00
$application -> add ( new \Foo3Command );
$tester = new ApplicationTester ( $application );
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( 'command' => 'foo3:bar' ), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_renderexception3.txt' , $tester -> getDisplay ( true ), '->renderException() renders a pretty exceptions with previous exceptions' );
2011-01-11 07:49:57 +00:00
2013-08-25 14:33:45 +01:00
$tester -> run ( array ( 'command' => 'foo3:bar' ), array ( 'decorated' => true ));
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_renderexception3decorated.txt' , $tester -> getDisplay ( true ), '->renderException() renders a pretty exceptions with previous exceptions' );
2012-04-06 13:21:18 +01:00
$application = $this -> getMock ( 'Symfony\Component\Console\Application' , array ( 'getTerminalWidth' ));
$application -> setAutoExit ( false );
$application -> expects ( $this -> any ())
-> method ( 'getTerminalWidth' )
-> will ( $this -> returnValue ( 32 ));
$tester = new ApplicationTester ( $application );
$tester -> run ( array ( 'command' => 'foo' ), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_renderexception4.txt' , $tester -> getDisplay ( true ), '->renderException() wraps messages when they are bigger than the terminal' );
2010-05-06 12:25:53 +01:00
}
public function testRun ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
2010-11-23 08:42:19 +00:00
$application -> add ( $command = new \Foo1Command ());
2010-05-06 12:25:53 +01:00
$_SERVER [ 'argv' ] = array ( 'cli.php' , 'foo:bar1' );
ob_start ();
$application -> run ();
ob_end_clean ();
2012-02-12 15:37:55 +00:00
$this -> assertSame ( 'Symfony\Component\Console\Input\ArgvInput' , get_class ( $command -> input ), '->run() creates an ArgvInput by default if none is given' );
$this -> assertSame ( 'Symfony\Component\Console\Output\ConsoleOutput' , get_class ( $command -> output ), '->run() creates a ConsoleOutput by default if none is given' );
2010-05-06 12:25:53 +01:00
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
2012-03-21 04:15:35 +00:00
$this -> ensureStaticCommandHelp ( $application );
2010-05-06 12:25:53 +01:00
$tester = new ApplicationTester ( $application );
2012-02-12 15:37:55 +00:00
2011-07-22 20:02:15 +01:00
$tester -> run ( array (), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_run1.txt' , $tester -> getDisplay ( true ), '->run() runs the list command if no argument is passed' );
2010-05-06 12:25:53 +01:00
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( '--help' => true ), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_run2.txt' , $tester -> getDisplay ( true ), '->run() runs the help command if --help is passed' );
2010-05-06 12:25:53 +01:00
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( '-h' => true ), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_run2.txt' , $tester -> getDisplay ( true ), '->run() runs the help command if -h is passed' );
2010-07-17 11:54:14 +01:00
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( 'command' => 'list' , '--help' => true ), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_run3.txt' , $tester -> getDisplay ( true ), '->run() displays the help if --help is passed' );
2010-05-06 12:25:53 +01:00
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( 'command' => 'list' , '-h' => true ), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_run3.txt' , $tester -> getDisplay ( true ), '->run() displays the help if -h is passed' );
2010-07-17 11:54:14 +01:00
2010-07-01 19:19:54 +01:00
$tester -> run ( array ( '--ansi' => true ));
$this -> assertTrue ( $tester -> getOutput () -> isDecorated (), '->run() forces color output if --ansi is passed' );
2010-05-06 12:25:53 +01:00
2011-06-08 09:26:48 +01:00
$tester -> run ( array ( '--no-ansi' => true ));
$this -> assertFalse ( $tester -> getOutput () -> isDecorated (), '->run() forces color output to be disabled if --no-ansi is passed' );
2010-07-17 11:58:23 +01:00
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( '--version' => true ), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_run4.txt' , $tester -> getDisplay ( true ), '->run() displays the program version if --version is passed' );
2010-05-06 12:25:53 +01:00
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( '-V' => true ), array ( 'decorated' => false ));
2013-03-24 09:34:03 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/application_run4.txt' , $tester -> getDisplay ( true ), '->run() displays the program version if -v is passed' );
2010-07-17 11:58:23 +01:00
2010-05-06 12:25:53 +01:00
$tester -> run ( array ( 'command' => 'list' , '--quiet' => true ));
2012-02-12 15:37:55 +00:00
$this -> assertSame ( '' , $tester -> getDisplay (), '->run() removes all output if --quiet is passed' );
2010-05-06 12:25:53 +01:00
2010-07-17 11:58:23 +01:00
$tester -> run ( array ( 'command' => 'list' , '-q' => true ));
2012-02-12 15:37:55 +00:00
$this -> assertSame ( '' , $tester -> getDisplay (), '->run() removes all output if -q is passed' );
2010-07-17 11:58:23 +01:00
2010-05-06 12:25:53 +01:00
$tester -> run ( array ( 'command' => 'list' , '--verbose' => true ));
2012-02-12 15:37:55 +00:00
$this -> assertSame ( Output :: VERBOSITY_VERBOSE , $tester -> getOutput () -> getVerbosity (), '->run() sets the output to verbose if --verbose is passed' );
2010-05-06 12:25:53 +01:00
2013-08-23 14:30:13 +01:00
$tester -> run ( array ( 'command' => 'list' , '--verbose' => 0 ));
$this -> assertSame ( Output :: VERBOSITY_VERBOSE , $tester -> getOutput () -> getVerbosity (), '->run() sets the output to verbose if smaller --verbose level is passed' );
2013-04-11 12:08:21 +01:00
$tester -> run ( array ( 'command' => 'list' , '--verbose' => 1 ));
2013-04-11 20:38:29 +01:00
$this -> assertSame ( Output :: VERBOSITY_VERBOSE , $tester -> getOutput () -> getVerbosity (), '->run() sets the output to verbose if --verbose=1 is passed' );
2013-04-11 12:08:21 +01:00
$tester -> run ( array ( 'command' => 'list' , '--verbose' => 2 ));
$this -> assertSame ( Output :: VERBOSITY_VERY_VERBOSE , $tester -> getOutput () -> getVerbosity (), '->run() sets the output to very verbose if --verbose=2 is passed' );
$tester -> run ( array ( 'command' => 'list' , '--verbose' => 3 ));
$this -> assertSame ( Output :: VERBOSITY_DEBUG , $tester -> getOutput () -> getVerbosity (), '->run() sets the output to debug if --verbose=3 is passed' );
$tester -> run ( array ( 'command' => 'list' , '--verbose' => 4 ));
2013-08-23 14:30:13 +01:00
$this -> assertSame ( Output :: VERBOSITY_DEBUG , $tester -> getOutput () -> getVerbosity (), '->run() sets the output to debug if greater --verbose level is passed' );
2013-04-11 12:08:21 +01:00
2010-07-17 11:58:23 +01:00
$tester -> run ( array ( 'command' => 'list' , '-v' => true ));
2012-02-12 15:37:55 +00:00
$this -> assertSame ( Output :: VERBOSITY_VERBOSE , $tester -> getOutput () -> getVerbosity (), '->run() sets the output to verbose if -v is passed' );
2010-07-17 11:58:23 +01:00
2013-04-25 11:15:38 +01:00
$tester -> run ( array ( 'command' => 'list' , '-vv' => true ));
$this -> assertSame ( Output :: VERBOSITY_VERY_VERBOSE , $tester -> getOutput () -> getVerbosity (), '->run() sets the output to verbose if -v is passed' );
$tester -> run ( array ( 'command' => 'list' , '-vvv' => true ));
$this -> assertSame ( Output :: VERBOSITY_DEBUG , $tester -> getOutput () -> getVerbosity (), '->run() sets the output to verbose if -v is passed' );
2010-05-06 12:25:53 +01:00
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
2010-11-23 08:42:19 +00:00
$application -> add ( new \FooCommand ());
2010-05-06 12:25:53 +01:00
$tester = new ApplicationTester ( $application );
2012-02-12 15:37:55 +00:00
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( 'command' => 'foo:bar' , '--no-interaction' => true ), array ( 'decorated' => false ));
2012-02-12 15:37:55 +00:00
$this -> assertSame ( 'called' . PHP_EOL , $tester -> getDisplay (), '->run() does not call interact() if --no-interaction is passed' );
2010-07-17 11:58:23 +01:00
2011-07-22 20:02:15 +01:00
$tester -> run ( array ( 'command' => 'foo:bar' , '-n' => true ), array ( 'decorated' => false ));
2012-02-12 15:37:55 +00:00
$this -> assertSame ( 'called' . PHP_EOL , $tester -> getDisplay (), '->run() does not call interact() if -n is passed' );
2010-03-18 07:48:03 +00:00
}
2011-11-07 16:22:50 +00:00
2013-05-17 22:54:02 +01:00
public function testRunReturnsIntegerExitCode ()
{
$exception = new \Exception ( '' , 4 );
$application = $this -> getMock ( 'Symfony\Component\Console\Application' , array ( 'doRun' ));
$application -> setAutoExit ( false );
$application -> expects ( $this -> once ())
-> method ( 'doRun' )
-> will ( $this -> throwException ( $exception ));
$exitCode = $application -> run ( new ArrayInput ( array ()), new NullOutput ());
$this -> assertSame ( 4 , $exitCode , '->run() returns integer exit code extracted from raised exception' );
}
2013-06-03 14:33:17 +01:00
public function testRunReturnsExitCodeOneForExceptionCodeZero ()
{
$exception = new \Exception ( '' , 0 );
$application = $this -> getMock ( 'Symfony\Component\Console\Application' , array ( 'doRun' ));
$application -> setAutoExit ( false );
$application -> expects ( $this -> once ())
-> method ( 'doRun' )
-> will ( $this -> throwException ( $exception ));
$exitCode = $application -> run ( new ArrayInput ( array ()), new NullOutput ());
$this -> assertSame ( 1 , $exitCode , '->run() returns exit code 1 when exception code is 0' );
}
2011-11-07 16:22:50 +00:00
/**
* @ expectedException \LogicException
* @ dataProvider getAddingAlreadySetDefinitionElementData
*/
public function testAddingAlreadySetDefinitionElementData ( $def )
{
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
$application
-> register ( 'foo' )
-> setDefinition ( array ( $def ))
-> setCode ( function ( InputInterface $input , OutputInterface $output ) {})
;
$input = new ArrayInput ( array ( 'command' => 'foo' ));
$output = new NullOutput ();
$application -> run ( $input , $output );
}
public function getAddingAlreadySetDefinitionElementData ()
{
return array (
array ( new InputArgument ( 'command' , InputArgument :: REQUIRED )),
array ( new InputOption ( 'quiet' , '' , InputOption :: VALUE_NONE )),
array ( new InputOption ( 'query' , 'q' , InputOption :: VALUE_NONE )),
);
}
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
public function testGetDefaultHelperSetReturnsDefaultValues ()
{
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$helperSet = $application -> getHelperSet ();
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$this -> assertTrue ( $helperSet -> has ( 'formatter' ));
$this -> assertTrue ( $helperSet -> has ( 'dialog' ));
$this -> assertTrue ( $helperSet -> has ( 'progress' ));
}
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
public function testAddingSingleHelperSetOverwritesDefaultValues ()
{
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$application -> setHelperSet ( new HelperSet ( array ( new FormatterHelper ())));
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$helperSet = $application -> getHelperSet ();
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$this -> assertTrue ( $helperSet -> has ( 'formatter' ));
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
// no other default helper set should be returned
$this -> assertFalse ( $helperSet -> has ( 'dialog' ));
$this -> assertFalse ( $helperSet -> has ( 'progress' ));
}
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
public function testOverwritingDefaultHelperSetOverwritesDefaultValues ()
{
$application = new CustomApplication ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$application -> setHelperSet ( new HelperSet ( array ( new FormatterHelper ())));
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$helperSet = $application -> getHelperSet ();
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$this -> assertTrue ( $helperSet -> has ( 'formatter' ));
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
// no other default helper set should be returned
$this -> assertFalse ( $helperSet -> has ( 'dialog' ));
$this -> assertFalse ( $helperSet -> has ( 'progress' ));
}
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
public function testGetDefaultInputDefinitionReturnsDefaultValues ()
{
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$inputDefinition = $application -> getDefinition ();
$this -> assertTrue ( $inputDefinition -> hasArgument ( 'command' ));
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$this -> assertTrue ( $inputDefinition -> hasOption ( 'help' ));
$this -> assertTrue ( $inputDefinition -> hasOption ( 'quiet' ));
$this -> assertTrue ( $inputDefinition -> hasOption ( 'verbose' ));
$this -> assertTrue ( $inputDefinition -> hasOption ( 'version' ));
$this -> assertTrue ( $inputDefinition -> hasOption ( 'ansi' ));
$this -> assertTrue ( $inputDefinition -> hasOption ( 'no-ansi' ));
$this -> assertTrue ( $inputDefinition -> hasOption ( 'no-interaction' ));
}
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
public function testOverwritingDefaultInputDefinitionOverwritesDefaultValues ()
{
$application = new CustomApplication ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$inputDefinition = $application -> getDefinition ();
2012-10-28 23:25:34 +00:00
// check whether the default arguments and options are not returned any more
2012-10-01 14:15:04 +01:00
$this -> assertFalse ( $inputDefinition -> hasArgument ( 'command' ));
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$this -> assertFalse ( $inputDefinition -> hasOption ( 'help' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'quiet' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'verbose' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'version' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'ansi' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'no-ansi' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'no-interaction' ));
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$this -> assertTrue ( $inputDefinition -> hasOption ( 'custom' ));
}
2012-12-11 10:49:22 +00:00
2012-10-01 14:15:04 +01:00
public function testSettingCustomInputDefinitionOverwritesDefaultValues ()
{
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
2012-12-11 10:49:22 +00:00
2012-10-01 14:15:04 +01:00
$application -> setDefinition ( new InputDefinition ( array ( new InputOption ( '--custom' , '-c' , InputOption :: VALUE_NONE , 'Set the custom input definition.' ))));
2012-12-11 10:49:22 +00:00
2012-10-01 14:15:04 +01:00
$inputDefinition = $application -> getDefinition ();
2012-10-28 23:25:34 +00:00
// check whether the default arguments and options are not returned any more
2012-10-01 14:15:04 +01:00
$this -> assertFalse ( $inputDefinition -> hasArgument ( 'command' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'help' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'quiet' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'verbose' ));
2012-10-01 14:15:04 +01:00
$this -> assertFalse ( $inputDefinition -> hasOption ( 'version' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'ansi' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'no-ansi' ));
$this -> assertFalse ( $inputDefinition -> hasOption ( 'no-interaction' ));
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
$this -> assertTrue ( $inputDefinition -> hasOption ( 'custom' ));
}
2013-03-24 09:08:12 +00:00
public function testRunWithDispatcher ()
{
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setDispatcher ( $this -> getDispatcher ());
$application -> register ( 'foo' ) -> setCode ( function ( InputInterface $input , OutputInterface $output ) {
$output -> write ( 'foo.' );
});
$tester = new ApplicationTester ( $application );
$tester -> run ( array ( 'command' => 'foo' ));
$this -> assertEquals ( 'before.foo.after.' , $tester -> getDisplay ());
}
/**
* @ expectedException \LogicException
* @ expectedExceptionMessage caught
*/
public function testRunWithExceptionAndDispatcher ()
{
$application = new Application ();
$application -> setDispatcher ( $this -> getDispatcher ());
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
$application -> register ( 'foo' ) -> setCode ( function ( InputInterface $input , OutputInterface $output ) {
throw new \RuntimeException ( 'foo' );
});
$tester = new ApplicationTester ( $application );
$tester -> run ( array ( 'command' => 'foo' ));
}
public function testRunDispatchesAllEventsWithException ()
{
$application = new Application ();
$application -> setDispatcher ( $this -> getDispatcher ());
$application -> setAutoExit ( false );
$application -> register ( 'foo' ) -> setCode ( function ( InputInterface $input , OutputInterface $output ) {
$output -> write ( 'foo.' );
throw new \RuntimeException ( 'foo' );
});
$tester = new ApplicationTester ( $application );
$tester -> run ( array ( 'command' => 'foo' ));
$this -> assertContains ( 'before.foo.after.caught.' , $tester -> getDisplay ());
}
2013-07-29 22:04:28 +01:00
public function testTerminalDimensions ()
{
$application = new Application ();
$originalDimensions = $application -> getTerminalDimensions ();
$this -> assertCount ( 2 , $originalDimensions );
$width = 80 ;
if ( $originalDimensions [ 0 ] == $width ) {
$width = 100 ;
}
$application -> setTerminalDimensions ( $width , 80 );
$this -> assertSame ( array ( $width , 80 ), $application -> getTerminalDimensions ());
}
2013-03-24 09:08:12 +00:00
protected function getDispatcher ()
{
$dispatcher = new EventDispatcher ;
$dispatcher -> addListener ( 'console.command' , function ( ConsoleCommandEvent $event ) {
$event -> getOutput () -> write ( 'before.' );
});
$dispatcher -> addListener ( 'console.terminate' , function ( ConsoleTerminateEvent $event ) {
$event -> getOutput () -> write ( 'after.' );
$event -> setExitCode ( 128 );
});
2013-05-27 11:36:30 +01:00
$dispatcher -> addListener ( 'console.exception' , function ( ConsoleExceptionEvent $event ) {
2013-03-24 09:08:12 +00:00
$event -> getOutput () -> writeln ( 'caught.' );
$event -> setException ( new \LogicException ( 'caught.' , $event -> getExitCode (), $event -> getException ()));
});
return $dispatcher ;
}
2010-03-18 07:48:03 +00:00
}
2012-10-01 14:15:04 +01:00
class CustomApplication extends Application
{
/**
* Overwrites the default input definition .
*
* @ return InputDefinition An InputDefinition instance
*/
protected function getDefaultInputDefinition ()
{
return new InputDefinition ( array ( new InputOption ( '--custom' , '-c' , InputOption :: VALUE_NONE , 'Set the custom input definition.' )));
}
2012-10-04 07:44:49 +01:00
2012-10-01 14:15:04 +01:00
/**
* Gets the default helper set with the helpers that should always be available .
*
* @ return HelperSet A HelperSet instance
*/
protected function getDefaultHelperSet ()
{
return new HelperSet ( array ( new FormatterHelper ()));
}
2010-03-18 07:48:03 +00:00
}