Skip to content

Commit

Permalink
[bug] Allow to add lazy commands by the inner command classname (#67)
Browse files Browse the repository at this point in the history
Co-authored-by: Giso Stallenberg <[email protected]>
  • Loading branch information
gisostallenberg and gisostallenberg authored Jul 20, 2022
1 parent 191e113 commit 99f8d9a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Schedule/Task/CommandTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LazyCommand;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\StringInput;
Expand Down Expand Up @@ -78,7 +79,11 @@ public function createCommand(Application $application): Command
}

foreach ($registeredCommands as $command) {
if ($this->name === \get_class($command)) {
$className = \get_class($command);
if ($command instanceof LazyCommand) {
$className = \get_class($command->getCommand());
}
if ($this->name === $className) {
return $command;
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/Schedule/Task/CommandTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LazyCommand;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Input\StringInput;
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
Expand Down Expand Up @@ -94,6 +95,33 @@ public function unregistered_command_throws_exception()

$task->createCommandInput(new Application());
}

/**
* @test
*/
public function can_create_from_lazy_command()
{
if (!\class_exists(LazyCommand::class)) {
$this->markTestSkipped('Not available before Symfony 5.3.');

return;
}
$dummyCommand = new LazyDummyCommand();
$command = new LazyCommand($dummyCommand->getName(), $dummyCommand->getAliases(), $dummyCommand->getDescription(), $dummyCommand->isHidden(), function() use ($dummyCommand) {
return $dummyCommand;
});

$application = new Application();
$application->add($command);

$task = new CommandTask(LazyDummyCommand::class);

$command = $task->createCommand($application);

$this->assertInstanceOf(Command::class, $command);
$this->assertSame('lazy:dummy:command', (string) $command->getName());
$this->assertInstanceOf(LazyCommand::class, $application->all()['lazy:dummy:command']);
}
}

final class DummyCommand extends Command
Expand All @@ -103,3 +131,10 @@ public static function getDefaultName(): string
return 'dummy:command';
}
}
final class LazyDummyCommand extends Command
{
public static function getDefaultName(): string
{
return 'lazy:dummy:command';
}
}

0 comments on commit 99f8d9a

Please sign in to comment.