Skip to content

Commit d46d595

Browse files
bug #61307 [Scheduler] Fix scheduler.task tag arguments optionality (Jan Pintr)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Scheduler] Fix `scheduler.task` tag arguments optionality | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT According to [this comment](#60741 (comment)) there is a change in behaviour after #60741 which causes requiring specify `arguments` parameter when tagging `scheduler.task` manually. This pull request makes `arguments` parameter back optional. ``` $myService->tag('scheduler.task', [ 'trigger' => 'every', 'frequency' => 15, 'arguments' => null, // <- had to add this line to avoid a Warning: Undefined array key "arguments" ]); ``` Commits ------- ad08041 [Scheduler] Fix `scheduler.task` tag arguments optionality
2 parents a516b2c + ad08041 commit d46d595

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function process(ContainerBuilder $container): void
6060
$attribute = ($container->getReflectionClass($serviceDefinition->getClass())->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
6161
$commandName = $attribute?->name ?? $serviceDefinition->getClass()::getDefaultName();
6262

63-
$message = new Definition(RunCommandMessage::class, [$commandName.($tagAttributes['arguments'] ? " {$tagAttributes['arguments']}" : '')]);
63+
$message = new Definition(RunCommandMessage::class, [$commandName.(($tagAttributes['arguments'] ?? null) ? " {$tagAttributes['arguments']}" : '')]);
6464
} else {
6565
$message = new Definition(ServiceCallMessage::class, [$serviceId, $tagAttributes['method'] ?? '__invoke', (array) ($tagAttributes['arguments'] ?? [])]);
6666
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\DependencyInjection;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Attribute\AsCommand;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\Scheduler\DependencyInjection\AddScheduleMessengerPass;
19+
20+
class AddScheduleMessengerPassTest extends TestCase
21+
{
22+
/**
23+
* @dataProvider processSchedulerTaskCommandProvider
24+
*/
25+
public function testProcessSchedulerTaskCommand(array $arguments, string $exceptedCommand)
26+
{
27+
$container = new ContainerBuilder();
28+
29+
$definition = new Definition(SchedulableCommand::class);
30+
$definition->addTag('console.command');
31+
$definition->addTag('scheduler.task', $arguments);
32+
$container->setDefinition(SchedulableCommand::class, $definition);
33+
34+
(new AddScheduleMessengerPass())->process($container);
35+
36+
$schedulerProvider = $container->getDefinition('scheduler.provider.default');
37+
$calls = $schedulerProvider->getMethodCalls();
38+
39+
$this->assertCount(1, $calls);
40+
$this->assertCount(2, $calls[0]);
41+
42+
$messageDefinition = $calls[0][1][0];
43+
$messageArguments = $messageDefinition->getArgument('$message');
44+
$command = $messageArguments->getArgument(0);
45+
46+
$this->assertSame($exceptedCommand, $command);
47+
}
48+
49+
public static function processSchedulerTaskCommandProvider(): iterable
50+
{
51+
yield 'no arguments' => [['trigger' => 'every', 'frequency' => '1 hour'], 'schedulable'];
52+
yield 'null arguments' => [['trigger' => 'every', 'frequency' => '1 hour', 'arguments' => null], 'schedulable'];
53+
yield 'empty arguments' => [['trigger' => 'every', 'frequency' => '1 hour', 'arguments' => ''], 'schedulable'];
54+
yield 'test argument' => [['trigger' => 'every', 'frequency' => '1 hour', 'arguments' => 'test'], 'schedulable test'];
55+
}
56+
}
57+
58+
#[AsCommand(name: 'schedulable')]
59+
class SchedulableCommand
60+
{
61+
public function __invoke(): void
62+
{
63+
}
64+
}

0 commit comments

Comments
 (0)