8000 [Console] add `RunCommandContext` and `RunCommandFailedException` · symfony/symfony@e2922e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit e2922e6

Browse files
committed
[Console] add RunCommandContext and RunCommandFailedException
1 parent b1384cc commit e2922e6

File tree

5 files changed

+80
-14
lines changed

5 files changed

+80
-14
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Exception;
13+
14+
use Symfony\Component\Console\Messenger\RunCommandContext;
15+
16+
/**
17+
* @author Kevin Bond <kevinbond@gmail.com>
18+
*/
19+
final class RunCommandFailedException extends RuntimeException
20+
{
21+
public function __construct(\Throwable $exception, public readonly RunCommandContext $context)
22+
{
23+
parent::__construct($exception->getMessage(), $exception->getCode(), $exception);
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Messenger;
13+
14+
/**
15+
* @author Kevin Bond <kevinbond@gmail.com>
16+
*/
17+
final class RunCommandContext extends RunCommandMessage
18+
{
19+
public function __construct(RunCommandMessage $message, public readonly int $exitCode, public readonly string $output)
20+
{
21+
parent::__construct($message->input, $message->catchExceptions);
22+
}
23+
}

src/Symfony/Component/Console/Messenger/RunCommandMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @author Kevin Bond <kevinbond@gmail.com>
1616
*/
17-
final class RunCommandMessage implements \Stringable
17+
class RunCommandMessage implements \Stringable
1818
{
1919
public function __construct(
2020
public readonly string $input,

src/Symfony/Component/Console/Messenger/RunCommandMessageHandler.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Console\Messenger;
1313

1414
use Symfony\Component\Console\Application;
15+
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Exception\RunCommandFailedException;
1517
use Symfony\Component\Console\Input\StringInput;
1618
use Symfony\Component\Console\Output\BufferedOutput;
1719

@@ -24,14 +26,19 @@ public function __construct(private readonly Application $application)
2426
{
2527
}
2628

27-
public function __invoke(RunCommandMessage $message): BufferedOutput
29+
public function __invoke(RunCommandMessage $message): RunCommandContext
2830
{
2931
$input = new StringInput($message->input);
3032
$output = new BufferedOutput();
3133

3234
$this->application->setCatchExceptions($message->catchExceptions);
33-
$this->application->run($input, $output);
3435

35-
return $output;
36+
try {
37+
$exitCode = $this->application->run($input, $output);
38+
} catch (\Throwable $e) {
39+
throw new RunCommandFailedException($e, new RunCommandContext($message, Command::FAILURE, $output->fetch()));
40+
}
41+
42+
return new RunCommandContext($message, $exitCode, $output->fetch());
3643
}
3744
}

src/Symfony/Component/Console/Tests/Messenger/RunCommandMessageHandlerTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Application;
1616
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Exception\RunCommandFailedException;
1718
use Symfony\Component\Console\Input\InputInterface;
1819
use Symfony\Component\Console\Messenger\RunCommandMessage;
1920
use Symfony\Component\Console\Messenger\RunCommandMessageHandler;
@@ -27,28 +28,38 @@ final class RunCommandMessageHandlerTest extends TestCase
2728
public function testExecutesCommand()
2829
{
2930
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());
30-
$output = $handler(new RunCommandMessage('test:command'))->fetch();
31+
$context = $handler(new RunCommandMessage('test:command'));
3132

32-
$this->assertStringContainsString('some message', $output);
33+
$this->assertSame(0, $context->exitCode);
34+
$this->assertStringContainsString('some message', $context->output);
3335
}
3436

3537
public function testExecutesCommandThatThrowsException()
3638
{
3739
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());
3840

39-
$this->expectException(\RuntimeException::class);
40-
$this->expectExceptionMessage('exception message');
41+
try {
42+
$handler(new RunCommandMessage('test:command --throw'));
43+
} catch (RunCommandFailedException $e) {
44+
$this->assertSame(1, $e->context->exitCode);
45+
$this->assertStringContainsString('some message', $e->context->output);
46+
$this->assertInstanceOf(\RuntimeException::class, $e->getPrevious());
47+
$this->assertSame('exception message', $e->getMessage());
4148

42-
$handler(new RunCommandMessage('test:command --throw'));
49+
return;
50+
}
51+
52+
$this->fail('Exception not thrown.');
4353
}
4454

4555
public function testExecutesCommandThatCatchesThrownException()
4656
{
4757
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());
48-
$output = $handler(new RunCommandMessage('test:command --throw -v', catchExceptions: true))->fetch();
58+
$context = $handler(new RunCommandMessage('test:command --throw -v', catchExceptions: true));
4959

50-
$this->assertStringContainsString('[RuntimeException]', $output);
51-
$this->assertStringContainsString('exception message', $output);
60+
$this->assertSame(1, $context->exitCode);
61+
$this->assertStringContainsString('[RuntimeException]', $context->output);
62+
$this->assertStringContainsString('exception message', $context->output);
5263
}
5364

5465
private function createApplicationWithCommand(): Application
@@ -67,12 +78,12 @@ public function configure(): void
6778

6879
protected function execute(InputInterface $input, OutputInterface $output): int
6980
{
81+
$output->write('some message');
82+
7083
if ($input->getOption('throw')) {
7184
throw new \RuntimeException('exception message');
7285
}
7386

74-
$output->write('some message');
75-
7687
return Command::SUCCESS;
7788
}
7889
},

0 commit comments

Comments
 (0)
0