Skip to content

[Scheduler] Normalize TriggerInterface as string #59679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Scheduler\EventListener\DispatchSchedulerEventListener;
use Symfony\Component\Scheduler\Messenger\SchedulerTransportFactory;
use Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer;
use Symfony\Component\Scheduler\Messenger\ServiceCallMessageHandler;

return static function (ContainerConfigurator $container) {
Expand All @@ -34,5 +35,7 @@
service('event_dispatcher'),
])
->tag('kernel.event_subscriber')
->set('serializer.normalizer.scheduler_trigger', SchedulerTriggerNormalizer::class)
->tag('serializer.normalizer', ['built_in' => true, 'priority' => -880])
;
};
5 changes: 5 additions & 0 deletions src/Symfony/Component/Scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `TriggerNormalizer`

7.2
---

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 <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Scheduler\Messenger\Serializer\Normalizer;

use Symfony\Component\Messenger\Transport\Serialization\Serializer;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class SchedulerTriggerNormalizer implements DenormalizerInterface, NormalizerInterface
{
public function getSupportedTypes(?string $format): array

Check failure on line 21 in src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

LessSpecificImplementedReturnType

src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php:21:57: LessSpecificImplementedReturnType: The inherited return type 'array<string, bool|null>' for Symfony\Component\Serializer\Normalizer\DenormalizerInterface::getSupportedTypes is more specific than the implemented return type for Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer::getsupportedtypes 'array<array-key, mixed>' (see https://psalm.dev/166)

Check failure on line 21 in src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

LessSpecificImplementedReturnType

src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php:21:57: LessSpecificImplementedReturnType: The inherited return type 'array<string, bool|null>' for Symfony\Component\Serializer\Normalizer\NormalizerInterface::getSupportedTypes is more specific than the implemented return type for Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer::getsupportedtypes 'array<array-key, mixed>' (see https://psalm.dev/166)

Check failure on line 21 in src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

LessSpecificImplementedReturnType

src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php:21:57: LessSpecificImplementedReturnType: The inherited return type 'array<string, bool|null>' for Symfony\Component\Serializer\Normalizer\DenormalizerInterface::getSupportedTypes is more specific than the implemented return type for Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer::getsupportedtypes 'array<array-key, mixed>' (see https://psalm.dev/166)

Check failure on line 21 in src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

LessSpecificImplementedReturnType

src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php:21:57: LessSpecificImplementedReturnType: The inherited return type 'array<string, bool|null>' for Symfony\Component\Serializer\Normalizer\NormalizerInterface::getSupportedTypes is more specific than the implemented return type for Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer::getsupportedtypes 'array<array-key, mixed>' (see https://psalm.dev/166)
{
return [
TriggerInterface::class => false,
];
}

/**
* @param TriggerInterface $data
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): string

Check failure on line 31 in src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificImplementedParamType

src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php:31:37: MoreSpecificImplementedParamType: Argument 1 of Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer::normalize has the more specific type 'Symfony\Component\Scheduler\Trigger\TriggerInterface', expecting 'mixed' as defined by Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize (see https://psalm.dev/140)

Check failure on line 31 in src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificImplementedParamType

src/Symfony/Component/Scheduler/Messenger/Serializer/Normalizer/SchedulerTriggerNormalizer.php:31:37: MoreSpecificImplementedParamType: Argument 1 of Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer::normalize has the more specific type 'Symfony\Component\Scheduler\Trigger\TriggerInterface', expecting 'mixed' as defined by Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize (see https://psalm.dev/140)
{
return (string) $data;
}

public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof TriggerInterface && ($context[Serializer::MESSENGER_SERIALIZATION_CONTEXT] ?? false);
}

public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): TriggerInterface
{
return new class($data) implements TriggerInterface {
public function __construct(private readonly string $description)
{
}

public function __toString(): string
{
return $this->description;
}

public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
{
throw new \LogicException('Not possible to get next run date from a deserialized trigger.');
}
};
}

public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
{
return TriggerInterface::class === $type && ($context[Serializer::MESSENGER_SERIALIZATION_CONTEXT] ?? false) && \is_string($data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

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

namespace Symfony\Component\Scheduler\Tests\Messenger;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer;
use Symfony\Component\Scheduler\Trigger\CallbackTrigger;
use Symfony\Component\Scheduler\Trigger\PeriodicalTrigger;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;

class SchedulerTriggerNormalizerTest extends TestCase
{
private SchedulerTriggerNormalizer $normalizer;

/**
* @before
*/
protected function setUpNormalizer(): void
{
$this->normalizer = new SchedulerTriggerNormalizer();
}

/**
* @dataProvider normalizeProvider
*/
public function testNormalize(mixed $data, mixed $expected)
{
self::assertSame($expected, $this->normalizer->normalize($data));
}

public static function normalizeProvider(): iterable
{
yield 'CallbackTrigger' => [new CallbackTrigger(fn () => null, 'test1'), 'test1'];
yield 'PeriodicalTrigger' => [new PeriodicalTrigger(5), 'every 5 seconds'];
}

/**
* @dataProvider supportsNormalizationProvider
*/
public function testSupportsNormalization(mixed $data, array $context, bool $expected)
{
self::assertSame($expected, $this->normalizer->supportsNormalization($data, 'json', $context));
}

public static function supportsNormalizationProvider(): iterable
{
yield 'CallbackTrigger, messenger context' => [new CallbackTrigger(fn () => null, 'test1'), ['messenger_serialization' => true], true];
yield 'CallbackTrigger, normal context' => [new CallbackTrigger(fn () => null, 'test1'), [], false];
yield 'PeriodicalTrigger, messenger context' => [new PeriodicalTrigger(5), ['messenger_serialization' => true], true];
yield 'PeriodicalTrigger, normal context' => [new PeriodicalTrigger(5), [], false];
yield 'stdClass, messenger context' => [new \stdClass(), ['messenger_serialization' => true], false];
yield 'stdClass, normal context' => [new \stdClass(), [], false];
}

/**
* @dataProvider supportsDenormalizationProvider
*/
public function testSupportsDenormalization(mixed $data, string $type, array $context, bool $expected)
{
self::assertSame($expected, $this->normalizer->supportsDenormalization($data, $type, 'json', $context));
}

public static function supportsDenormalizationProvider(): iterable
{
yield 'unknown type' => ['test', \stdClass::class, ['messenger_serialization' => true], false];
yield 'string, messenger context' => ['test', TriggerInterface::class, ['messenger_serialization' => true], true];
yield 'string, normal context' => ['test', TriggerInterface::class, [], false];
yield 'array, messenger context' => [['a' => 'b'], TriggerInterface::class, ['messenger_serialization' => true], false];
yield 'array, normal context' => [['a' => 'b'], TriggerInterface::class, [], false];
}

public function testDenormalize()
{
$trigger = $this->normalizer->denormalize('every 5 seconds', TriggerInterface::class);
self::assertSame('every 5 seconds', (string) $trigger);
}
}
Loading