Skip to content

Commit 8db23e8

Browse files
bug #60741 [Scheduler] Fix #[AsCronTask] not passing arguments to command (Jan Pintr, jan-pintr)
This PR was merged into the 7.3 branch. Discussion ---------- [Scheduler] Fix `#[AsCronTask]` not passing arguments to command | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT This MR fixes passing `AsCronTask` arguments to `Command`. Reproduced can be by following command. It wont receive `--all` option or any other argument specified in `AsCronTask` when executed by Scheduler. ``` #[AsCommand(name: 'debug:as-cron-task')] #[AsCronTask('* * * * *', arguments: '--all')] class SampleCommand extends Command { protected function configure(): void { $this->addOption('all', null, InputOption::VALUE_NONE); } protected function execute(InputInterface $input, OutputInterface $output): int { dump($input->getOption('all')); return Command::SUCCESS; } } ``` Fix description: When command name was found in `AsCommand` attribute then attributes were not appended to message definition due to missing brackets. Commits ------- b57a8154843 Fix AsCronTask not passing arguments to command
2 parents 9aaaf25 + 80848c0 commit 8db23e8

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
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+
}

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();

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

0 commit comments

Comments
 (0)