Skip to content

Commit

Permalink
[bug] don't split output streams by default
Browse files Browse the repository at this point in the history
Must be enabled via TestCommand::splitOutputStreams()
  • Loading branch information
kbond committed Mar 19, 2021
1 parent 9aecf59 commit 64b6eb5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class CreateUserCommandTest extends KernelTestCase

// advanced usage
$this->consoleCommand(CreateUserCommand::class) // can use the command class or "name"
->splitOutputStreams() // by default stdout/stderr are combined, this options splits them
->addArgument('kbond')
->addOption('--admin') // with or without "--" prefix
->addOption('role', ['ROLE_EMPLOYEE', 'ROLE_MANAGER'])
Expand All @@ -49,8 +50,8 @@ class CreateUserCommandTest extends KernelTestCase
->assertSuccessful()
->assertStatusCode(0) // equivalent to ->assertSuccessful()
->assertOutputContains('Creating admin user "kbond"')
->assertErrorOutputContains('this is in stderr')
->assertErrorOutputNotContains('admin user')
->assertErrorOutputContains('this is in stderr') // used in conjunction with ->splitOutputStreams()
->assertErrorOutputNotContains('admin user') // used in conjunction with ->splitOutputStreams()
->dump() // dump() the status code/outputs and continue
->dd() // dd() the status code/outputs
;
Expand Down Expand Up @@ -94,6 +95,7 @@ class CreateUserCommandTest extends TestCase
public function test_can_create_user(): void
{
TestCommand::for(new CreateUserCommand(/** args... */))
->splitOutputStreams() // by default stdout/stderr are combined, this options splits them
->addArgument('kbond')
->addOption('--admin') // with or without "--" prefix
->addOption('role', ['ROLE_EMPLOYEE', 'ROLE_MANAGER'])
Expand All @@ -104,8 +106,8 @@ class CreateUserCommandTest extends TestCase
->assertSuccessful()
->assertStatusCode(0) // equivalent to ->assertSuccessful()
->assertOutputContains('Creating admin user "kbond"')
->assertErrorOutputContains('this is in stderr')
->assertErrorOutputNotContains('admin user')
->assertErrorOutputContains('this is in stderr') // used in conjunction with ->splitOutputStreams()
->assertErrorOutputNotContains('admin user') // used in conjunction with ->splitOutputStreams()
->dump() // dump() the status code/outputs and continue
->dd() // dd() the status code/outputs
;
Expand Down
6 changes: 3 additions & 3 deletions src/CommandTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(Command $command, InputInterface $input)
$this->input = $input;
}

public function execute(): CommandResult
public function execute(bool $splitStreams): CommandResult
{
$this->input->setInteractive(false);

Expand All @@ -41,12 +41,12 @@ public function execute(): CommandResult
$this->initOutput([
'decorated' => true === $this->input->hasParameterOption(['--ansi'], true),
'verbosity' => $this->verbosity(),
'capture_stderr_separately' => true,
'capture_stderr_separately' => $splitStreams,
]);

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

return new CommandResult($statusCode, $this->getDisplay(), $this->getErrorOutput());
return new CommandResult($statusCode, $this->getDisplay(), $splitStreams ? $this->getErrorOutput() : '');
}

private function verbosity(): int
Expand Down
10 changes: 9 additions & 1 deletion src/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class TestCommand
private Command $command;
private string $cli;
private array $inputs = [];
private bool $splitOutputStreams = false;

private function __construct(Command $command, string $cli)
{
Expand Down Expand Up @@ -41,6 +42,13 @@ public static function from(Application $application, string $cli): self
return new self($application->find(\explode(' ', $cli, 2)[0]), $cli);
}

public function splitOutputStreams(): self
{
$this->splitOutputStreams = true;

return $this;
}

public function addArgument(string $value): self
{
$this->cli .= \sprintf(' "%s"', $value);
Expand Down Expand Up @@ -86,6 +94,6 @@ public function execute(): CommandResult
$tester = new CommandTester($this->command, new StringInput($this->cli));
$tester->setInputs($this->inputs);

return $tester->execute();
return $tester->execute($this->splitOutputStreams);
}
}
16 changes: 16 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ final class FunctionalTest extends KernelTestCase
public function string_command_with_no_arguments(): void
{
$this->executeConsoleCommand('fixture:command')
->assertSuccessful()
->assertOutputContains('Executing command')
->assertOutputNotContains('arg1')
->assertOutputNotContains('opt1')
->assertOutputContains('Error output')
;
}

/**
* @test
*/
public function can_split_output_streams(): void
{
$this->consoleCommand('fixture:command')
->splitOutputStreams()
->execute()
->assertSuccessful()
->assertOutputContains('Executing command')
->assertOutputNotContains('arg1')
Expand Down
16 changes: 16 additions & 0 deletions tests/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ final class UnitTest extends TestCase
public function command_with_no_arguments(): void
{
TestCommand::for(new FixtureCommand())->execute()
->assertSuccessful()
->assertOutputContains('Executing command')
->assertOutputNotContains('arg1')
->assertOutputNotContains('opt1')
->assertOutputContains('Error output')
;
}

/**
* @test
*/
public function can_split_output_streams(): void
{
TestCommand::for(new FixtureCommand())
->splitOutputStreams()
->execute()
->assertSuccessful()
->assertOutputContains('Executing command')
->assertOutputNotContains('arg1')
Expand Down

0 comments on commit 64b6eb5

Please sign in to comment.