fix Command:asXml to use processed help
This commit is contained in:
parent
304e13daa3
commit
11585c3b67
@ -560,7 +560,7 @@ class Command
|
||||
|
||||
if ($help = $this->getProcessedHelp()) {
|
||||
$messages[] = '<comment>Help:</comment>';
|
||||
$messages[] = ' '.implode("\n ", explode("\n", $help))."\n";
|
||||
$messages[] = ' '.str_replace("\n", "\n ", $help)."\n";
|
||||
}
|
||||
|
||||
return implode("\n", $messages);
|
||||
@ -585,11 +585,10 @@ class Command
|
||||
$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) {
|
||||
|
@ -33,11 +33,23 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
|
||||
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 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
|
||||
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 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$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 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$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 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
|
||||
$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');
|
||||
|
||||
}
|
||||
|
||||
|
@ -128,6 +128,14 @@ class CommandTest extends \PHPUnit_Framework_TestCase
|
||||
$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();
|
||||
|
@ -4,13 +4,13 @@
|
||||
<command id="help" name="help">
|
||||
<usage>help [--xml] [command_name]</usage>
|
||||
<description>Displays help for a command</description>
|
||||
<help>The <info>%command.name%</info> command displays help for a given command:
|
||||
<help>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></help>
|
||||
<info>php app/console help --xml list</info></help>
|
||||
<aliases />
|
||||
<arguments>
|
||||
<argument name="command_name" is_required="0" is_array="0">
|
||||
@ -29,21 +29,21 @@
|
||||
<command id="list" name="list">
|
||||
<usage>list [--xml] [--raw] [namespace]</usage>
|
||||
<description>Lists commands</description>
|
||||
<help>The <info>%command.name%</info> command lists all commands:
|
||||
<help>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></help>
|
||||
<info>php app/console list --raw</info></help>
|
||||
<aliases/>
|
||||
<arguments>
|
||||
<argument name="namespace" is_required="0" is_array="0">
|
||||
|
Reference in New Issue
Block a user