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
Fix AsCronTask not passing arguments to command
  • Loading branch information
jan-pintr authored and nicolas-grekas committed Jul 9, 2025
commit b57a8154843eb1e6c4ece24526838cad268f2ca3
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Scheduler\Attribute\AsCronTask;

#[AsCronTask(expression: '* * * * *', schedule: 'dummy_command')]
#[AsCronTask(expression: '0 * * * *', arguments: 'test', schedule: 'dummy_command')]
#[AsCommand(name: 'test:dummy-command')]
class DummyCommand extends Command
{
public static array $calls = [];

public function configure(): void
{
$this->addArgument('dummy-argument', InputArgument::OPTIONAL);
}

public function execute(InputInterface $input, ?OutputInterface $output = null): int
{
self::$calls[__FUNCTION__][] = $input->getArgument('dummy-argument');

return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\BarMessage;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyCommand;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummySchedule;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyTask;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\FooMessage;
Expand Down Expand Up @@ -88,6 +89,29 @@ public function testAutoconfiguredScheduler()
$this->assertSame([['5', 6], ['7', 8]], $calls['attributesOnMethod']);
}

public function testAutoconfiguredSchedulerCommand()
{
$container = self::getContainer();
$container->set('clock', $clock = new MockClock('2023-10-26T08:59:59Z'));

$this->assertTrue($container->get('receivers')->has('scheduler_dummy_command'));
$this->assertInstanceOf(SchedulerTransport::class, $cron = $container->get('receivers')->get('scheduler_dummy_command'));
$bus = $container->get(MessageBusInterface::class);

$getCalls = static function (float $sleep) use ($clock, $cron, $bus) {
DummyCommand::$calls = [];
$clock->sleep($sleep);
foreach ($cron->get() as $message) {
$bus->dispatch($message->with(new ReceivedStamp('scheduler_dummy_command')));
}

return DummyCommand::$calls;
};

$this->assertSame([], $getCalls(0));
$this->assertSame(['execute' => [0 => null, 1 => 'test']], $getCalls(1));
}

public function testSchedulerWithCustomTransport()
{
$container = self::getContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ services:
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyTaskWithCustomReceiver:
autoconfigure: true

Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyCommand:
autoconfigure: true

clock:
synthetic: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function process(ContainerBuilder $container): void
if ($serviceDefinition->hasTag('console.command')) {
/** @var AsCommand|null $attribute */
$attribute = ($container->getReflectionClass($serviceDefinition->getClass())->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
$message = new Definition(RunCommandMessage::class, [$attribute?->name ?? $serviceDefinition->getClass()::getDefaultName().(empty($tagAttributes['arguments']) ? '' : " {$tagAttributes['arguments']}")]);
$commandName = $attribute?->name ?? $serviceDefinition->getClass()::getDefaultName();

$message = new Definition(RunCommandMessage::class, [$commandName.($tagAttributes['arguments'] ? " {$tagAttributes['arguments']}" : '')]);
} else {
$message = new Definition(ServiceCallMessage::class, [$serviceId, $tagAttributes['method'] ?? '__invoke', (array) ($tagAttributes['arguments'] ?? [])]);
}
Expand Down
Loading