Skip to content

Commit

Permalink
Added ConsoleTrait.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Dec 17, 2024
1 parent 4878567 commit 58ce3f0
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
107 changes: 107 additions & 0 deletions tests/phpunit/Traits/ConsoleTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace YourNamespace\App\Tests\Traits;

use PHPUnit\Framework\AssertionFailedError;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\ApplicationTester;

/**
* Trait ConsoleTrait.
*
* Helpers to work with Console.
*
* @phpstan-ignore trait.unused
*/
trait ConsoleTrait {

/**
* Application tester.
*/
protected ApplicationTester $appTester;

/**
* Initialize application tester.
*
* @param string|object $object_or_class
* Command class or object.
* @param bool $is_single_command
* Is single command. Defaults to TRUE.
*/
protected function consoleInitApplicationTester(string|object $object_or_class, bool $is_single_command = TRUE): void {
$application = new Application();

$instance = is_object($object_or_class) ? $object_or_class : new $object_or_class();
if (!$instance instanceof Command) {
throw new \InvalidArgumentException('The provided object is not an instance of Command');
}

$application->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<string, string> $input
* Input arguments.
* @param array<string, string> $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;
}

}
2 changes: 2 additions & 0 deletions tests/phpunit/Traits/FixtureTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Trait FixtureTrait.
*
* Helpers to work with fixture files.
*
* @phpstan-ignore trait.unused
*/
trait FixtureTrait {

Expand Down

0 comments on commit 58ce3f0

Please sign in to comment.