Skip to content

Commit 0d9cf19

Browse files
authored
feat(framework): Add Symfony 7.4 compatibility with batch command registration (#7620)
1 parent f9bae8c commit 0d9cf19

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/ApplicationFactory.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ public function __invoke(ContainerInterface $container)
5252
$application->setDispatcher(new SymfonyEventDispatcher($eventDispatcher));
5353
}
5454

55-
foreach ($commands as $command) {
56-
$application->add(
57-
$this->pendingCommand($container->get($command))
58-
);
59-
}
55+
$application->addCommands(
56+
array_map(
57+
fn ($command) => $this->pendingCommand($container->get($command)),
58+
$commands
59+
)
60+
);
6061

6162
return $application;
6263
}

tests/ApplicationFactoryTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Psr\Container\ContainerInterface;
2424
use Psr\EventDispatcher\EventDispatcherInterface as PsrEventDispatcherInterface;
2525
use Symfony\Component\Console\Application;
26+
use Symfony\Component\Console\Command\Command;
2627
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2728

2829
/**
@@ -72,4 +73,34 @@ public function testInvokeApplicationWithoutSymfonyEventDispatcher()
7273
$application = new ClassInvoker((new ApplicationFactory())($container));
7374
$this->assertNull($application->dispatcher);
7475
}
76+
77+
public function testApplicationFactoryUsesAddCommandWhenAvailable()
78+
{
79+
$container = Mockery::mock(ContainerInterface::class);
80+
$container->shouldReceive('has')->andReturnFalse();
81+
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(
82+
new Config([
83+
'commands' => [TestCommand::class],
84+
])
85+
);
86+
$testCommand = new TestCommand();
87+
$container->shouldReceive('get')->with(TestCommand::class)->andReturn($testCommand);
88+
89+
/** @var Application $application */
90+
$application = (new ApplicationFactory())($container);
91+
$this->assertInstanceOf(Application::class, $application);
92+
93+
// Verify that the command was added to the application
94+
// The Application should have the command whether using add() or addCommand()
95+
$this->assertTrue($application->has('test:command'));
96+
}
97+
}
98+
99+
// Mock command class for testing
100+
class TestCommand extends Command
101+
{
102+
protected function configure(): void
103+
{
104+
$this->setName('test:command');
105+
}
75106
}

0 commit comments

Comments
 (0)