Skip to content

Commit 19e0705

Browse files
authored
[feature] run commands via application (#8)
1 parent ba07fa5 commit 19e0705

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

phpunit.xml.dist

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
>
1111
<php>
1212
<ini name="error_reporting" value="-1" />
13-
<server name="KERNEL_CLASS" value="Zenstruck\Console\Test\Tests\Fixture\Kernel" />
14-
<server name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0"/>
13+
<env name="KERNEL_CLASS" value="Zenstruck\Console\Test\Tests\Fixture\Kernel" />
14+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0"/>
1515
<env name="COLUMNS" value="120" />
16+
<env name="SHELL_VERBOSITY" value="-1"/>
1617
</php>
1718

1819
<testsuites>

src/TestCommand.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@
1010
*/
1111
final class TestCommand
1212
{
13-
private Command $command;
13+
private Application $application;
1414
private string $cli;
1515
private array $inputs = [];
1616
private bool $splitOutputStreams = false;
1717

1818
private function __construct(Command $command, string $cli)
1919
{
2020
if (!$command->getApplication()) {
21-
$command->setApplication(new Application());
21+
$application = new Application();
22+
$application->add($command);
23+
24+
$command->setApplication($application);
2225
}
2326

24-
$this->command = $command;
27+
$this->application = $command->getApplication();
2528
$this->cli = $cli;
2629
}
2730

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

9194
public function execute(?string $cli = null): CommandResult
9295
{
93-
$status = $this->command->run(
96+
$autoExit = $this->application->isAutoExitEnabled();
97+
$catchExceptions = $this->application->areExceptionsCaught();
98+
99+
$this->application->setAutoExit(false);
100+
$this->application->setCatchExceptions(false);
101+
102+
$status = $this->application->run(
94103
$input = new TestInput($cli ? \sprintf('%s %s', $this->cli, $cli) : $this->cli, $this->inputs),
95104
$output = new TestOutput($this->splitOutputStreams, $input)
96105
);
97106

107+
$this->application->setAutoExit($autoExit);
108+
$this->application->setCatchExceptions($catchExceptions);
109+
98110
return new CommandResult($status, $output);
99111
}
100112
}

src/TestInput.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct(string $input, array $inputs)
1616
{
1717
parent::__construct($input);
1818

19-
$this->setInteractive(false);
19+
parent::setInteractive(false);
2020

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

3030
$this->setStream($stream);
31-
$this->setInteractive(true);
31+
parent::setInteractive(true);
3232
}
3333

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

39+
public function setInteractive($interactive): void
40+
{
41+
// noop, prevent Application from setting this value
42+
}
43+
3944
public function isDecorated(): bool
4045
{
4146
return true === $this->hasParameterOption(['--ansi'], true);

src/TestOutput.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ public function section(): ConsoleSectionOutput
4848
return new ConsoleSectionOutput($this->getStream(), $this->sections, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
4949
}
5050

51+
public function setDecorated($decorated): void
52+
{
53+
// noop, prevent Application from setting this value
54+
}
55+
56+
public function setVerbosity($level): void
57+
{
58+
// noop, prevent Application from setting this value
59+
}
60+
5161
public function getDisplay(): string
5262
{
5363
\rewind($this->getStream());

0 commit comments

Comments
 (0)