Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Scheduler] Fix scheduler.task tag arguments optionality
  • Loading branch information
Jan Pintr authored and nicolas-grekas committed Aug 5, 2025
commit ad08041e57d1642bf71b39ae005d3401dc30b705
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function process(ContainerBuilder $container): void
$attribute = ($container->getReflectionClass($serviceDefinition->getClass())->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
$commandName = $attribute?->name ?? $serviceDefinition->getClass()::getDefaultName();

$message = new Definition(RunCommandMessage::class, [$commandName.($tagAttributes['arguments'] ? " {$tagAttributes['arguments']}" : '')]);
$message = new Definition(RunCommandMessage::class, [$commandName.(($tagAttributes['arguments'] ?? null) ? " {$tagAttributes['arguments']}" : '')]);
} else {
$message = new Definition(ServiceCallMessage::class, [$serviceId, $tagAttributes['method'] ?? '__invoke', (array) ($tagAttributes['arguments'] ?? [])]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Scheduler\DependencyInjection\AddScheduleMessengerPass;

class AddScheduleMessengerPassTest extends TestCase
{
/**
* @dataProvider processSchedulerTaskCommandProvider
*/
public function testProcessSchedulerTaskCommand(array $arguments, string $exceptedCommand)
{
$container = new ContainerBuilder();

$definition = new Definition(SchedulableCommand::class);
$definition->addTag('console.command');
$definition->addTag('scheduler.task', $arguments);
$container->setDefinition(SchedulableCommand::class, $definition);

(new AddScheduleMessengerPass())->process($container);

$schedulerProvider = $container->getDefinition('scheduler.provider.default');
$calls = $schedulerProvider->getMethodCalls();

$this->assertCount(1, $calls);
$this->assertCount(2, $calls[0]);

$messageDefinition = $calls[0][1][0];
$messageArguments = $messageDefinition->getArgument('$message');
$command = $messageArguments->getArgument(0);

$this->assertSame($exceptedCommand, $command);
}

public static function processSchedulerTaskCommandProvider(): iterable
{
yield 'no arguments' => [['trigger' => 'every', 'frequency' => '1 hour'], 'schedulable'];
yield 'null arguments' => [['trigger' => 'every', 'frequency' => '1 hour', 'arguments' => null], 'schedulable'];
yield 'empty arguments' => [['trigger' => 'every', 'frequency' => '1 hour', 'arguments' => ''], 'schedulable'];
yield 'test argument' => [['trigger' => 'every', 'frequency' => '1 hour', 'arguments' => 'test'], 'schedulable test'];
}
}

#[AsCommand(name: 'schedulable')]
class SchedulableCommand
{
public function __invoke(): void
{
}
}