From 4fd5a37df382df3017de292372989855dbe839d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 11 Dec 2023 16:09:26 +0100 Subject: [PATCH] Add serializer groups to SignedUrl and SignedUrlPost classes, include tests Serializer groups have been added to the SignedUrl and SignedUrlPost classes to ensure correct serialization. Two new test files were also included: SignedUrlNormalizerTest and SignedUrlPostNormalizerTest which test the normalization of instances of the classes. --- .../AsyncUpload/SignedUrl.php | 17 +++++ .../AsyncUpload/SignedUrlPost.php | 26 ++++++++ .../Normalizer/SignedUrlNormalizerTest.php | 55 ++++++++++++++++ .../SignedUrlPostNormalizerTest.php | 66 +++++++++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 src/Bundle/ChillDocStoreBundle/Tests/Serializer/Normalizer/SignedUrlNormalizerTest.php create mode 100644 src/Bundle/ChillDocStoreBundle/Tests/Serializer/Normalizer/SignedUrlPostNormalizerTest.php diff --git a/src/Bundle/ChillDocStoreBundle/AsyncUpload/SignedUrl.php b/src/Bundle/ChillDocStoreBundle/AsyncUpload/SignedUrl.php index 583325a26..9003a69ea 100644 --- a/src/Bundle/ChillDocStoreBundle/AsyncUpload/SignedUrl.php +++ b/src/Bundle/ChillDocStoreBundle/AsyncUpload/SignedUrl.php @@ -11,11 +11,28 @@ declare(strict_types=1); namespace Chill\DocStoreBundle\AsyncUpload; +use Symfony\Component\Serializer\Annotation as Serializer; + readonly class SignedUrl { public function __construct( + /** + * @Serializer\Groups({"read"}) + */ public string $method, + + /** + * @Serializer\Groups({"read"}) + */ public string $url, public \DateTimeImmutable $expires, ) {} + + /** + * @Serializer\Groups({"read"}) + */ + public function getExpires(): int + { + return $this->expires->getTimestamp(); + } } diff --git a/src/Bundle/ChillDocStoreBundle/AsyncUpload/SignedUrlPost.php b/src/Bundle/ChillDocStoreBundle/AsyncUpload/SignedUrlPost.php index 98e273635..4a5b4e0fb 100644 --- a/src/Bundle/ChillDocStoreBundle/AsyncUpload/SignedUrlPost.php +++ b/src/Bundle/ChillDocStoreBundle/AsyncUpload/SignedUrlPost.php @@ -11,16 +11,42 @@ declare(strict_types=1); namespace Chill\DocStoreBundle\AsyncUpload; +use Symfony\Component\Serializer\Annotation as Serializer; + readonly class SignedUrlPost extends SignedUrl { public function __construct( string $url, \DateTimeImmutable $expires, + + /** + * @Serializer\Groups({"read"}) + */ public int $max_file_size, + + /** + * @Serializer\Groups({"read"}) + */ public int $max_file_count, + + /** + * @Serializer\Groups({"read"}) + */ public int $submit_delay, + + /** + * @Serializer\Groups({"read"}) + */ public string $redirect, + + /** + * @Serializer\Groups({"read"}) + */ public string $prefix, + + /** + * @Serializer\Groups({"read"}) + */ public string $signature, ) { parent::__construct('POST', $url, $expires); diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Serializer/Normalizer/SignedUrlNormalizerTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Serializer/Normalizer/SignedUrlNormalizerTest.php new file mode 100644 index 000000000..8cf9c5a6d --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/Serializer/Normalizer/SignedUrlNormalizerTest.php @@ -0,0 +1,55 @@ +get(NormalizerInterface::class); + } + + public function testNormalizerSignedUrl(): void + { + $signedUrl = new SignedUrl( + 'GET', + 'https://object.store.example/container/object', + \DateTimeImmutable::createFromFormat('U', '1700000') + ); + + $actual = self::$normalizer->normalize($signedUrl, 'json', [AbstractNormalizer::GROUPS => ['read']]); + + self::assertEqualsCanonicalizing( + [ + 'method' => 'GET', + 'expires' => 1_700_000, + 'url' => 'https://object.store.example/container/object', + ], + $actual + ); + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Serializer/Normalizer/SignedUrlPostNormalizerTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Serializer/Normalizer/SignedUrlPostNormalizerTest.php new file mode 100644 index 000000000..3d510d22b --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/Serializer/Normalizer/SignedUrlPostNormalizerTest.php @@ -0,0 +1,66 @@ +get(NormalizerInterface::class); + } + + public function testNormalizerSignedUrl(): void + { + $signedUrl = new SignedUrlPost( + 'https://object.store.example/container/object', + \DateTimeImmutable::createFromFormat('U', '1700000'), + 15000, + 1, + 180, + '', + 'abc', + 'SiGnaTure' + ); + + $actual = self::$normalizer->normalize($signedUrl, 'json', [AbstractNormalizer::GROUPS => ['read']]); + + self::assertEqualsCanonicalizing( + [ + 'max_file_size' => 15000, + 'max_file_count' => 1, + 'submit_delay' => 180, + 'redirect' => '', + 'prefix' => 'abc', + 'signature' => 'SiGnaTure', + 'method' => 'POST', + 'expires' => 1_700_000, + 'url' => 'https://object.store.example/container/object', + ], + $actual + ); + } +}