diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index 010abb433a90..033a95c7630a 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -560,7 +560,7 @@ public function asText() if ($help = $this->getProcessedHelp()) { $messages[] = 'Help:'; - $messages[] = ' '.implode("\n ", explode("\n", $help))."\n"; + $messages[] = ' '.str_replace("\n", "\n ", $help)."\n"; } return implode("\n", $messages); @@ -585,11 +585,10 @@ public function asXml($asDom = false) $usageXML->appendChild($dom->createTextNode(sprintf($this->getSynopsis(), ''))); $commandXML->appendChild($descriptionXML = $dom->createElement('description')); - $descriptionXML->appendChild($dom->createTextNode(implode("\n ", explode("\n", $this->getDescription())))); + $descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $this->getDescription()))); $commandXML->appendChild($helpXML = $dom->createElement('help')); - $help = $this->help; - $helpXML->appendChild($dom->createTextNode(implode("\n ", explode("\n", $help)))); + $helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $this->getProcessedHelp()))); $commandXML->appendChild($aliasesXML = $dom->createElement('aliases')); foreach ($this->getAliases() as $alias) { diff --git a/tests/Symfony/Tests/Component/Console/ApplicationTest.php b/tests/Symfony/Tests/Component/Console/ApplicationTest.php index c413108e1004..f3e5a51e638b 100644 --- a/tests/Symfony/Tests/Component/Console/ApplicationTest.php +++ b/tests/Symfony/Tests/Component/Console/ApplicationTest.php @@ -33,11 +33,23 @@ static public function setUpBeforeClass() require_once self::$fixturesPath.'/Foo3Command.php'; } - protected function normalize($text) + protected function normalizeLineBreaks($text) { return str_replace(PHP_EOL, "\n", $text); } + /** + * 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())); + } + } + public function testConstructor() { $application = new Application('foo', 'bar'); @@ -69,7 +81,7 @@ public function testGetLongVersion() public function testHelp() { $application = new Application(); - $this->assertStringEqualsFile(self::$fixturesPath.'/application_gethelp.txt', str_replace(PHP_EOL, "\n", $application->getHelp()), '->setHelp() returns a help message'); + $this->assertStringEqualsFile(self::$fixturesPath.'/application_gethelp.txt', $this->normalizeLineBreaks($application->getHelp()), '->setHelp() returns a help message'); } public function testAll() @@ -280,7 +292,7 @@ public function testSetCatchExceptions() $application->setCatchExceptions(true); $tester->run(array('command' => 'foo'), array('decorated' => false)); - $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalize($tester->getDisplay()), '->setCatchExceptions() sets the catch exception flag'); + $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->setCatchExceptions() sets the catch exception flag'); $application->setCatchExceptions(false); try { @@ -296,14 +308,16 @@ public function testAsText() { $application = new Application(); $application->add(new \FooCommand); - $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext1.txt', str_replace(PHP_EOL, "\n", $application->asText()), '->asText() returns a text representation of the application'); - $this->assertStringEqualsFile(self::$fixturesPath.'/application_astext2.txt', str_replace(PHP_EOL, "\n", $application->asText('foo')), '->asText() returns a text representation of the application'); + $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'); } public function testAsXml() { $application = new Application(); $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'); } @@ -315,18 +329,18 @@ public function testRenderException() $tester = new ApplicationTester($application); $tester->run(array('command' => 'foo'), array('decorated' => false)); - $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalize($tester->getDisplay()), '->renderException() renders a pretty exception'); + $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders a pretty exception'); $tester->run(array('command' => 'foo'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE)); - $this->assertContains('Exception trace', $this->normalize($tester->getDisplay()), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose'); + $this->assertContains('Exception trace', $tester->getDisplay(), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose'); $tester->run(array('command' => 'list', '--foo' => true), array('decorated' => false)); - $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt', $this->normalize($tester->getDisplay()), '->renderException() renders the command synopsis when an exception occurs in the context of a command'); + $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders the command synopsis when an exception occurs in the context of a command'); $application->add(new \Foo3Command); $tester = new ApplicationTester($application); $tester->run(array('command' => 'foo3:bar'), array('decorated' => false)); - $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $this->normalize($tester->getDisplay()), '->renderException() renders a pretty exceptions with previous exceptions'); + $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders a pretty exceptions with previous exceptions'); } diff --git a/tests/Symfony/Tests/Component/Console/Command/CommandTest.php b/tests/Symfony/Tests/Component/Console/Command/CommandTest.php index 81371c019ff2..0c3f71f4a7b7 100644 --- a/tests/Symfony/Tests/Component/Console/Command/CommandTest.php +++ b/tests/Symfony/Tests/Component/Console/Command/CommandTest.php @@ -128,6 +128,14 @@ public function testGetSetHelp() $this->assertEquals('help1', $command->getHelp(), '->setHelp() sets the help'); } + public function testGetProcessedHelp() + { + $command = new \TestCommand(); + $command->setHelp('The %command.name% command does... Example: php %command.full_name%.'); + $this->assertContains('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly'); + $this->assertNotContains('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name%'); + } + public function testGetSetAliases() { $command = new \TestCommand(); diff --git a/tests/Symfony/Tests/Component/Console/Fixtures/application_asxml1.txt b/tests/Symfony/Tests/Component/Console/Fixtures/application_asxml1.txt index 3ae1fca142f4..ce21a328699e 100644 --- a/tests/Symfony/Tests/Component/Console/Fixtures/application_asxml1.txt +++ b/tests/Symfony/Tests/Component/Console/Fixtures/application_asxml1.txt @@ -4,13 +4,13 @@ help [--xml] [command_name] Displays help for a command - The <info>%command.name%</info> command displays help for a given command: + The <info>help</info> command displays help for a given command: - <info>php %command.full_name% list</info> + <info>php app/console help list</info> You can also output the help as XML by using the <comment>--xml</comment> option: - <info>php %command.full_name% --xml list</info> + <info>php app/console help --xml list</info> @@ -29,21 +29,21 @@ list [--xml] [--raw] [namespace] Lists commands - The <info>%command.name%</info> command lists all commands: + The <info>list</info> command lists all commands: - <info>php %command.full_name%</info> + <info>php app/console list</info> You can also display the commands for a specific namespace: - <info>php %command.full_name% test</info> + <info>php app/console list test</info> You can also output the information as XML by using the <comment>--xml</comment> option: - <info>php %command.full_name% --xml</info> + <info>php app/console list --xml</info> It's also possible to get raw list of commands (useful for embedding command runner): - <info>php %command.full_name% --raw</info> + <info>php app/console list --raw</info>