Skip to content

Commit

Permalink
fix Command:asXml to use processed help
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobion committed Mar 21, 2012
1 parent 304e13d commit 11585c3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
7 changes: 3 additions & 4 deletions src/Symfony/Component/Console/Command/Command.php
Expand Up @@ -560,7 +560,7 @@ public function asText()

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);
Expand All @@ -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) {
Expand Down
32 changes: 23 additions & 9 deletions tests/Symfony/Tests/Component/Console/ApplicationTest.php
Expand Up @@ -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');
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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');
}
Expand All @@ -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');

}

Expand Down
8 changes: 8 additions & 0 deletions tests/Symfony/Tests/Component/Console/Command/CommandTest.php
Expand Up @@ -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();
Expand Down
Expand Up @@ -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">
Expand All @@ -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">
Expand Down

0 comments on commit 11585c3

Please sign in to comment.