|
| 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\Scheduler\Tests\Messenger; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer; |
| 16 | +use Symfony\Component\Scheduler\Trigger\CallbackTrigger; |
| 17 | +use Symfony\Component\Scheduler\Trigger\PeriodicalTrigger; |
| 18 | +use Symfony\Component\Scheduler\Trigger\TriggerInterface; |
| 19 | + |
| 20 | +class SchedulerTriggerNormalizerTest extends TestCase |
| 21 | +{ |
| 22 | + private SchedulerTriggerNormalizer $normalizer; |
| 23 | + |
| 24 | + /** |
| 25 | + * @before |
| 26 | + */ |
| 27 | + protected function setUpNormalizer(): void |
| 28 | + { |
| 29 | + $this->normalizer = new SchedulerTriggerNormalizer(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @dataProvider normalizeProvider |
| 34 | + */ |
| 35 | + public function testNormalize(mixed $data, mixed $expected) |
| 36 | + { |
| 37 | + self::assertSame($expected, $this->normalizer->normalize($data)); |
| 38 | + } |
| 39 | + |
| 40 | + public static function normalizeProvider(): iterable |
| 41 | + { |
| 42 | + yield 'CallbackTrigger' => [new CallbackTrigger(fn () => null, 'test1'), 'test1']; |
| 43 | + yield 'PeriodicalTrigger' => [new PeriodicalTrigger(5), 'every 5 seconds']; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @dataProvider supportsNormalizationProvider |
| 48 | + */ |
| 49 | + public function testSupportsNormalization(mixed $data, array $context, bool $expected) |
| 50 | + { |
| 51 | + self::assertSame($expected, $this->normalizer->supportsNormalization($data, 'json', $context)); |
| 52 | + } |
| 53 | + |
| 54 | + public static function supportsNormalizationProvider(): iterable |
| 55 | + { |
| 56 | + yield 'CallbackTrigger, messenger context' => [new CallbackTrigger(fn () => null, 'test1'), ['messenger_serialization' => true], true]; |
| 57 | + yield 'CallbackTrigger, normal context' => [new CallbackTrigger(fn () => null, 'test1'), [], false]; |
| 58 | + yield 'PeriodicalTrigger, messenger context' => [new PeriodicalTrigger(5), ['messenger_serialization' => true], true]; |
| 59 | + yield 'PeriodicalTrigger, normal context' => [new PeriodicalTrigger(5), [], false]; |
| 60 | + yield 'stdClass, messenger context' => [new \stdClass(), ['messenger_serialization' => true], false]; |
| 61 | + yield 'stdClass, normal context' => [new \stdClass(), [], false]; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @dataProvider supportsDenormalizationProvider |
| 66 | + */ |
| 67 | + public function testSupportsDenormalization(mixed $data, string $type, array $context, bool $expected) |
| 68 | + { |
| 69 | + self::assertSame($expected, $this->normalizer->supportsDenormalization($data, $type, 'json', $context)); |
| 70 | + } |
| 71 | + |
| 72 | + public static function supportsDenormalizationProvider(): iterable |
| 73 | + { |
| 74 | + yield 'unknown type' => ['test', \stdClass::class, ['messenger_serialization' => true], false]; |
| 75 | + yield 'string, messenger context' => ['test', TriggerInterface::class, ['messenger_serialization' => true], true]; |
| 76 | + yield 'string, normal context' => ['test', TriggerInterface::class, [], false]; |
| 77 | + yield 'array, messenger context' => [['a' => 'b'], TriggerInterface::class, ['messenger_serialization' => true], false]; |
| 78 | + yield 'array, normal context' => [['a' => 'b'], TriggerInterface::class, [], false]; |
| 79 | + } |
| 80 | + |
| 81 | + public function testDenormalize() |
| 82 | + { |
| 83 | + $trigger = $this->normalizer->denormalize('every 5 seconds', TriggerInterface::class); |
| 84 | + self::assertSame('every 5 seconds', (string) $trigger); |
| 85 | + } |
| 86 | +} |
0 commit comments