Skip to content

Commit

Permalink
feat: Add assertOutputEmpty/NotEmpty and assertErrorOutputEmpty
Browse files Browse the repository at this point in the history
…/`NotEmpty` (#26)
  • Loading branch information
smnandre authored Jan 8, 2025
1 parent c7347a2 commit 61189de
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/CommandResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ public function assertOutputNotContains(string $expected): self
return $this;
}

public function assertOutputEmpty(): self
{
Assert::that($this->output())->isEmpty();

return $this;
}

public function assertOutputNotEmpty(): self
{
Assert::that($this->output())->isNotEmpty();

return $this;
}

public function assertErrorOutputContains(string $expected): self
{
Assert::that($this->errorOutput())->contains($expected);
Expand All @@ -75,6 +89,20 @@ public function assertErrorOutputNotContains(string $expected): self
return $this;
}

public function assertErrorOutputEmpty(): self
{
Assert::that($this->errorOutput())->isEmpty();

return $this;
}

public function assertErrorOutputNotEmpty(): self
{
Assert::that($this->errorOutput())->isNotEmpty();

return $this;
}

public function assertSuccessful(): self
{
return $this->assertStatusCode(0);
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixture/FixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand All @@ -40,6 +41,8 @@ protected function configure(): void
->addOption('opt3', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY)
->addOption('throw', null, InputOption::VALUE_NONE)
->addOption('code', null, InputOption::VALUE_REQUIRED, '', 0)
->addOption('output', null, InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE)
->addOption('error-output', null, InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE)
;
}

Expand All @@ -50,6 +53,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$errOutput = $output->getErrorOutput();

if (false === $input->getOption('output')) {
$output = new NullOutput();
}
if (false === $input->getOption('error-output')) {
$errOutput = new NullOutput();
}

$output->writeln('Executing <info>command</info>...');
$output->writeln("verbosity: {$output->getVerbosity()}");
$output->writeln('decorated: '.($output->isDecorated() ? 'yes' : 'no'));
Expand All @@ -75,6 +85,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln("opt3 value: {$value}");
}

if ($output instanceof NullOutput || $errOutput instanceof NullOutput) {
return (int) $input->getOption('code');
}

(new SymfonyStyle($input, $output))->success('Long link: https://github.com/zenstruck/console-test/blob/997ee1f66743342ffd9cd00a77613ebfa2efd2b8/src/CommandResult.php');

$table = new Table($output->section());
Expand Down
27 changes: 27 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function string_command_with_no_arguments(): void
->assertOutputNotContains('arg1')
->assertOutputNotContains('opt1')
->assertOutputContains('Error output')
->assertOutputNotEmpty()
;
}

Expand Down Expand Up @@ -245,6 +246,32 @@ public function can_adjust_verbosity_with_v_option(): void
;
}

/**
* @test
*/
public function can_turn_off_output(): void
{
$this->consoleCommand('fixture:command --no-output')
->splitOutputStreams()
->execute()
->assertOutputEmpty()
->assertErrorOutputNotEmpty()
;
}

/**
* @test
*/
public function can_turn_off_error_output(): void
{
$this->consoleCommand('fixture:command --no-error-output')
->splitOutputStreams()
->execute()
->assertOutputNotEmpty()
->assertErrorOutputEmpty()
;
}

/**
* @test
*/
Expand Down

0 comments on commit 61189de

Please sign in to comment.