2010-01-04 14:42:28 +00:00
< ? php
/*
* This file is part of the symfony package .
* ( c ) Fabien Potencier < fabien . potencier @ symfony - project . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
require_once __DIR__ . '/../../../bootstrap.php' ;
2010-01-09 11:57:15 +00:00
use Symfony\Components\Console\Application ;
use Symfony\Components\Console\Input\ArrayInput ;
use Symfony\Components\Console\Output\Output ;
use Symfony\Components\Console\Output\StreamOutput ;
use Symfony\Components\Console\Tester\ApplicationTester ;
2010-01-04 14:42:28 +00:00
2010-01-09 11:57:15 +00:00
$fixtures = __DIR__ . '/../../../../fixtures/Symfony/Components/Console' ;
2010-01-04 14:42:28 +00:00
2010-01-04 18:51:21 +00:00
require_once $fixtures . '/FooCommand.php' ;
require_once $fixtures . '/Foo1Command.php' ;
require_once $fixtures . '/Foo2Command.php' ;
2010-01-04 14:42:28 +00:00
$t = new LimeTest ( 52 );
// __construct()
$t -> diag ( '__construct()' );
$application = new Application ( 'foo' , 'bar' );
$t -> is ( $application -> getName (), 'foo' , '__construct() takes the application name as its first argument' );
$t -> is ( $application -> getVersion (), 'bar' , '__construct() takes the application version as its first argument' );
2010-01-04 18:51:21 +00:00
$t -> is ( array_keys ( $application -> getCommands ()), array ( 'help' , 'list' ), '__construct() registered the help and list commands by default' );
2010-01-04 14:42:28 +00:00
// ->setName() ->getName()
$t -> diag ( '->setName() ->getName()' );
$application = new Application ();
$application -> setName ( 'foo' );
$t -> is ( $application -> getName (), 'foo' , '->setName() sets the name of the application' );
// ->getVersion() ->getVersion()
$t -> diag ( '->getVersion() ->getVersion()' );
$application = new Application ();
$application -> setVersion ( 'bar' );
$t -> is ( $application -> getVersion (), 'bar' , '->setVersion() sets the version of the application' );
// ->getLongVersion()
$t -> diag ( '->getLongVersion()' );
$application = new Application ( 'foo' , 'bar' );
$t -> is ( $application -> getLongVersion (), '<info>foo</info> version <comment>bar</comment>' , '->getLongVersion() returns the long version of the application' );
// ->getHelp()
$t -> diag ( '->getHelp()' );
$application = new Application ();
$t -> is ( $application -> getHelp (), file_get_contents ( $fixtures . '/application_gethelp.txt' ), '->setHelp() returns a help message' );
2010-01-04 18:51:21 +00:00
// ->getCommands()
$t -> diag ( '->getCommands()' );
2010-01-04 14:42:28 +00:00
$application = new Application ();
2010-01-04 18:51:21 +00:00
$commands = $application -> getCommands ();
2010-01-09 11:57:15 +00:00
$t -> is ( get_class ( $commands [ 'help' ]), 'Symfony\\Components\\Console\\Command\\HelpCommand' , '->getCommands() returns the registered commands' );
2010-01-04 14:42:28 +00:00
2010-01-04 18:51:21 +00:00
$application -> addCommand ( new FooCommand ());
$commands = $application -> getCommands ( 'foo' );
$t -> is ( count ( $commands ), 1 , '->getCommands() takes a namespace as its first argument' );
2010-01-04 14:42:28 +00:00
// ->register()
$t -> diag ( '->register()' );
$application = new Application ();
2010-01-04 18:51:21 +00:00
$command = $application -> register ( 'foo' );
$t -> is ( $command -> getName (), 'foo' , '->register() regiters a new command' );
2010-01-04 14:42:28 +00:00
2010-01-04 18:51:21 +00:00
// ->addCommand() ->addCommands()
$t -> diag ( '->addCommand() ->addCommands()' );
2010-01-04 14:42:28 +00:00
$application = new Application ();
2010-01-04 18:51:21 +00:00
$application -> addCommand ( $foo = new FooCommand ());
$commands = $application -> getCommands ();
$t -> is ( $commands [ 'foo:bar' ], $foo , '->addCommand() registers a command' );
2010-01-04 14:42:28 +00:00
$application = new Application ();
2010-01-04 18:51:21 +00:00
$application -> addCommands ( array ( $foo = new FooCommand (), $foo1 = new Foo1Command ()));
$commands = $application -> getCommands ();
$t -> is ( array ( $commands [ 'foo:bar' ], $commands [ 'foo:bar1' ]), array ( $foo , $foo1 ), '->addCommands() registers an array of commands' );
2010-01-04 14:42:28 +00:00
2010-01-04 18:51:21 +00:00
// ->hasCommand() ->getCommand()
$t -> diag ( '->hasCommand() ->getCommand()' );
2010-01-04 14:42:28 +00:00
$application = new Application ();
2010-01-04 18:51:21 +00:00
$t -> ok ( $application -> hasCommand ( 'list' ), '->hasCommand() returns true if a named command is registered' );
$t -> ok ( ! $application -> hasCommand ( 'afoobar' ), '->hasCommand() returns false if a named command is not registered' );
2010-01-04 14:42:28 +00:00
2010-01-04 18:51:21 +00:00
$application -> addCommand ( $foo = new FooCommand ());
$t -> ok ( $application -> hasCommand ( 'afoobar' ), '->hasCommand() returns true if an alias is registered' );
$t -> is ( $application -> getCommand ( 'foo:bar' ), $foo , '->getCommand() returns a command by name' );
$t -> is ( $application -> getCommand ( 'afoobar' ), $foo , '->getCommand() returns a command by alias' );
2010-01-04 14:42:28 +00:00
try
{
2010-01-04 18:51:21 +00:00
$application -> getCommand ( 'foofoo' );
$t -> fail ( '->getCommand() throws an \InvalidArgumentException if the command does not exist' );
2010-01-04 14:42:28 +00:00
}
catch ( \InvalidArgumentException $e )
{
2010-01-04 18:51:21 +00:00
$t -> pass ( '->getCommand() throws an \InvalidArgumentException if the command does not exist' );
2010-01-04 14:42:28 +00:00
}
class TestApplication extends Application
{
public function setWantHelps ()
{
$this -> wantHelps = true ;
}
}
$application = new TestApplication ();
2010-01-04 18:51:21 +00:00
$application -> addCommand ( $foo = new FooCommand ());
2010-01-04 14:42:28 +00:00
$application -> setWantHelps ();
2010-01-04 18:51:21 +00:00
$command = $application -> getCommand ( 'foo:bar' );
2010-01-09 11:57:15 +00:00
$t -> is ( get_class ( $command ), 'Symfony\Components\Console\Command\HelpCommand' , '->getCommand() returns the help command if --help is provided as the input' );
2010-01-04 14:42:28 +00:00
// ->getNamespaces()
$t -> diag ( '->getNamespaces()' );
$application = new TestApplication ();
2010-01-04 18:51:21 +00:00
$application -> addCommand ( new FooCommand ());
$application -> addCommand ( new Foo1Command ());
2010-01-04 14:42:28 +00:00
$t -> is ( $application -> getNamespaces (), array ( 'foo' ), '->getNamespaces() returns an array of unique used namespaces' );
// ->findNamespace()
$t -> diag ( '->findNamespace()' );
$application = new TestApplication ();
2010-01-04 18:51:21 +00:00
$application -> addCommand ( new FooCommand ());
2010-01-04 14:42:28 +00:00
$t -> is ( $application -> findNamespace ( 'foo' ), 'foo' , '->findNamespace() returns the given namespace if it exists' );
$t -> is ( $application -> findNamespace ( 'f' ), 'foo' , '->findNamespace() finds a namespace given an abbreviation' );
2010-01-04 18:51:21 +00:00
$application -> addCommand ( new Foo2Command ());
2010-01-04 14:42:28 +00:00
$t -> is ( $application -> findNamespace ( 'foo' ), 'foo' , '->findNamespace() returns the given namespace if it exists' );
try
{
$application -> findNamespace ( 'f' );
$t -> fail ( '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous' );
}
catch ( \InvalidArgumentException $e )
{
$t -> pass ( '->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous' );
}
try
{
$application -> findNamespace ( 'bar' );
2010-01-04 18:51:21 +00:00
$t -> fail ( '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace' );
2010-01-04 14:42:28 +00:00
}
catch ( \InvalidArgumentException $e )
{
2010-01-04 18:51:21 +00:00
$t -> pass ( '->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace' );
2010-01-04 14:42:28 +00:00
}
2010-01-04 18:51:21 +00:00
// ->findCommand()
$t -> diag ( '->findCommand()' );
2010-01-04 14:42:28 +00:00
$application = new TestApplication ();
2010-01-04 18:51:21 +00:00
$application -> addCommand ( new FooCommand ());
$t -> is ( get_class ( $application -> findCommand ( 'foo:bar' )), 'FooCommand' , '->findCommand() returns a command if its name exists' );
2010-01-09 11:57:15 +00:00
$t -> is ( get_class ( $application -> findCommand ( 'h' )), 'Symfony\Components\Console\Command\HelpCommand' , '->findCommand() returns a command if its name exists' );
2010-01-04 18:51:21 +00:00
$t -> is ( get_class ( $application -> findCommand ( 'f:bar' )), 'FooCommand' , '->findCommand() returns a command if the abbreviation for the namespace exists' );
$t -> is ( get_class ( $application -> findCommand ( 'f:b' )), 'FooCommand' , '->findCommand() returns a command if the abbreviation for the namespace and the command name exist' );
$t -> is ( get_class ( $application -> findCommand ( 'a' )), 'FooCommand' , '->findCommand() returns a command if the abbreviation exists for an alias' );
2010-01-04 14:42:28 +00:00
2010-01-04 18:51:21 +00:00
$application -> addCommand ( new Foo1Command ());
$application -> addCommand ( new Foo2Command ());
2010-01-04 14:42:28 +00:00
try
{
2010-01-04 18:51:21 +00:00
$application -> findCommand ( 'f' );
$t -> fail ( '->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace' );
2010-01-04 14:42:28 +00:00
}
catch ( \InvalidArgumentException $e )
{
2010-01-04 18:51:21 +00:00
$t -> pass ( '->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace' );
2010-01-04 14:42:28 +00:00
}
try
{
2010-01-04 18:51:21 +00:00
$application -> findCommand ( 'a' );
$t -> fail ( '->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias' );
2010-01-04 14:42:28 +00:00
}
catch ( \InvalidArgumentException $e )
{
2010-01-04 18:51:21 +00:00
$t -> pass ( '->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias' );
2010-01-04 14:42:28 +00:00
}
try
{
2010-01-04 18:51:21 +00:00
$application -> findCommand ( 'foo:b' );
$t -> fail ( '->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command' );
2010-01-04 14:42:28 +00:00
}
catch ( \InvalidArgumentException $e )
{
2010-01-04 18:51:21 +00:00
$t -> pass ( '->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a taks' );
2010-01-04 14:42:28 +00:00
}
// ->setCatchExceptions()
$t -> diag ( '->setCatchExceptions()' );
$application = new Application ();
$application -> setAutoExit ( false );
$tester = new ApplicationTester ( $application );
$application -> setCatchExceptions ( true );
2010-01-04 18:51:21 +00:00
$tester -> run ( array ( 'command' => 'foo' ));
2010-01-04 14:42:28 +00:00
$t -> is ( $tester -> getDisplay (), file_get_contents ( $fixtures . '/application_renderexception1.txt' ), '->setCatchExceptions() sets the catch exception flag' );
$application -> setCatchExceptions ( false );
try
{
2010-01-04 18:51:21 +00:00
$tester -> run ( array ( 'command' => 'foo' ));
2010-01-04 14:42:28 +00:00
$t -> fail ( '->setCatchExceptions() sets the catch exception flag' );
}
catch ( \Exception $e )
{
$t -> pass ( '->setCatchExceptions() sets the catch exception flag' );
}
// ->asText()
$t -> diag ( '->asText()' );
$application = new Application ();
2010-01-04 18:51:21 +00:00
$application -> addCommand ( new FooCommand );
2010-01-04 14:42:28 +00:00
$t -> is ( $application -> asText (), file_get_contents ( $fixtures . '/application_astext1.txt' ), '->asText() returns a text representation of the application' );
$t -> is ( $application -> asText ( 'foo' ), file_get_contents ( $fixtures . '/application_astext2.txt' ), '->asText() returns a text representation of the application' );
// ->asXml()
$t -> diag ( '->asXml()' );
$application = new Application ();
2010-01-04 18:51:21 +00:00
$application -> addCommand ( new FooCommand );
2010-01-04 14:42:28 +00:00
$t -> is ( $application -> asXml (), file_get_contents ( $fixtures . '/application_asxml1.txt' ), '->asXml() returns an XML representation of the application' );
$t -> is ( $application -> asXml ( 'foo' ), file_get_contents ( $fixtures . '/application_asxml2.txt' ), '->asXml() returns an XML representation of the application' );
// ->renderException()
$t -> diag ( '->renderException()' );
$application = new Application ();
$application -> setAutoExit ( false );
$tester = new ApplicationTester ( $application );
2010-01-04 18:51:21 +00:00
$tester -> run ( array ( 'command' => 'foo' ));
2010-01-04 14:42:28 +00:00
$t -> is ( $tester -> getDisplay (), file_get_contents ( $fixtures . '/application_renderexception1.txt' ), '->renderException() renders a pretty exception' );
2010-01-04 18:51:21 +00:00
$tester -> run ( array ( 'command' => 'foo' ), array ( 'verbosity' => Output :: VERBOSITY_VERBOSE ));
2010-01-04 14:42:28 +00:00
$t -> like ( $tester -> getDisplay (), '/Exception trace/' , '->renderException() renders a pretty exception with a stack trace when verbosity is verbose' );
2010-01-04 18:51:21 +00:00
$tester -> run ( array ( 'command' => 'list' , '--foo' => true ));
$t -> is ( $tester -> getDisplay (), file_get_contents ( $fixtures . '/application_renderexception2.txt' ), '->renderException() renders the command synopsis when an exception occurs in the context of a command' );
2010-01-04 14:42:28 +00:00
// ->run()
$t -> diag ( '->run()' );
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
2010-01-04 18:51:21 +00:00
$application -> addCommand ( $command = new Foo1Command ());
2010-01-04 14:42:28 +00:00
$_SERVER [ 'argv' ] = array ( 'cli.php' , 'foo:bar1' );
ob_start ();
$application -> run ();
ob_end_clean ();
2010-01-09 11:57:15 +00:00
$t -> is ( get_class ( $command -> input ), 'Symfony\Components\Console\Input\ArgvInput' , '->run() creates an ArgvInput by default if none is given' );
$t -> is ( get_class ( $command -> output ), 'Symfony\Components\Console\Output\ConsoleOutput' , '->run() creates a ConsoleOutput by default if none is given' );
2010-01-04 14:42:28 +00:00
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
$tester = new ApplicationTester ( $application );
$tester -> run ( array ());
2010-01-04 18:51:21 +00:00
$t -> is ( $tester -> getDisplay (), file_get_contents ( $fixtures . '/application_run1.txt' ), '->run() runs the list command if no argument is passed' );
2010-01-04 14:42:28 +00:00
$tester -> run ( array ( '--help' => true ));
2010-01-04 18:51:21 +00:00
$t -> is ( $tester -> getDisplay (), file_get_contents ( $fixtures . '/application_run2.txt' ), '->run() runs the help command if --help is passed' );
2010-01-04 14:42:28 +00:00
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
$tester = new ApplicationTester ( $application );
2010-01-04 18:51:21 +00:00
$tester -> run ( array ( 'command' => 'list' , '--help' => true ));
2010-01-04 14:42:28 +00:00
$t -> is ( $tester -> getDisplay (), file_get_contents ( $fixtures . '/application_run3.txt' ), '->run() displays the help if --help is passed' );
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
$tester = new ApplicationTester ( $application );
$tester -> run ( array ( '--color' => true ));
$t -> ok ( $tester -> getOutput () -> isDecorated (), '->run() forces color output if --color is passed' );
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
$tester = new ApplicationTester ( $application );
$tester -> run ( array ( '--version' => true ));
$t -> is ( $tester -> getDisplay (), file_get_contents ( $fixtures . '/application_run4.txt' ), '->run() displays the program version if --version is passed' );
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
$tester = new ApplicationTester ( $application );
2010-01-04 18:51:21 +00:00
$tester -> run ( array ( 'command' => 'list' , '--quiet' => true ));
2010-01-04 14:42:28 +00:00
$t -> is ( $tester -> getDisplay (), '' , '->run() removes all output if --quiet is passed' );
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
$tester = new ApplicationTester ( $application );
2010-01-04 18:51:21 +00:00
$tester -> run ( array ( 'command' => 'list' , '--verbose' => true ));
2010-01-04 14:42:28 +00:00
$t -> is ( $tester -> getOutput () -> getVerbosity (), Output :: VERBOSITY_VERBOSE , '->run() sets the output to verbose is --verbose is passed' );
$application = new Application ();
$application -> setAutoExit ( false );
$application -> setCatchExceptions ( false );
2010-01-04 18:51:21 +00:00
$application -> addCommand ( new FooCommand ());
2010-01-04 14:42:28 +00:00
$tester = new ApplicationTester ( $application );
2010-01-04 18:51:21 +00:00
$tester -> run ( array ( 'command' => 'foo:bar' , '--no-interaction' => true ));
2010-01-04 14:42:28 +00:00
$t -> is ( $tester -> getDisplay (), " called \n " , '->run() does not called interact() if --no-interaction is passed' );