From 35f513d4f65a735b5c08e5ae615245965b03ab51 Mon Sep 17 00:00:00 2001 From: Beno!t POLASZEK Date: Wed, 13 Dec 2023 10:01:40 +0100 Subject: [PATCH 1/5] fix(serializer): properly normalize `DateTimeInterface` objects --- config/services.xml | 4 ++++ src/SearchableEntity.php | 1 + src/Services/UnixTimestampNormalizer.php | 23 +++++++++++++++++++++++ tests/Unit/SerializationTest.php | 8 ++------ 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 src/Services/UnixTimestampNormalizer.php diff --git a/config/services.xml b/config/services.xml index b9736346..07e1b5b4 100644 --- a/config/services.xml +++ b/config/services.xml @@ -66,5 +66,9 @@ + + + + diff --git a/src/SearchableEntity.php b/src/SearchableEntity.php index 6fd617a1..b29913ef 100644 --- a/src/SearchableEntity.php +++ b/src/SearchableEntity.php @@ -61,6 +61,7 @@ public function getIndexUid(): string public function getSearchableArray(): array { $context = [ + 'meilisearch' => true, 'fieldsMapping' => $this->entityMetadata->fieldMappings, ]; diff --git a/src/Services/UnixTimestampNormalizer.php b/src/Services/UnixTimestampNormalizer.php new file mode 100644 index 00000000..59ccc920 --- /dev/null +++ b/src/Services/UnixTimestampNormalizer.php @@ -0,0 +1,23 @@ +format('U'); + } + + public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool + { + return $data instanceof \DateTimeInterface && true === ($context['meilisearch'] ?? null); + } +} diff --git a/tests/Unit/SerializationTest.php b/tests/Unit/SerializationTest.php index f53aba5f..bd3ba547 100644 --- a/tests/Unit/SerializationTest.php +++ b/tests/Unit/SerializationTest.php @@ -18,10 +18,6 @@ class SerializationTest extends KernelTestCase public function testSimpleEntityToSearchableArray(): void { $datetime = new \DateTime(); - $dateNormalizer = static::getContainer()->get('serializer.normalizer.datetime'); - // This way we can test that DateTime's are serialized with DateTimeNormalizer - // And not the default ObjectNormalizer - $serializedDateTime = $dateNormalizer->normalize($datetime, Searchable::NORMALIZATION_FORMAT); $post = new Post( [ @@ -51,12 +47,12 @@ public function testSimpleEntityToSearchableArray(): void 'id' => 12, 'title' => 'a simple post', 'content' => 'some text', - 'publishedAt' => $serializedDateTime, + 'publishedAt' => (int) $datetime->format('U'), 'comments' => [ [ 'id' => null, 'content' => 'a great comment', - 'publishedAt' => $serializedDateTime, + 'publishedAt' => (int) $datetime->format('U'), ], ], ]; From 2e4139ef952f7c40a550a032e05916bf5bcab2c0 Mon Sep 17 00:00:00 2001 From: Beno!t POLASZEK Date: Wed, 13 Dec 2023 13:52:35 +0100 Subject: [PATCH 2/5] chore: apply review advices --- src/Services/UnixTimestampNormalizer.php | 2 +- tests/Unit/SerializationTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Services/UnixTimestampNormalizer.php b/src/Services/UnixTimestampNormalizer.php index 59ccc920..5a559968 100644 --- a/src/Services/UnixTimestampNormalizer.php +++ b/src/Services/UnixTimestampNormalizer.php @@ -13,7 +13,7 @@ final class UnixTimestampNormalizer implements NormalizerInterface */ public function normalize(mixed $object, string $format = null, array $context = []): int { - return (int) $object->format('U'); + return $object->getTimestamp(); } public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool diff --git a/tests/Unit/SerializationTest.php b/tests/Unit/SerializationTest.php index bd3ba547..93b614ce 100644 --- a/tests/Unit/SerializationTest.php +++ b/tests/Unit/SerializationTest.php @@ -47,12 +47,12 @@ public function testSimpleEntityToSearchableArray(): void 'id' => 12, 'title' => 'a simple post', 'content' => 'some text', - 'publishedAt' => (int) $datetime->format('U'), + 'publishedAt' => $datetime->getTimestamp(), 'comments' => [ [ 'id' => null, 'content' => 'a great comment', - 'publishedAt' => (int) $datetime->format('U'), + 'publishedAt' => $datetime->getTimestamp(), ], ], ]; From c40251f8ff9ba77552991c85718456ae04b7bd87 Mon Sep 17 00:00:00 2001 From: Beno!t POLASZEK Date: Wed, 13 Dec 2023 15:00:09 +0100 Subject: [PATCH 3/5] fix: PHP < 8.0 compatibility --- src/Services/UnixTimestampNormalizer.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Services/UnixTimestampNormalizer.php b/src/Services/UnixTimestampNormalizer.php index 5a559968..6d8cb64e 100644 --- a/src/Services/UnixTimestampNormalizer.php +++ b/src/Services/UnixTimestampNormalizer.php @@ -11,12 +11,15 @@ final class UnixTimestampNormalizer implements NormalizerInterface /** * @param \DateTimeInterface $object */ - public function normalize(mixed $object, string $format = null, array $context = []): int + public function normalize($object, string $format = null, array $context = []): int { return $object->getTimestamp(); } - public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool + /** + * @param mixed $data + */ + public function supportsNormalization($data, string $format = null, array $context = []): bool { return $data instanceof \DateTimeInterface && true === ($context['meilisearch'] ?? null); } From 074a14c3e6300631f962d6fb187cb11cc5a0672b Mon Sep 17 00:00:00 2001 From: Beno!t POLASZEK Date: Wed, 13 Dec 2023 15:14:51 +0100 Subject: [PATCH 4/5] tests: fix CommandsTest --- tests/Integration/CommandsTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/CommandsTest.php b/tests/Integration/CommandsTest.php index 6ad455ce..5278d2c1 100644 --- a/tests/Integration/CommandsTest.php +++ b/tests/Integration/CommandsTest.php @@ -440,13 +440,13 @@ public function testImportsDummyWithCustomGroups(): void 'objectID' => 1, 'id' => 1, 'name' => 'Dummy 1', - 'createdAt' => '2024-04-04T07:32:01+00:00', + 'createdAt' => 1712215921, ], [ 'objectID' => 2, 'id' => 2, 'name' => 'Dummy 2', - 'createdAt' => '2024-04-04T07:32:02+00:00', + 'createdAt' => 1712215922, ], ], $this->client->index('sf_phpunit__dummy_custom_groups')->getDocuments()->getResults()); } From 845efd52347520c175ee77ce442d3cc8a26baa21 Mon Sep 17 00:00:00 2001 From: Beno!t POLASZEK Date: Wed, 13 Dec 2023 15:21:25 +0100 Subject: [PATCH 5/5] tests: fix deprecation notice --- src/Services/UnixTimestampNormalizer.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Services/UnixTimestampNormalizer.php b/src/Services/UnixTimestampNormalizer.php index 6d8cb64e..43e29810 100644 --- a/src/Services/UnixTimestampNormalizer.php +++ b/src/Services/UnixTimestampNormalizer.php @@ -23,4 +23,11 @@ public function supportsNormalization($data, string $format = null, array $conte { return $data instanceof \DateTimeInterface && true === ($context['meilisearch'] ?? null); } + + public function getSupportedTypes(?string $format): array + { + return [ + \DateTimeInterface::class => true, // @codeCoverageIgnore + ]; + } }