Skip to content

Commit

Permalink
[feature] access/assert against stderr output
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Mar 19, 2021
1 parent ae01bf0 commit 9aecf59
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class CreateUserCommandTest extends KernelTestCase
->assertSuccessful()
->assertStatusCode(0) // equivalent to ->assertSuccessful()
->assertOutputContains('Creating admin user "kbond"')
->dump() // dump() the status code/output and continue
->dd() // dd() the status code/output
->assertErrorOutputContains('this is in stderr')
->assertErrorOutputNotContains('admin user')
->dump() // dump() the status code/outputs and continue
->dd() // dd() the status code/outputs
;

// testing interactive commands
Expand All @@ -67,6 +69,13 @@ class CreateUserCommandTest extends KernelTestCase
->assertSuccessful()
->assertOutputContains('Creating regular user "kbond"')
;

// access result
$result = $this->executeConsoleCommand('create:user');

$result->statusCode();
$result->output();
$result->errorOutput();
}
}
```
Expand Down Expand Up @@ -95,8 +104,10 @@ class CreateUserCommandTest extends TestCase
->assertSuccessful()
->assertStatusCode(0) // equivalent to ->assertSuccessful()
->assertOutputContains('Creating admin user "kbond"')
->dump() // dump() the status code/output and continue
->dd() // dd() the status code/output
->assertErrorOutputContains('this is in stderr')
->assertErrorOutputNotContains('admin user')
->dump() // dump() the status code/outputs and continue
->dd() // dd() the status code/outputs
;

// testing interactive commands
Expand All @@ -107,6 +118,13 @@ class CreateUserCommandTest extends TestCase
->assertSuccessful()
->assertOutputContains('Creating regular user "kbond"')
;

// access result
$result = TestCommand::for(new CreateUserCommand(/** args... */))->execute();

$result->statusCode();
$result->output();
$result->errorOutput();
}
}
```
24 changes: 23 additions & 1 deletion src/CommandResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ final class CommandResult
{
private int $statusCode;
private string $output;
private string $errorOutput;

/**
* @internal
*/
public function __construct(int $statusCode, string $output)
public function __construct(int $statusCode, string $output, string $errorOutput)
{
$this->statusCode = $statusCode;
$this->output = $output;
$this->errorOutput = $errorOutput;
}

public function statusCode(): int
Expand All @@ -32,6 +34,11 @@ public function output(): string
return $this->output;
}

public function errorOutput(): string
{
return $this->errorOutput;
}

public function assertOutputContains(string $expected): self
{
PHPUnit::assertStringContainsString($expected, $this->output());
Expand All @@ -46,6 +53,20 @@ public function assertOutputNotContains(string $expected): self
return $this;
}

public function assertErrorOutputContains(string $expected): self
{
PHPUnit::assertStringContainsString($expected, $this->errorOutput());

return $this;
}

public function assertErrorOutputNotContains(string $expected): self
{
PHPUnit::assertStringNotContainsString($expected, $this->errorOutput());

return $this;
}

public function assertSuccessful(): self
{
return $this->assertStatusCode(0);
Expand All @@ -62,6 +83,7 @@ public function dump(): self
{
VarDumper::dump("Status: {$this->statusCode()}");
VarDumper::dump($this->output());
VarDumper::dump($this->errorOutput());

return $this;
}
Expand Down
3 changes: 2 additions & 1 deletion src/CommandTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ public function execute(): CommandResult
$this->initOutput([
'decorated' => true === $this->input->hasParameterOption(['--ansi'], true),
'verbosity' => $this->verbosity(),
'capture_stderr_separately' => true,
]);

$statusCode = $this->command->run($this->input, $this->output);

return new CommandResult($statusCode, $this->getDisplay());
return new CommandResult($statusCode, $this->getDisplay(), $this->getErrorOutput());
}

private function verbosity(): int
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixture/FixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

Expand All @@ -31,9 +32,12 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output)
{
$errOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;

$output->writeln('Executing <info>command</info>...');
$output->writeln("verbosity: {$output->getVerbosity()}");
$output->writeln('decorated: '.($output->isDecorated() ? 'yes' : 'no'));
$errOutput->writeln('Error output.');

if ($input->getOption('throw')) {
throw new \RuntimeException('Exception thrown!');
Expand Down
3 changes: 3 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function string_command_with_no_arguments(): void
->assertOutputContains('Executing command')
->assertOutputNotContains('arg1')
->assertOutputNotContains('opt1')
->assertOutputNotContains('Error output')
->assertErrorOutputContains('Error output')
->assertErrorOutputNotContains('Executing command')
;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public function command_with_no_arguments(): void
->assertOutputContains('Executing command')
->assertOutputNotContains('arg1')
->assertOutputNotContains('opt1')
->assertOutputNotContains('Error output')
->assertErrorOutputContains('Error output')
->assertErrorOutputNotContains('Executing command')
;
}

Expand Down

0 comments on commit 9aecf59

Please sign in to comment.