Skip to content

Commit be67887

Browse files
committed
[Console] added a way to normalize a command display when using the tester
1 parent ce8d34e commit be67887

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
2.3.0
55
-----
66

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

910
2.2.0

Tester/ApplicationTester.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,21 @@ public function run(array $input, $options = array())
7878
/**
7979
* Gets the display returned by the last execution of the application.
8080
*
81+
* @param Boolean $normalize Whether to normalize end of lines to \n or not
82+
*
8183
* @return string The display
8284
*/
83-
public function getDisplay()
85+
public function getDisplay($normalize = false)
8486
{
8587
rewind($this->output->getStream());
8688

87-
return stream_get_contents($this->output->getStream());
89+
$display = stream_get_contents($this->output->getStream());
90+
91+
if ($normalize) {
92+
$display = str_replace(PHP_EOL, "\n", $display);
93+
}
94+
95+
return $display;
8896
}
8997

9098
/**

Tester/CommandTester.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,21 @@ public function execute(array $input, array $options = array())
7171
/**
7272
* Gets the display returned by the last execution of the command.
7373
*
74+
* @param Boolean $normalize Whether to normalize end of lines to \n or not
75+
*
7476
* @return string The display
7577
*/
76-
public function getDisplay()
78+
public function getDisplay($normalize = false)
7779
{
7880
rewind($this->output->getStream());
7981

80-
return stream_get_contents($this->output->getStream());
82+
$display = stream_get_contents($this->output->getStream());
83+
84+
if ($normalize) {
85+
$display = str_replace(PHP_EOL, "\n", $display);
86+
}
87+
88+
return $display;
8189
}
8290

8391
/**

Tests/ApplicationTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public function testSetCatchExceptions()
356356

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

361361
$application->setCatchExceptions(false);
362362
try {
@@ -396,18 +396,18 @@ public function testRenderException()
396396
$tester = new ApplicationTester($application);
397397

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

401401
$tester->run(array('command' => 'foo'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
402402
$this->assertContains('Exception trace', $tester->getDisplay(), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
403403

404404
$tester->run(array('command' => 'list', '--foo' => true), array('decorated' => false));
405-
$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');
405+
$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');
406406

407407
$application->add(new \Foo3Command);
408408
$tester = new ApplicationTester($application);
409409
$tester->run(array('command' => 'foo3:bar'), array('decorated' => false));
410-
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders a pretty exceptions with previous exceptions');
410+
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions');
411411

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

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

423423
public function testRun()
@@ -443,19 +443,19 @@ public function testRun()
443443
$tester = new ApplicationTester($application);
444444

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)