Skip to content

Commit b57a815

Browse files
jan-pintrnicolas-grekas
authored andcommitted
Fix AsCronTask not passing arguments to command
1 parent 9a8d3aa commit b57a815

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Scheduler\Attribute\AsCronTask;
11+
12+
#[AsCronTask(expression: '* * * * *', schedule: 'dummy_command')]
13+
#[AsCronTask(expression: '0 * * * *', arguments: 'test', schedule: 'dummy_command')]
14+
#[AsCommand(name: 'test:dummy-command')]
15+
class DummyCommand extends Command
16+
{
17+
public static array $calls = [];
18+
19+
public function configure(): void
20+
{
21+
$this->addArgument('dummy-argument', InputArgument::OPTIONAL);
22+
}
23+
24+
public function execute(InputInterface $input, ?OutputInterface $output = null): int
25+
{
26+
self::$calls[__FUNCTION__][] = $input->getArgument('dummy-argument');
27+
28+
return Command::SUCCESS;
29+
}
30+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SchedulerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
1313

1414
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\BarMessage;
15+
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyCommand;
1516
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummySchedule;
1617
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyTask;
1718
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\FooMessage;
@@ -88,6 +89,29 @@ public function testAutoconfiguredScheduler()
8889
$this->assertSame([['5', 6], ['7', 8]], $calls['attributesOnMethod']);
8990
}
9091

92+
public function testAutoconfiguredSchedulerCommand()
93+
{
94+
$container = self::getContainer();
95+
$container->set('clock', $clock = new MockClock('2023-10-26T08:59:59Z'));
96+
97+
$this->assertTrue($container->get('receivers')->has('scheduler_dummy_command'));
98+
$this->assertInstanceOf(SchedulerTransport::class, $cron = $container->get('receivers')->get('scheduler_dummy_command'));
99+
$bus = $container->get(MessageBusInterface::class);
100+
101+
$getCalls = static function (float $sleep) use ($clock, $cron, $bus) {
102+
DummyCommand::$calls = [];
103+
$clock->sleep($sleep);
104+
foreach ($cron->get() as $message) {
105+
$bus->dispatch($message->with(new ReceivedStamp('scheduler_dummy_command')));
106+
}
107+
108+
return DummyCommand::$calls;
109+
};
110+
111+
$this->assertSame([], $getCalls(0));
112+
$this->assertSame(['execute' => [0 => null, 1 => 'test']], $getCalls(1));
113+
}
114+
91115
public function testSchedulerWithCustomTransport()
92116
{
93117
$container = self::getContainer();

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Scheduler/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ services:
1616
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyTaskWithCustomReceiver:
1717
autoconfigure: true
1818

19+
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyCommand:
20+
autoconfigure: true
21+
1922
clock:
2023
synthetic: true
2124

src/Symfony/Component/Scheduler/DependencyInjection/AddScheduleMessengerPass.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public function process(ContainerBuilder $container): void
5858
if ($serviceDefinition->hasTag('console.command')) {
5959
/** @var AsCommand|null $attribute */
6060
$attribute = ($container->getReflectionClass($serviceDefinition->getClass())->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
61-
$message = new Definition(RunCommandMessage::class, [$attribute?->name ?? $serviceDefinition->getClass()::getDefaultName().(empty($tagAttributes['arguments']) ? '' : " {$tagAttributes['arguments']}")]);
61+
$commandName = $attribute?->name ?? $serviceDefinition->getClass()::getDefaultName();
62+
63+
$message = new Definition(RunCommandMessage::class, [$commandName.($tagAttributes['arguments'] ? " {$tagAttributes['arguments']}" : '')]);
6264
} else {
6365
$message = new Definition(ServiceCallMessage::class, [$serviceId, $tagAttributes['method'] ?? '__invoke', (array) ($tagAttributes['arguments'] ?? [])]);
6466
}

0 commit comments

Comments
 (0)