fix Command:asXml to use processed help

This commit is contained in:
Tobias Schultze 2012-02-17 18:58:01 +01:00
parent 304e13daa3
commit 11585c3b67
4 changed files with 42 additions and 21 deletions

View File

@ -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) {

View File

@ -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');
}

View File

@ -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();

View File

@ -4,13 +4,13 @@
<command id="help" name="help">
<usage>help [--xml] [command_name]</usage>
<description>Displays help for a command</description>
<help>The &lt;info&gt;%command.name%&lt;/info&gt; command displays help for a given command:
<help>The &lt;info&gt;help&lt;/info&gt; command displays help for a given command:
&lt;info&gt;php %command.full_name% list&lt;/info&gt;
&lt;info&gt;php app/console help list&lt;/info&gt;
You can also output the help as XML by using the &lt;comment&gt;--xml&lt;/comment&gt; option:
&lt;info&gt;php %command.full_name% --xml list&lt;/info&gt;</help>
&lt;info&gt;php app/console help --xml list&lt;/info&gt;</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 &lt;info&gt;%command.name%&lt;/info&gt; command lists all commands:
<help>The &lt;info&gt;list&lt;/info&gt; command lists all commands:
&lt;info&gt;php %command.full_name%&lt;/info&gt;
&lt;info&gt;php app/console list&lt;/info&gt;
You can also display the commands for a specific namespace:
&lt;info&gt;php %command.full_name% test&lt;/info&gt;
&lt;info&gt;php app/console list test&lt;/info&gt;
You can also output the information as XML by using the &lt;comment&gt;--xml&lt;/comment&gt; option:
&lt;info&gt;php %command.full_name% --xml&lt;/info&gt;
&lt;info&gt;php app/console list --xml&lt;/info&gt;
It's also possible to get raw list of commands (useful for embedding command runner):
&lt;info&gt;php %command.full_name% --raw&lt;/info&gt;</help>
&lt;info&gt;php app/console list --raw&lt;/info&gt;</help>
<aliases/>
<arguments>
<argument name="namespace" is_required="0" is_array="0">