Skip to content

Commit

Permalink
[Console] added a way to normalize a command display when using the t…
Browse files Browse the repository at this point in the history
…ester
  • Loading branch information
fabpot committed Mar 24, 2013
1 parent ddd30d0 commit afd7d05
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
2.3.0
-----

* added a way to normalize EOLs in `ApplicationTester::getDisplay()` and `CommandTester::getDisplay()`
* added a way to set the progress bar progress via the `setCurrent` method

2.2.0
Expand Down
12 changes: 10 additions & 2 deletions src/Symfony/Component/Console/Tester/ApplicationTester.php
Expand Up @@ -78,13 +78,21 @@ public function run(array $input, $options = array())
/**
* Gets the display returned by the last execution of the application.
*
* @param Boolean $normalize Whether to normalize end of lines to \n or not
*
* @return string The display
*/
public function getDisplay()
public function getDisplay($normalize = false)
{
rewind($this->output->getStream());

return stream_get_contents($this->output->getStream());
$display = stream_get_contents($this->output->getStream());

if ($normalize) {
$display = str_replace(PHP_EOL, "\n", $display);
}

return $display;
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/Symfony/Component/Console/Tester/CommandTester.php
Expand Up @@ -71,13 +71,21 @@ public function execute(array $input, array $options = array())
/**
* Gets the display returned by the last execution of the command.
*
* @param Boolean $normalize Whether to normalize end of lines to \n or not
*
* @return string The display
*/
public function getDisplay()
public function getDisplay($normalize = false)
{
rewind($this->output->getStream());

return stream_get_contents($this->output->getStream());
$display = stream_get_contents($this->output->getStream());

if ($normalize) {
$display = str_replace(PHP_EOL, "\n", $display);
}

return $display;
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Expand Up @@ -356,7 +356,7 @@ public function testSetCatchExceptions()

$application->setCatchExceptions(true);
$tester->run(array('command' => 'foo'), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->setCatchExceptions() sets the catch exception flag');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getDisplay(true), '->setCatchExceptions() sets the catch exception flag');

$application->setCatchExceptions(false);
try {
Expand Down Expand Up @@ -396,18 +396,18 @@ public function testRenderException()
$tester = new ApplicationTester($application);

$tester->run(array('command' => 'foo'), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders a pretty exception');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getDisplay(true), '->renderException() renders a pretty exception');

$tester->run(array('command' => 'foo'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_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->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders the command synopsis when an exception occurs in the context of a command');
$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');

$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->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders a pretty exceptions with previous exceptions');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions');

$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth'));
$application->setAutoExit(false);
Expand All @@ -417,7 +417,7 @@ public function testRenderException()
$tester = new ApplicationTester($application);

$tester->run(array('command' => 'foo'), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() wraps messages when they are bigger than the terminal');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt', $tester->getDisplay(true), '->renderException() wraps messages when they are bigger than the terminal');
}

public function testRun()
Expand All @@ -443,19 +443,19 @@ public function testRun()
$tester = new ApplicationTester($application);

$tester->run(array(), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run1.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the list command if no argument is passed');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run1.txt', $tester->getDisplay(true), '->run() runs the list command if no argument is passed');

$tester->run(array('--help' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the help command if --help is passed');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $tester->getDisplay(true), '->run() runs the help command if --help is passed');

$tester->run(array('-h' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() runs the help command if -h is passed');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run2.txt', $tester->getDisplay(true), '->run() runs the help command if -h is passed');

$tester->run(array('command' => 'list', '--help' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the help if --help is passed');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $tester->getDisplay(true), '->run() displays the help if --help is passed');

$tester->run(array('command' => 'list', '-h' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the help if -h is passed');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run3.txt', $tester->getDisplay(true), '->run() displays the help if -h is passed');

$tester->run(array('--ansi' => true));
$this->assertTrue($tester->getOutput()->isDecorated(), '->run() forces color output if --ansi is passed');
Expand All @@ -464,10 +464,10 @@ public function testRun()
$this->assertFalse($tester->getOutput()->isDecorated(), '->run() forces color output to be disabled if --no-ansi is passed');

$tester->run(array('--version' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->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 --version is passed');

$tester->run(array('-V' => true), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->run() displays the program version if -v is passed');
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run4.txt', $tester->getDisplay(true), '->run() displays the program version if -v is passed');

$tester->run(array('command' => 'list', '--quiet' => true));
$this->assertSame('', $tester->getDisplay(), '->run() removes all output if --quiet is passed');
Expand Down

0 comments on commit afd7d05

Please sign in to comment.