-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Console] add
ExecuteCommand
and ExecuteCommandHandler
- Loading branch information
Showing
7 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/Console/Messenger/ExecuteCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Messenger; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class ExecuteCommand implements \Stringable | ||
{ | ||
public function __construct( | ||
public readonly string $input, | ||
public readonly bool $catchExceptions = false, | ||
) { | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->input; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Symfony/Component/Console/Messenger/ExecuteCommandHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Messenger; | ||
|
||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Input\StringInput; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class ExecuteCommandHandler | ||
{ | ||
public function __construct(private readonly Application $application) | ||
{ | ||
} | ||
|
||
public function __invoke(ExecuteCommand $message): BufferedOutput | ||
{ | ||
$input = new StringInput($message->input); | ||
$output = new BufferedOutput(); | ||
|
||
$this->application->setCatchExceptions($message->catchExceptions); | ||
$this->application->run($input, $output); | ||
|
||
return $output; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/Symfony/Component/Console/Tests/Messenger/ExecuteCommandHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Tests\Messenger; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Messenger\ExecuteCommand; | ||
use Symfony\Component\Console\Messenger\ExecuteCommandHandler; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class ExecuteCommandHandlerTest extends TestCase | ||
{ | ||
public function testExecutesCommand() | ||
{ | ||
$handler = new ExecuteCommandHandler($this->createApplicationWithCommand()); | ||
$output = $handler(new ExecuteCommand('test:command'))->fetch(); | ||
|
||
$this->assertStringContainsString('some message', $output); | ||
} | ||
|
||
public function testExecutesCommandThatThrowsException() | ||
{ | ||
$handler = new ExecuteCommandHandler($this->createApplicationWithCommand()); | ||
|
||
$this->expectException(\RuntimeException::class); | ||
$this->expectExceptionMessage('exception message'); | ||
|
||
$handler(new ExecuteCommand('test:command --throw')); | ||
} | ||
|
||
public function testExecutesCommandThatCatchesThrownException() | ||
{ | ||
$handler = new ExecuteCommandHandler($this->createApplicationWithCommand()); | ||
$output = $handler(new ExecuteCommand('test:command --throw -v', catchExceptions: true))->fetch(); | ||
|
||
$this->assertStringContainsString('[RuntimeException]', $output); | ||
$this->assertStringContainsString('exception message', $output); | ||
} | ||
|
||
private function createApplicationWithCommand(): Application | ||
{ | ||
$application = new Application(); | ||
$application->setAutoExit(false); | ||
$application->addCommands([ | ||
new class() extends Command | ||
{ | ||
public function configure(): void | ||
{ | ||
$this | ||
->setName('test:command') | ||
->addOption('throw') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
if ($input->getOption('throw')) { | ||
throw new \RuntimeException('exception message'); | ||
} | ||
|
||
$output->write('some message'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} | ||
]); | ||
|
||
return $application; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters