From 32d163766a662ea216d1e0936451f2f9559566c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Fri, 17 Nov 2023 22:42:29 +0100 Subject: [PATCH] feat: Add support for using native Symfony commands (#201) Closes #195. --- doc/application.md | 6 +-- src/Application/Application.php | 3 +- .../CommandLoader/CommandLoaderFactory.php | 3 +- .../SymfonyFactoryCommandLoaderFactory.php | 6 ++- .../ApplicationSymfonyCommandsSupportTest.php | 43 +++++++++++++++++++ .../Fixture/ApplicationWithSymfonyCommand.php | 42 ++++++++++++++++++ ...SymfonyFactoryCommandLoaderFactoryTest.php | 8 ++++ .../Command/Fixture/SimpleSymfonyCommand.php | 4 +- 8 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 tests/Application/Feature/ApplicationSymfonyCommandsSupportTest.php create mode 100644 tests/Application/Fixture/ApplicationWithSymfonyCommand.php diff --git a/doc/application.md b/doc/application.md index f43a962..41b874d 100644 --- a/doc/application.md +++ b/doc/application.md @@ -66,10 +66,8 @@ final class Application implements FidryApplication CreateUserCommand::class, static fn () => new CreateUserCommand(/*...*/), ), - // A regular Symfony command - new ReversedSymfonyCommand( - new OriginalUpdateUserSymfonyCommand(), - ), + // A regular Symfony command: yes it just works! + new OriginalUpdateUserSymfonyCommand(), ]; } diff --git a/src/Application/Application.php b/src/Application/Application.php index daded4f..176f26c 100644 --- a/src/Application/Application.php +++ b/src/Application/Application.php @@ -15,6 +15,7 @@ use Fidry\Console\Command\Command; use Fidry\Console\Command\LazyCommandEnvelope; +use Symfony\Component\Console\Command\Command as SymfonyCommand; interface Application { @@ -44,7 +45,7 @@ public function getHelp(): string; * Exhaustive list of the custom commands. A few more commands such as * the HelpCommand or ListCommand are also included besides those. * - * @return array + * @return array */ public function getCommands(): array; diff --git a/src/Bridge/CommandLoader/CommandLoaderFactory.php b/src/Bridge/CommandLoader/CommandLoaderFactory.php index 9daab9b..4c8aa43 100644 --- a/src/Bridge/CommandLoader/CommandLoaderFactory.php +++ b/src/Bridge/CommandLoader/CommandLoaderFactory.php @@ -15,12 +15,13 @@ use Fidry\Console\Command\Command as FidryCommand; use Fidry\Console\Command\LazyCommandEnvelope; +use Symfony\Component\Console\Command\Command as SymfonyCommand; use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; interface CommandLoaderFactory { /** - * @param array $commands + * @param array $commands */ public function createCommandLoader(array $commands): CommandLoaderInterface; } diff --git a/src/Bridge/CommandLoader/SymfonyFactoryCommandLoaderFactory.php b/src/Bridge/CommandLoader/SymfonyFactoryCommandLoaderFactory.php index 3d72f96..bd0045d 100644 --- a/src/Bridge/CommandLoader/SymfonyFactoryCommandLoaderFactory.php +++ b/src/Bridge/CommandLoader/SymfonyFactoryCommandLoaderFactory.php @@ -42,8 +42,12 @@ public function createCommandLoader(array $commands): CommandLoaderInterface return new FactoryCommandLoader($factories); } - private function createCommand(LazyCommandEnvelope|FidryCommand $commandOrCommandFactory): SymfonyNativeCommand + private function createCommand(LazyCommandEnvelope|FidryCommand|SymfonyNativeCommand $commandOrCommandFactory): SymfonyNativeCommand { + if ($commandOrCommandFactory instanceof SymfonyNativeCommand) { + return $commandOrCommandFactory; + } + return $commandOrCommandFactory instanceof FidryCommand ? $this->commandFactory->crateSymfonyCommand($commandOrCommandFactory) : $this->commandFactory->crateSymfonyLazyCommand( diff --git a/tests/Application/Feature/ApplicationSymfonyCommandsSupportTest.php b/tests/Application/Feature/ApplicationSymfonyCommandsSupportTest.php new file mode 100644 index 0000000..e99ed20 --- /dev/null +++ b/tests/Application/Feature/ApplicationSymfonyCommandsSupportTest.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Fidry\Console\Tests\Application\Feature; + +use Fidry\Console\Application\ApplicationRunner; +use Fidry\Console\Tests\Application\Fixture\ApplicationWithSymfonyCommand; +use Fidry\Console\Tests\Application\OutputAssertions; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\StringInput; +use Symfony\Component\Console\Output\BufferedOutput; + +final class ApplicationSymfonyCommandsSupportTest extends TestCase +{ + public function test_it_can_execute_the_symfony_command(): void + { + $input = new StringInput('symfony-cmd'); + $output = new BufferedOutput(); + + ApplicationRunner::runApplication( + new ApplicationWithSymfonyCommand(), + $input, + $output, + ); + + $actual = $output->fetch(); + + OutputAssertions::assertSameOutput( + '', + $actual, + ); + } +} diff --git a/tests/Application/Fixture/ApplicationWithSymfonyCommand.php b/tests/Application/Fixture/ApplicationWithSymfonyCommand.php new file mode 100644 index 0000000..a26c042 --- /dev/null +++ b/tests/Application/Fixture/ApplicationWithSymfonyCommand.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Fidry\Console\Tests\Application\Fixture; + +use Fidry\Console\Application\BaseApplication; +use Fidry\Console\Tests\Command\Fixture\SimpleSymfonyCommand; + +final class ApplicationWithSymfonyCommand extends BaseApplication +{ + public function getCommands(): array + { + return [ + new SimpleSymfonyCommand(), + ]; + } + + public function getName(): string + { + return 'ApplicationWithSymfonyCommand'; + } + + public function getVersion(): string + { + return '0.0'; + } + + public function isAutoExitEnabled(): bool + { + return false; + } +} diff --git a/tests/Bridge/CommandLoader/SymfonyFactoryCommandLoaderFactoryTest.php b/tests/Bridge/CommandLoader/SymfonyFactoryCommandLoaderFactoryTest.php index 7d5fb2d..ee4a710 100644 --- a/tests/Bridge/CommandLoader/SymfonyFactoryCommandLoaderFactoryTest.php +++ b/tests/Bridge/CommandLoader/SymfonyFactoryCommandLoaderFactoryTest.php @@ -17,8 +17,10 @@ use Fidry\Console\Bridge\CommandLoader\CommandLoaderFactory; use Fidry\Console\Bridge\CommandLoader\SymfonyFactoryCommandLoaderFactory; use Fidry\Console\Command\LazyCommandEnvelope; +use Fidry\Console\Command\ReversedSymfonyCommand; use Fidry\Console\Tests\Command\Fixture\SimpleCommand; use Fidry\Console\Tests\Command\Fixture\SimpleLazyCommand; +use Fidry\Console\Tests\Command\Fixture\SimpleSymfonyCommand; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -60,11 +62,17 @@ public static function commandProvider(): iterable SimpleLazyCommand::class, static fn () => new SimpleLazyCommand(static function (): void {}), ), + new ReversedSymfonyCommand( + new SimpleSymfonyCommand('app:reversed-cmd'), + ), + new SimpleSymfonyCommand('app:original-cmd'), ], [ 'app:foo', 'app:lazy:foo', 'app:lazy', + 'app:reversed-cmd', + 'app:original-cmd', ], ]; } diff --git a/tests/Command/Fixture/SimpleSymfonyCommand.php b/tests/Command/Fixture/SimpleSymfonyCommand.php index bb008f5..8fc341f 100644 --- a/tests/Command/Fixture/SimpleSymfonyCommand.php +++ b/tests/Command/Fixture/SimpleSymfonyCommand.php @@ -19,9 +19,9 @@ final class SimpleSymfonyCommand extends Command { - public function __construct() + public function __construct(?string $name = null) { - parent::__construct('symfony-cmd'); + parent::__construct($name ?? 'symfony-cmd'); } protected function execute(InputInterface $input, OutputInterface $output): int