Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9f6583d

Browse filesBrowse files
committed
Normalize TriggerInterface as string
1 parent 54b61d4 commit 9f6583d
Copy full SHA for 9f6583d

File tree

3 files changed

+145
-0
lines changed
Filter options

3 files changed

+145
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/scheduler.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/scheduler.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Scheduler\EventListener\DispatchSchedulerEventListener;
1515
use Symfony\Component\Scheduler\Messenger\SchedulerTransportFactory;
16+
use Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer;
1617
use Symfony\Component\Scheduler\Messenger\ServiceCallMessageHandler;
1718

1819
return static function (ContainerConfigurator $container) {
@@ -34,5 +35,7 @@
3435
service('event_dispatcher'),
3536
])
3637
->tag('kernel.event_subscriber')
38+
->set('serializer.normalizer.scheduler_trigger', SchedulerTriggerNormalizer::class)
39+
->tag('serializer.normalizer', ['built_in' => true, 'priority' => -880])
3740
;
3841
};
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Messenger\Serializer\Normalizer;
13+
14+
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
15+
use Symfony\Component\Scheduler\Trigger\TriggerInterface;
16+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
17+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18+
19+
final class SchedulerTriggerNormalizer implements DenormalizerInterface, NormalizerInterface
20+
{
21+
22+
public function getSupportedTypes(?string $format): array
23+
{
24+
return [
25+
TriggerInterface::class => false,
26+
];
27+
}
28+
29+
/**
30+
* @param TriggerInterface $data
31+
*/
32+
public function normalize(mixed $data, ?string $format = null, array $context = []): string
33+
{
34+
return (string) $data;
35+
}
36+
37+
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
38+
{
39+
return $data instanceof TriggerInterface && ($context[Serializer::MESSENGER_SERIALIZATION_CONTEXT] ?? false);
40+
}
41+
42+
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): TriggerInterface
43+
{
44+
return new class($data) implements TriggerInterface
45+
{
46+
public function __construct(private readonly string $description)
47+
{
48+
}
49+
50+
public function __toString(): string
51+
{
52+
return $this->description;
53+
}
54+
55+
public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
56+
{
57+
throw new \LogicException('Not possible to get next run date from a deserialized trigger.');
58+
}
59+
};
60+
}
61+
62+
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
63+
{
64+
return TriggerInterface::class === $type && ($context[Serializer::MESSENGER_SERIALIZATION_CONTEXT] ?? false) && is_string($data);
65+
}
66+
}
+76Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Symfony\Component\Scheduler\Tests\Messenger;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer;
7+
use Symfony\Component\Scheduler\Trigger\CallbackTrigger;
8+
use Symfony\Component\Scheduler\Trigger\PeriodicalTrigger;
9+
use Symfony\Component\Scheduler\Trigger\TriggerInterface;
10+
11+
class SchedulerTriggerNormalizerTest extends TestCase
12+
{
13+
private SchedulerTriggerNormalizer $normalizer;
14+
15+
/**
16+
* @before
17+
*/
18+
protected function setUpNormalizer(): void
19+
{
20+
$this->normalizer = new SchedulerTriggerNormalizer();
21+
}
22+
23+
public static function normalizeProvider(): iterable
24+
{
25+
yield 'CallbackTrigger' => [new CallbackTrigger(fn () => null, 'test1'), 'test1'];
26+
yield 'PeriodicalTrigger' => [new PeriodicalTrigger(5), 'every 5 seconds'];
27+
}
28+
29+
/**
30+
* @dataProvider normalizeProvider
31+
*/
32+
public function testNormalize(mixed $data, mixed $expected)
33+
{
34+
self::assertSame($expected, $this->normalizer->normalize($data));
35+
}
36+
37+
public static function supportsNormalizationProvider(): iterable
38+
{
39+
yield 'CallbackTrigger, messenger context' => [new CallbackTrigger(fn () => null, 'test1'), ['messenger_serialization' => true], true];
40+
yield 'CallbackTrigger, normal context' => [new CallbackTrigger(fn () => null, 'test1'), [], false];
41+
yield 'PeriodicalTrigger, messenger context' => [new PeriodicalTrigger(5), ['messenger_serialization' => true], true];
42+
yield 'PeriodicalTrigger, normal context' => [new PeriodicalTrigger(5), [], false];
43+
yield 'stdClass, messenger context' => [new \stdClass(), ['messenger_serialization' => true], false];
44+
yield 'stdClass, normal context' => [new \stdClass(), [], false];
45+
}
46+
47+
/**
48+
* @dataProvider supportsNormalizationProvider
49+
*/
50+
public function testSupportsNormalization(mixed $data, array $context, bool $expected)
51+
{
52+
self::assertSame($expected, $this->normalizer->supportsNormalization($data, 'json', $context));
53+
}
54+
public static function supportsDenormalizationProvider(): iterable
55+
{
56+
yield 'unknown type' => ['test', \stdClass::class, ['messenger_serialization' => true], false];
57+
yield 'string, messenger context' => ['test', TriggerInterface::class, ['messenger_serialization' => true], true];
58+
yield 'string, normal context' => ['test', TriggerInterface::class, [], false];
59+
yield 'array, messenger context' => [['a' => 'b'], TriggerInterface::class, ['messenger_serialization' => true], false];
60+
yield 'array, normal context' => [['a' => 'b'], TriggerInterface::class, [], false];
61+
}
62+
63+
/**
64+
* @dataProvider supportsDenormalizationProvider
65+
*/
66+
public function testSupportsDenormalization(mixed $data, string $type, array $context, bool $expected)
67+
{
68+
self::assertSame($expected, $this->normalizer->supportsDenormalization($data, $type, 'json', $context));
69+
}
70+
71+
public function testDenormalize()
72+
{
73+
$trigger = $this->normalizer->denormalize('every 5 seconds', TriggerInterface::class);
74+
self::assertSame('every 5 seconds', (string) $trigger);
75+
}
76+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.