From 72045ce0826be06c7616ff5b7545cc43dce804e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 12 Jun 2024 11:46:49 +0200 Subject: [PATCH] Add DocGenNormalizerTestAbstract class A new class, DocGenNormalizerTestAbstract, was added to the ChillDocGeneratorBundle. This abstract class tests the normalization of null values and ensures they comply with expected formats and behaviors. It implements key methods that allow for providing non-null objects, expectation setting, and normalization. --- .../Test/DocGenNormalizerTestAbstract.php | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Test/DocGenNormalizerTestAbstract.php diff --git a/src/Bundle/ChillDocGeneratorBundle/Test/DocGenNormalizerTestAbstract.php b/src/Bundle/ChillDocGeneratorBundle/Test/DocGenNormalizerTestAbstract.php new file mode 100644 index 000000000..2839be3f5 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Test/DocGenNormalizerTestAbstract.php @@ -0,0 +1,65 @@ +getNormalizer()->normalize($this->provideNotNullObject(), 'docgen', [ + AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => $this->provideDocGenExpectClass(), + ]); + $nullNormalizedObject = $this->getNormalizer()->normalize(null, 'docgen', [ + AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => $this->provideDocGenExpectClass(), + ]); + + self::assertEqualsCanonicalizing(array_keys($normalizedObject), array_keys($nullNormalizedObject)); + self::assertArrayHasKey('isNull', $nullNormalizedObject, 'each object must have an "isNull" key'); + self::assertTrue($nullNormalizedObject['isNull'], 'isNull key must be true for null objects'); + self::assertFalse($normalizedObject['isNull'], 'isNull key must be false for null objects'); + + foreach ($normalizedObject as $key => $value) { + if (in_array($key, ['isNull', 'type'])) { + continue; + } + + if (is_array($value)) { + if (array_is_list($value)) { + self::assertEquals([], $nullNormalizedObject[$key], "list must be serialized as an empty array, in {$key}"); + } else { + self::assertEqualsCanonicalizing(array_keys($value), array_keys($nullNormalizedObject[$key]), "sub-object must have the same keys, in {$key}"); + } + } elseif (is_string($value)) { + self::assertEquals('', $nullNormalizedObject[$key], 'strings must be '); + } + } + } + + /** + * @return T + */ + abstract public function provideNotNullObject(): object; + + /** + * @return class-string + */ + abstract public function provideDocGenExpectClass(): string; + + abstract public function getNormalizer(): NormalizerInterface; +}