diff --git a/src/Traits/InteractsWithConsole.php b/src/Traits/InteractsWithConsole.php new file mode 100644 index 0000000..c77e05c --- /dev/null +++ b/src/Traits/InteractsWithConsole.php @@ -0,0 +1,70 @@ +runCommand($command, $args); + + foreach ((array) $strings as $string) { + $this->assertStringContainsString( + $string, + $output, + \sprintf( + 'Console command [%s] with args [%s] does not contain string [%s]', + $command, + json_encode($args, JSON_THROW_ON_ERROR), + $string, + ), + ); + } + } + + public function assertCommandRegistered(string $name): void + { + $this->assertTrue( + $this->getConsole()->getApplication()->has($name), + \sprintf('Command [%s] is not registered.', $name), + ); + } + + final public function runCommand( + string $command, + array $args = [], + ?OutputInterface $output = null, + ?int $verbosityLevel = null, + ): string { + $input = new ArrayInput($args); + $input->setInteractive(false); + $output = $output ?? new BufferedOutput(); + /** @psalm-suppress ArgumentTypeCoercion */ + $output->setVerbosity($verbosityLevel ?? $this->defaultVerbosityLevel); + + $this->getConsole()->run($command, $input, $output); + + return $output->fetch(); + } + + final public function getConsole(): Console + { + return $this->getContainer()->get(Console::class); + } +} diff --git a/tests/src/ConsoleTest.php b/tests/src/ConsoleTest.php new file mode 100644 index 0000000..2c79ac9 --- /dev/null +++ b/tests/src/ConsoleTest.php @@ -0,0 +1,23 @@ +assertCommandRegistered('foo'); + } + + public function testNotRegisteredCommandShouldThrowAnException(): void + { + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('Command [bar] is not registered.'); + + $this->assertCommandRegistered('bar'); + } +}