|
| 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