Skip to content

Commit

Permalink
[feature] run commands via application (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored Oct 22, 2021
1 parent ba07fa5 commit 19e0705
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
5 changes: 3 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="Zenstruck\Console\Test\Tests\Fixture\Kernel" />
<server name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0"/>
<env name="KERNEL_CLASS" value="Zenstruck\Console\Test\Tests\Fixture\Kernel" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0"/>
<env name="COLUMNS" value="120" />
<env name="SHELL_VERBOSITY" value="-1"/>
</php>

<testsuites>
Expand Down
20 changes: 16 additions & 4 deletions src/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@
*/
final class TestCommand
{
private Command $command;
private Application $application;
private string $cli;
private array $inputs = [];
private bool $splitOutputStreams = false;

private function __construct(Command $command, string $cli)
{
if (!$command->getApplication()) {
$command->setApplication(new Application());
$application = new Application();
$application->add($command);

$command->setApplication($application);
}

$this->command = $command;
$this->application = $command->getApplication();
$this->cli = $cli;
}

Expand Down Expand Up @@ -90,11 +93,20 @@ public function withInput(array $inputs): self

public function execute(?string $cli = null): CommandResult
{
$status = $this->command->run(
$autoExit = $this->application->isAutoExitEnabled();
$catchExceptions = $this->application->areExceptionsCaught();

$this->application->setAutoExit(false);
$this->application->setCatchExceptions(false);

$status = $this->application->run(
$input = new TestInput($cli ? \sprintf('%s %s', $this->cli, $cli) : $this->cli, $this->inputs),
$output = new TestOutput($this->splitOutputStreams, $input)
);

$this->application->setAutoExit($autoExit);
$this->application->setCatchExceptions($catchExceptions);

return new CommandResult($status, $output);
}
}
11 changes: 8 additions & 3 deletions src/TestInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(string $input, array $inputs)
{
parent::__construct($input);

$this->setInteractive(false);
parent::setInteractive(false);

if ($inputs) {
$stream = \fopen('php://memory', 'r+', false);
Expand All @@ -28,14 +28,19 @@ public function __construct(string $input, array $inputs)
\rewind($stream);

$this->setStream($stream);
$this->setInteractive(true);
parent::setInteractive(true);
}

if (true === $this->hasParameterOption(['--no-interaction', '-n'], true)) {
$this->setInteractive(false);
parent::setInteractive(false);
}
}

public function setInteractive($interactive): void
{
// noop, prevent Application from setting this value
}

public function isDecorated(): bool
{
return true === $this->hasParameterOption(['--ansi'], true);
Expand Down
10 changes: 10 additions & 0 deletions src/TestOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public function section(): ConsoleSectionOutput
return new ConsoleSectionOutput($this->getStream(), $this->sections, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
}

public function setDecorated($decorated): void
{
// noop, prevent Application from setting this value
}

public function setVerbosity($level): void
{
// noop, prevent Application from setting this value
}

public function getDisplay(): string
{
\rewind($this->getStream());
Expand Down

0 comments on commit 19e0705

Please sign in to comment.