diff --git a/tests/phpunit/Traits/ConsoleTrait.php b/tests/phpunit/Traits/ConsoleTrait.php new file mode 100644 index 0000000..5acae39 --- /dev/null +++ b/tests/phpunit/Traits/ConsoleTrait.php @@ -0,0 +1,107 @@ +add($instance); + + $name = $instance->getName(); + if (empty($name)) { + $ret = $this->getProtectedValue($instance, 'defaultName'); + if (!empty($ret) || !is_string($ret)) { + throw new \InvalidArgumentException('The provided object does not have a valid name'); + } + $name = $ret; + } + + $application->setDefaultCommand($name, $is_single_command); + + $application->setAutoExit(FALSE); + $application->setCatchExceptions(FALSE); + if (method_exists($application, 'setCatchErrors')) { + $application->setCatchErrors(FALSE); + } + + $this->appTester = new ApplicationTester($application); + } + + /** + * Run console application. + * + * @param array $input + * Input arguments. + * @param array $options + * Options. + * @param bool $expect_fail + * Whether a failure is expected. Defaults to FALSE. + * + * @return string + * Run output (stdout or stderr). + */ + protected function consoleApplicationRun(array $input = [], array $options = [], bool $expect_fail = FALSE): string { + $options += ['capture_stderr_separately' => TRUE]; + + try { + $this->appTester->run($input, $options); + $output = $this->appTester->getDisplay(); + + if ($this->appTester->getStatusCode() !== 0) { + throw new \Exception(sprintf("Application exited with non-zero code.\nThe output was:\n%s\nThe error output was:\n%s", $this->appTester->getDisplay(), $this->appTester->getErrorOutput())); + } + + if ($expect_fail) { + throw new AssertionFailedError(sprintf("Application exited successfully but should not.\nThe output was:\n%s\nThe error output was:\n%s", $this->appTester->getDisplay(), $this->appTester->getErrorOutput())); + } + } + catch (\RuntimeException $exception) { + if (!$expect_fail) { + throw new AssertionFailedError('Application exited with an error:' . PHP_EOL . $exception->getMessage()); + } + $output = $exception->getMessage(); + } + catch (\Exception $exception) { + if (!$expect_fail) { + throw new AssertionFailedError('Application exited with an error:' . PHP_EOL . $exception->getMessage()); + } + } + + return $output; + } + +} diff --git a/tests/phpunit/Traits/FixtureTrait.php b/tests/phpunit/Traits/FixtureTrait.php index a984f89..1ff2395 100644 --- a/tests/phpunit/Traits/FixtureTrait.php +++ b/tests/phpunit/Traits/FixtureTrait.php @@ -10,6 +10,8 @@ * Trait FixtureTrait. * * Helpers to work with fixture files. + * + * @phpstan-ignore trait.unused */ trait FixtureTrait {