From 61189de6cb5a7c9691d86a8015d07e1d715fff53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Wed, 8 Jan 2025 22:48:27 +0100 Subject: [PATCH] feat: Add `assertOutputEmpty`/`NotEmpty` and `assertErrorOutputEmpty`/`NotEmpty` (#26) --- src/CommandResult.php | 28 ++++++++++++++++++++++++++++ tests/Fixture/FixtureCommand.php | 14 ++++++++++++++ tests/FunctionalTest.php | 27 +++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/src/CommandResult.php b/src/CommandResult.php index 00153de..787bf71 100644 --- a/src/CommandResult.php +++ b/src/CommandResult.php @@ -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); @@ -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); diff --git a/tests/Fixture/FixtureCommand.php b/tests/Fixture/FixtureCommand.php index cc96475..5d10693 100644 --- a/tests/Fixture/FixtureCommand.php +++ b/tests/Fixture/FixtureCommand.php @@ -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; @@ -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) ; } @@ -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 command...'); $output->writeln("verbosity: {$output->getVerbosity()}"); $output->writeln('decorated: '.($output->isDecorated() ? 'yes' : 'no')); @@ -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()); diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 630c2bf..8ac0314 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -37,6 +37,7 @@ public function string_command_with_no_arguments(): void ->assertOutputNotContains('arg1') ->assertOutputNotContains('opt1') ->assertOutputContains('Error output') + ->assertOutputNotEmpty() ; } @@ -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 */