From c97dba58c2fb36dee0381da84952cad21c49b6fb Mon Sep 17 00:00:00 2001 From: HypeMC Date: Thu, 27 Jun 2024 18:09:31 +0200 Subject: [PATCH 1/4] [Serializer] Check if exception message in test is correct --- Tests/Fixtures/NotNormalizableDummy.php | 2 +- Tests/Normalizer/AbstractObjectNormalizerTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/Fixtures/NotNormalizableDummy.php b/Tests/Fixtures/NotNormalizableDummy.php index 86ee6dbe..d146c1a8 100644 --- a/Tests/Fixtures/NotNormalizableDummy.php +++ b/Tests/Fixtures/NotNormalizableDummy.php @@ -26,6 +26,6 @@ public function __construct() public function denormalize(DenormalizerInterface $denormalizer, $data, ?string $format = null, array $context = []) { - throw new NotNormalizableValueException(); + throw new NotNormalizableValueException('Custom exception message'); } } diff --git a/Tests/Normalizer/AbstractObjectNormalizerTest.php b/Tests/Normalizer/AbstractObjectNormalizerTest.php index afaf57ea..b700f6ee 100644 --- a/Tests/Normalizer/AbstractObjectNormalizerTest.php +++ b/Tests/Normalizer/AbstractObjectNormalizerTest.php @@ -514,6 +514,7 @@ public function testDenormalizeUntypedFormat() public function testDenormalizeUntypedFormatNotNormalizable() { $this->expectException(NotNormalizableValueException::class); + $this->expectExceptionMessage('Custom exception message'); $serializer = new Serializer([new CustomNormalizer(), new ObjectNormalizer(null, null, null, new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]))]); $serializer->denormalize(['value' => 'test'], DummyWithNotNormalizable::class, 'xml'); } From 3bfc744bd038f16bd9d976c7d3af64e12d14288f Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 4 Jul 2024 11:36:30 +0200 Subject: [PATCH 2/4] [Serializer] Raise correct exception in ArrayDenormalizer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … when it's called without a nested denormalizer --- Normalizer/ArrayDenormalizer.php | 4 ++-- Tests/Normalizer/ArrayDenormalizerTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Normalizer/ArrayDenormalizer.php b/Normalizer/ArrayDenormalizer.php index a9c64a1e..a52c0de4 100644 --- a/Normalizer/ArrayDenormalizer.php +++ b/Normalizer/ArrayDenormalizer.php @@ -42,7 +42,7 @@ public function getSupportedTypes(?string $format): array */ public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): array { - if (null === $this->denormalizer) { + if (!isset($this->denormalizer)) { throw new BadMethodCallException('Please set a denormalizer before calling denormalize()!'); } if (!\is_array($data)) { @@ -72,7 +72,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool { - if (null === $this->denormalizer) { + if (!isset($this->denormalizer)) { throw new BadMethodCallException(sprintf('The nested denormalizer needs to be set to allow "%s()" to be used.', __METHOD__)); } diff --git a/Tests/Normalizer/ArrayDenormalizerTest.php b/Tests/Normalizer/ArrayDenormalizerTest.php index be4a30d8..b60f57ba 100644 --- a/Tests/Normalizer/ArrayDenormalizerTest.php +++ b/Tests/Normalizer/ArrayDenormalizerTest.php @@ -108,6 +108,22 @@ public function testSupportsNoArray() ) ); } + + public function testDenormalizeWithoutDenormalizer() + { + $arrayDenormalizer = new ArrayDenormalizer(); + + $this->expectException(\BadMethodCallException::class); + $arrayDenormalizer->denormalize([], 'string[]'); + } + + public function testSupportsDenormalizationWithoutDenormalizer() + { + $arrayDenormalizer = new ArrayDenormalizer(); + + $this->expectException(\BadMethodCallException::class); + $arrayDenormalizer->supportsDenormalization([], 'string[]'); + } } class ArrayDummy From 1e538a66f85f8f56d9b77726c94c028a8b4fa66f Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 9 Jul 2024 22:57:15 +0200 Subject: [PATCH 3/4] use more entropy with uniqid() --- Tests/Mapping/Factory/ClassMetadataFactoryCompilerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Mapping/Factory/ClassMetadataFactoryCompilerTest.php b/Tests/Mapping/Factory/ClassMetadataFactoryCompilerTest.php index 6d562e30..43e764ab 100644 --- a/Tests/Mapping/Factory/ClassMetadataFactoryCompilerTest.php +++ b/Tests/Mapping/Factory/ClassMetadataFactoryCompilerTest.php @@ -29,7 +29,7 @@ final class ClassMetadataFactoryCompilerTest extends TestCase protected function setUp(): void { - $this->dumpPath = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'php_serializer_metadata.'.uniqid('CompiledClassMetadataFactory').'.php'; + $this->dumpPath = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'php_serializer_metadata.'.uniqid('CompiledClassMetadataFactory', true).'.php'; } protected function tearDown(): void From 430a73df11875ec5aead1c147d4ad9ef2493acd5 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 15 Jul 2024 11:35:34 +0200 Subject: [PATCH 4/4] use more entropy with uniqid() --- Debug/TraceableSerializer.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Debug/TraceableSerializer.php b/Debug/TraceableSerializer.php index 2e375e08..789ae65c 100644 --- a/Debug/TraceableSerializer.php +++ b/Debug/TraceableSerializer.php @@ -41,7 +41,7 @@ public function __construct( public function serialize(mixed $data, string $format, array $context = []): string { - $context[self::DEBUG_TRACE_ID] = $traceId = uniqid(); + $context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true); $startTime = microtime(true); $result = $this->serializer->serialize($data, $format, $context); @@ -56,7 +56,7 @@ public function serialize(mixed $data, string $format, array $context = []): str public function deserialize(mixed $data, string $type, string $format, array $context = []): mixed { - $context[self::DEBUG_TRACE_ID] = $traceId = uniqid(); + $context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true); $startTime = microtime(true); $result = $this->serializer->deserialize($data, $type, $format, $context); @@ -71,7 +71,7 @@ public function deserialize(mixed $data, string $type, string $format, array $co public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { - $context[self::DEBUG_TRACE_ID] = $traceId = uniqid(); + $context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true); $startTime = microtime(true); $result = $this->serializer->normalize($object, $format, $context); @@ -86,7 +86,7 @@ public function normalize(mixed $object, ?string $format = null, array $context public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed { - $context[self::DEBUG_TRACE_ID] = $traceId = uniqid(); + $context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true); $startTime = microtime(true); $result = $this->serializer->denormalize($data, $type, $format, $context); @@ -101,7 +101,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a public function encode(mixed $data, string $format, array $context = []): string { - $context[self::DEBUG_TRACE_ID] = $traceId = uniqid(); + $context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true); $startTime = microtime(true); $result = $this->serializer->encode($data, $format, $context); @@ -116,7 +116,7 @@ public function encode(mixed $data, string $format, array $context = []): string public function decode(string $data, string $format, array $context = []): mixed { - $context[self::DEBUG_TRACE_ID] = $traceId = uniqid(); + $context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true); $startTime = microtime(true); $result = $this->serializer->decode($data, $format, $context);