$this->assertEquals('foo',$application->getName(),'__construct() takes the application name as its first argument');
$this->assertEquals('bar',$application->getVersion(),'__construct() takes the application version as its second argument');
$this->assertEquals(['help','list'],array_keys($application->all()),'__construct() registered the help and list commands by default');
}
publicfunctiontestSetGetName()
{
$application=newApplication();
$application->setName('foo');
$this->assertEquals('foo',$application->getName(),'->setName() sets the name of the application');
}
publicfunctiontestSetGetVersion()
{
$application=newApplication();
$application->setVersion('bar');
$this->assertEquals('bar',$application->getVersion(),'->setVersion() sets the version of the application');
}
publicfunctiontestGetLongVersion()
{
$application=newApplication('foo','bar');
$this->assertEquals('foo <info>bar</info>',$application->getLongVersion(),'->getLongVersion() returns the long version of the application');
}
publicfunctiontestHelp()
{
$application=newApplication();
$this->assertStringEqualsFile(self::$fixturesPath.'/application_gethelp.txt',$this->normalizeLineBreaks($application->getHelp()),'->getHelp() returns a help message');
}
publicfunctiontestAll()
{
$application=newApplication();
$commands=$application->all();
$this->assertInstanceOf('Symfony\\Component\\Console\\Command\\HelpCommand',$commands['help'],'->all() returns the registered commands');
$application->add(new\FooCommand());
$commands=$application->all('foo');
$this->assertCount(1,$commands,'->all() takes a namespace as its first argument');
}
publicfunctiontestAllWithCommandLoader()
{
$application=newApplication();
$commands=$application->all();
$this->assertInstanceOf('Symfony\\Component\\Console\\Command\\HelpCommand',$commands['help'],'->all() returns the registered commands');
$application->add(new\FooCommand());
$commands=$application->all('foo');
$this->assertCount(1,$commands,'->all() takes a namespace as its first argument');
$this->expectExceptionMessage('Command class "Foo5Command" is not correctly initialized. You probably forgot to call the parent constructor.');
$application=newApplication();
$application->add(new\Foo5Command());
}
publicfunctiontestHasGet()
{
$application=newApplication();
$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');
$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');
$application=newApplication();
$application->add($foo=new\FooCommand());
// simulate --help
$r=new\ReflectionObject($application);
$p=$r->getProperty('wantHelps');
$p->setAccessible(true);
$p->setValue($application,true);
$command=$application->get('foo:bar');
$this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand',$command,'->get() returns the help command if --help is provided as the input');
}
publicfunctiontestHasGetWithCommandLoader()
{
$application=newApplication();
$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');
$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');
$this->assertEquals('foo',$application->findNamespace('foo'),'->findNamespace() returns commands even if the commands are only contained in subnamespaces');
}
publicfunctiontestFindAmbiguousNamespace()
{
$application=newApplication();
$application->add(new\BarBucCommand());
$application->add(new\FooCommand());
$application->add(new\Foo2Command());
$expectedMsg="The namespace \"f\" is ambiguous.\nDid you mean one of these?\n foo\n foo1";
$this->expectExceptionMessage('Command "foo1" is not defined');
$application=newApplication();
$application->add(new\FooCommand());
$application->add(new\Foo1Command());
$application->add(new\Foo2Command());
$application->find($commandName='foo1');
}
publicfunctiontestFind()
{
$application=newApplication();
$application->add(new\FooCommand());
$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');
$this->assertInstanceOf('FooSameCaseUppercaseCommand',$application->find('f:B'),'->find() returns a command if the abbreviation is the correct case');
$this->assertInstanceOf('FooSameCaseUppercaseCommand',$application->find('f:BAR'),'->find() returns a command if the abbreviation is the correct case');
$this->assertInstanceOf('FooSameCaseLowercaseCommand',$application->find('f:b'),'->find() returns a command if the abbreviation is the correct case');
$this->assertInstanceOf('FooSameCaseLowercaseCommand',$application->find('f:bar'),'->find() returns a command if the abbreviation is the correct case');
$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');
"Command \"a\" is ambiguous.\nDid you mean one of these?\n".
" afoobar The foo:bar command\n".
" afoobar1 The foo:bar1 command\n".
' afoobar2 The foo1:bar command',
],
[
'foo:b',
"Command \"foo:b\" is ambiguous.\nDid you mean one of these?\n".
" foo:bar The foo:bar command\n".
" foo:bar1 The foo:bar1 command\n".
' foo1:bar The foo1:bar command',
],
];
}
publicfunctiontestFindCommandEqualNamespace()
{
$application=newApplication();
$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');
$this->fail('->find() throws a CommandNotFoundException if command does not exist, with alternatives');
}catch(\Exception$e){
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException',$e,'->find() throws a CommandNotFoundException if command does not exist, with alternatives');
$this->assertRegExp('/Did you mean one of these/',$e->getMessage(),'->find() throws a CommandNotFoundException if command does not exist, with alternatives');
$this->fail('->find() throws a CommandNotFoundException if command does not exist, with alternatives');
}catch(\Exception$e){
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException',$e,'->find() throws a CommandNotFoundException if command does not exist, with alternatives');
$this->assertRegExp('/Did you mean one of these/',$e->getMessage(),'->find() throws a CommandNotFoundException if command does not exist, with alternatives');
$this->assertRegExp('/foo1/',$e->getMessage());
}
$application->add(new\Foo3Command());
$application->add(new\Foo4Command());
// Subnamespace + plural
try{
$a=$application->find('foo3:');
$this->fail('->find() should throw an Symfony\Component\Console\Exception\CommandNotFoundException if a command is ambiguous because of a subnamespace, with alternatives');
$this->fail('->find() throws a CommandNotFoundException if command does not exist');
}catch(\Exception$e){
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException',$e,'->find() throws a CommandNotFoundException if command does not exist');
$this->assertSame([],$e->getAlternatives());
$this->assertEquals(sprintf('Command "%s" is not defined.',$commandName),$e->getMessage(),'->find() throws a CommandNotFoundException if command does not exist, without alternatives');
}
// Test if "bar1" command throw a "CommandNotFoundException" and does not contain
// "foo:bar" as alternative because "bar1" is too far from "foo:bar"
try{
$application->find($commandName='bar1');
$this->fail('->find() throws a CommandNotFoundException if command does not exist');
}catch(\Exception$e){
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException',$e,'->find() throws a CommandNotFoundException if command does not exist');
$this->assertRegExp(sprintf('/Command "%s" is not defined./',$commandName),$e->getMessage(),'->find() throws a CommandNotFoundException if command does not exist, with alternatives');
$this->assertRegExp('/afoobar1/',$e->getMessage(),'->find() throws a CommandNotFoundException if command does not exist, with alternative : "afoobar1"');
$this->assertRegExp('/foo:bar1/',$e->getMessage(),'->find() throws a CommandNotFoundException if command does not exist, with alternative : "foo:bar1"');
$this->assertNotRegExp('/foo:bar(?>!1)/',$e->getMessage(),'->find() throws a CommandNotFoundException if command does not exist, without "foo:bar" alternative');
$this->fail('->find() throws a CommandNotFoundException if namespace does not exist');
}catch(\Exception$e){
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException',$e,'->find() throws a CommandNotFoundException if namespace does not exist');
$this->assertSame([],$e->getAlternatives());
$this->assertEquals('There are no commands defined in the "Unknown-namespace" namespace.',$e->getMessage(),'->find() throws a CommandNotFoundException if namespace does not exist, without alternatives');
}
try{
$application->find('foo2:command');
$this->fail('->find() throws a CommandNotFoundException if namespace does not exist');
}catch(\Exception$e){
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException',$e,'->find() throws a CommandNotFoundException if namespace does not exist');
$this->assertRegExp('/There are no commands defined in the "foo2" namespace./',$e->getMessage(),'->find() throws a CommandNotFoundException if namespace does not exist, with alternative');
$this->assertRegExp('/foo/',$e->getMessage(),'->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo"');
$this->assertRegExp('/foo1/',$e->getMessage(),'->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo1"');
$this->assertRegExp('/foo3/',$e->getMessage(),'->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo3"');
$this->fail('->find() throws a CommandNotFoundException if command is not defined');
}catch(\Exception$e){
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException',$e,'->find() throws a CommandNotFoundException if command is not defined');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt',$tester->getErrorOutput(true),'->renderException() renders a pretty exception');
$this->assertStringContainsString('Exception trace',$tester->getErrorOutput(),'->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt',$tester->getErrorOutput(true),'->renderException() renders the command synopsis when an exception occurs in the context of a command');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt',$tester->getErrorOutput(true),'->renderException() renders a pretty exceptions with previous exceptions');
$this->assertRegExp('/\[Exception\]\s*First exception/',$tester->getDisplay(),'->renderException() renders a pretty exception without code exception when code exception is default and verbosity is verbose');
$this->assertRegExp('/\[Exception\]\s*Second exception/',$tester->getDisplay(),'->renderException() renders a pretty exception without code exception when code exception is 0 and verbosity is verbose');
$this->assertRegExp('/\[Exception \(404\)\]\s*Third exception/',$tester->getDisplay(),'->renderException() renders a pretty exception with code exception when code exception is 404 and verbosity is verbose');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt',$tester->getDisplay(true),'->renderException() renders a pretty exceptions with previous exceptions');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt',$tester->getErrorOutput(true),'->renderException() renders a pretty exceptions with previous exceptions');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt',$tester->getErrorOutput(true),'->renderException() wraps messages when they are bigger than the terminal');
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/application_renderexception_doublewidth1.txt',$tester->getErrorOutput(true),'->renderException() renders a pretty exceptions with previous exceptions');
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/application_renderexception_doublewidth1decorated.txt',$tester->getErrorOutput(true),'->renderException() renders a pretty exceptions with previous exceptions');
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/application_renderexception_doublewidth2.txt',$tester->getErrorOutput(true),'->renderException() wraps messages when they are bigger than the terminal');
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/application_renderexception_linebreaks.txt',$tester->getDisplay(true),'->renderException() keep multiple line breaks');
$this->assertStringContainsString(sprintf('() at %s:',__FILE__),$tester->getDisplay());
}
publicfunctiontestRun()
{
$application=newApplication();
$application->setAutoExit(false);
$application->setCatchExceptions(false);
$application->add($command=new\Foo1Command());
$_SERVER['argv']=['cli.php','foo:bar1'];
ob_start();
$application->run();
ob_end_clean();
$this->assertInstanceOf('Symfony\Component\Console\Input\ArgvInput',$command->input,'->run() creates an ArgvInput by default if none is given');
$this->assertInstanceOf('Symfony\Component\Console\Output\ConsoleOutput',$command->output,'->run() creates a ConsoleOutput by default if none is given');
$application=newApplication();
$application->setAutoExit(false);
$application->setCatchExceptions(false);
$this->ensureStaticCommandHelp($application);
$tester=newApplicationTester($application);
$tester->run([],['decorated'=>false]);
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run1.txt',$tester->getDisplay(true),'->run() runs the list command if no argument is passed');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt',$tester->getDisplay(true),'->run() runs the help command if --help is passed');
$tester->run(['-h'=>true],['decorated'=>false]);
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt',$tester->getDisplay(true),'->run() runs the help command if -h is passed');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt',$tester->getDisplay(true),'->run() displays the program version if --version is passed');
$tester->run(['-V'=>true],['decorated'=>false]);
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt',$tester->getDisplay(true),'->run() displays the program version if -v is passed');
$this->assertSame(Output::VERBOSITY_VERBOSE,$tester->getOutput()->getVerbosity(),'->run() sets the output to verbose if --verbose is passed');
$tester->run(['command'=>'list','--verbose'=>1]);
$this->assertSame(Output::VERBOSITY_VERBOSE,$tester->getOutput()->getVerbosity(),'->run() sets the output to verbose if --verbose=1 is passed');
$tester->run(['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(['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(['command'=>'list','--verbose'=>4]);
$this->assertSame(Output::VERBOSITY_VERBOSE,$tester->getOutput()->getVerbosity(),'->run() sets the output to verbose if unknown --verbose level is passed');
$tester->run(['command'=>'list','-v'=>true]);
$this->assertSame(Output::VERBOSITY_VERBOSE,$tester->getOutput()->getVerbosity(),'->run() sets the output to verbose if -v is passed');
$tester->run(['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(['command'=>'list','-vvv'=>true]);
$this->assertSame(Output::VERBOSITY_DEBUG,$tester->getOutput()->getVerbosity(),'->run() sets the output to verbose if -v is passed');
$this->assertEquals('called'.PHP_EOL.'opt'.PHP_EOL,$tester->getDisplay(),'Application runs the default set command if different from \'list\' command');