Skip to content

Commit

Permalink
feat: Add support for using native Symfony commands (#201)
Browse files Browse the repository at this point in the history
Closes #195.
  • Loading branch information
theofidry authored Nov 17, 2023
1 parent cca27a2 commit 32d1637
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 9 deletions.
6 changes: 2 additions & 4 deletions doc/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
}

Expand Down
3 changes: 2 additions & 1 deletion src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Fidry\Console\Command\Command;
use Fidry\Console\Command\LazyCommandEnvelope;
use Symfony\Component\Console\Command\Command as SymfonyCommand;

interface Application
{
Expand Down Expand Up @@ -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<LazyCommandEnvelope|Command>
* @return array<LazyCommandEnvelope|Command|SymfonyCommand>
*/
public function getCommands(): array;

Expand Down
3 changes: 2 additions & 1 deletion src/Bridge/CommandLoader/CommandLoaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<LazyCommandEnvelope|FidryCommand> $commands
* @param array<LazyCommandEnvelope|FidryCommand|SymfonyCommand> $commands
*/
public function createCommandLoader(array $commands): CommandLoaderInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Fidry\Console package.
*
* (c) Théo FIDRY <[email protected]>
*
* 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,
);
}
}
42 changes: 42 additions & 0 deletions tests/Application/Fixture/ApplicationWithSymfonyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Fidry\Console package.
*
* (c) Théo FIDRY <[email protected]>
*
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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',
],
];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Command/Fixture/SimpleSymfonyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 32d1637

Please sign in to comment.