$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(array('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('<info>foo</info> version <comment>bar</comment>',$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');
}
publicfunctiontestRegister()
{
$application=newApplication();
$command=$application->register('foo');
$this->assertEquals('foo',$command->getName(),'->register() registers a new command');
}
publicfunctiontestAdd()
{
$application=newApplication();
$application->add($foo=new\FooCommand());
$commands=$application->all();
$this->assertEquals($foo,$commands['foo:bar'],'->add() registers a command');
$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');
$this->assertEquals('foo',$application->findNamespace('foo'),'->findNamespace() returns commands even if the commands are only contained in subnamespaces');
$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');
array('a','Command "a" is ambiguous (afoobar, afoobar1 and 1 more).'),
array('foo:b','Command "foo:b" is ambiguous (foo:bar, foo:bar1 and 1 more).'),
);
}
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 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');
$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');
$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 \InvalidArgumentException if a command is ambiguous because of a subnamespace, with alternatives');
$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');
}
// Test if "bar1" command throw an "\InvalidArgumentException" 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 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');
$this->assertRegExp('/afoobar1/',$e->getMessage(),'->find() throws an \InvalidArgumentException if command does not exist, with alternative : "afoobar1"');
$this->assertRegExp('/foo:bar1/',$e->getMessage(),'->find() throws an \InvalidArgumentException if command does not exist, with alternative : "foo:bar1"');
$this->assertNotRegExp('/foo:bar(?>!1)/',$e->getMessage(),'->find() throws an \InvalidArgumentException if command does not exist, without "foo:bar" alternative');
$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->assertEquals('There are no commands defined in the "Unknown-namespace" namespace.',$e->getMessage(),'->find() throws an \InvalidArgumentException if namespace does not exist, without alternatives');
}
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"');
$this->fail('->setCatchExceptions() sets the catch exception flag');
}catch(\Exception$e){
$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');
}
}
/**
*@grouplegacy
*/
publicfunctiontestLegacyAsText()
{
$application=newApplication();
$application->add(new\FooCommand());
$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');
}
/**
*@grouplegacy
*/
publicfunctiontestLegacyAsXml()
{
$application=newApplication();
$application->add(new\FooCommand());
$this->ensureStaticCommandHelp($application);
$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');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt',$tester->getDisplay(true),'->renderException() renders a pretty exception');
$this->assertContains('Exception trace',$tester->getDisplay(),'->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
$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');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt',$tester->getDisplay(true),'->renderException() renders a pretty exceptions with previous exceptions');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt',$tester->getDisplay(true),'->renderException() renders a pretty exceptions with previous exceptions');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt',$tester->getDisplay(true),'->renderException() wraps messages when they are bigger than the terminal');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth1.txt',$tester->getDisplay(true),'->renderException() renders a pretty exceptions with previous exceptions');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth1decorated.txt',$tester->getDisplay(true),'->renderException() renders a pretty exceptions with previous exceptions');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth2.txt',$tester->getDisplay(true),'->renderException() wraps messages when they are bigger than the terminal');
}
publicfunctiontestRun()
{
$application=newApplication();
$application->setAutoExit(false);
$application->setCatchExceptions(false);
$application->add($command=new\Foo1Command());
$_SERVER['argv']=array('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(array(),array('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');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt',$tester->getDisplay(true),'->run() displays the program version if --version is passed');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt',$tester->getDisplay(true),'->run() displays the program version if -v is passed');
$this->assertSame(Output::VERBOSITY_VERY_VERBOSE,$tester->getOutput()->getVerbosity(),'->run() sets the output to very verbose if --verbose=2 is passed');
$this->assertSame(Output::VERBOSITY_VERBOSE,$tester->getOutput()->getVerbosity(),'->run() sets the output to verbose if unknown --verbose level is passed');
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL,$tester->getDisplay(),'Application runs the default set command if different from \'list\' command');
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL,$tester->getDisplay(),'Application runs the default set command if different from \'list\' command');