From 82a021ba096999100d86ea0543965233d0c5fac7 Mon Sep 17 00:00:00 2001 From: David Barratt Date: Sun, 18 Mar 2018 10:14:36 -0400 Subject: [PATCH 1/2] Normalize the propertyPath so it matches the repsonse --- .../ConstraintViolationNormalizer.php | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Serializer/ConstraintViolationNormalizer.php b/src/Serializer/ConstraintViolationNormalizer.php index 6670ed2..05bb254 100644 --- a/src/Serializer/ConstraintViolationNormalizer.php +++ b/src/Serializer/ConstraintViolationNormalizer.php @@ -2,24 +2,50 @@ namespace GeoSocio\HttpSerializer\Serializer; +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Validator\ConstraintViolationInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; /** * Exception Normalizer */ -class ConstraintViolationNormalizer implements NormalizerInterface +class ConstraintViolationNormalizer implements NormalizerInterface, NormalizerAwareInterface { + use NormalizerAwareTrait; + /** * {@inheritdoc} */ public function normalize($object, $format = null, array $context = array()) { - return [ + + // Convert the peroperty paths so they match the serialization. + $propertyPath = $object->getPropertyPath(); + if ($this->normalizer && $this->normalizer->nameConverter instanceof NameConverterInterface) { + $properties = explode('.', $propertyPath); + $properties = array_map(function ($property) { + return $this->normalizer->nameConverter->normalize($property); + }, $properties); + $propertyPath = implode('.', $properties); + } + + $data = [ 'message' => $object->getMessage(), - 'propertyPath' => $object->getPropertyPath(), + 'propertyPath' => $propertyPath, 'code' => $object->getCode(), ]; + + if ($this->normalizer && $this->normalizer->nameConverter instanceof NameConverterInterface) { + foreach ($data as $key => $value) { + $normalized[$this->normalizer->nameConverter->normalize($key)] = $value; + } + } else { + $normalized = $data; + } + + return $normalized; } /** From 247b28a8935b63f476b479f95d787e0231232121 Mon Sep 17 00:00:00 2001 From: David Barratt Date: Sun, 18 Mar 2018 10:39:52 -0400 Subject: [PATCH 2/2] Add test with name converter --- .../ConstraintViolationNormalizer.php | 27 +++++++++---- .../ConstraintViolationNormalizerTest.php | 39 ++++++++++++++++++- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/Serializer/ConstraintViolationNormalizer.php b/src/Serializer/ConstraintViolationNormalizer.php index 05bb254..dc96f93 100644 --- a/src/Serializer/ConstraintViolationNormalizer.php +++ b/src/Serializer/ConstraintViolationNormalizer.php @@ -5,15 +5,26 @@ use Symfony\Component\Serializer\NameConverter\NameConverterInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Validator\ConstraintViolationInterface; -use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; -use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; /** * Exception Normalizer */ -class ConstraintViolationNormalizer implements NormalizerInterface, NormalizerAwareInterface +class ConstraintViolationNormalizer implements NormalizerInterface { - use NormalizerAwareTrait; + /** + * @var NameConverterInterface|null + */ + protected $nameConverter; + + /** + * Constraint Violoation Normalizer + * + * @param NameConverterInterface|null $nameConverter + */ + public function __construct(?NameConverterInterface $nameConverter = null) + { + $this->nameConverter = $nameConverter; + } /** * {@inheritdoc} @@ -23,10 +34,10 @@ public function normalize($object, $format = null, array $context = array()) // Convert the peroperty paths so they match the serialization. $propertyPath = $object->getPropertyPath(); - if ($this->normalizer && $this->normalizer->nameConverter instanceof NameConverterInterface) { + if ($this->nameConverter) { $properties = explode('.', $propertyPath); $properties = array_map(function ($property) { - return $this->normalizer->nameConverter->normalize($property); + return $this->nameConverter->normalize($property); }, $properties); $propertyPath = implode('.', $properties); } @@ -37,9 +48,9 @@ public function normalize($object, $format = null, array $context = array()) 'code' => $object->getCode(), ]; - if ($this->normalizer && $this->normalizer->nameConverter instanceof NameConverterInterface) { + if ($this->nameConverter) { foreach ($data as $key => $value) { - $normalized[$this->normalizer->nameConverter->normalize($key)] = $value; + $normalized[$this->nameConverter->normalize($key)] = $value; } } else { $normalized = $data; diff --git a/tests/Serializer/ConstraintViolationNormalizerTest.php b/tests/Serializer/ConstraintViolationNormalizerTest.php index b9ba705..7da1bfa 100644 --- a/tests/Serializer/ConstraintViolationNormalizerTest.php +++ b/tests/Serializer/ConstraintViolationNormalizerTest.php @@ -3,6 +3,7 @@ namespace GeoSocio\HttpSerializer\Serializer; use PHPUnit\Framework\TestCase; +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; use Symfony\Component\Validator\ConstraintViolationInterface; /** @@ -11,7 +12,7 @@ class ConstraintViolationNormalizerTest extends TestCase { /** - * Test Supports Normalization. + * Test Normalization without Serializer. */ public function testNormalize() { @@ -40,6 +41,42 @@ public function testNormalize() $this->assertEquals($code, $result['code']); } + /** + * Test Normalization with Serializer. + */ + public function testNormalizeWithNameConvertor() + { + $message = 'Test Message'; + $propertyPath = 'id'; + $code = 123; + + $nameConverter = $this->createMock(NameConverterInterface::class); + $nameConverter->method('normalize') + ->willReturnArgument(0); + + $normalizer = new ConstraintViolationNormalizer($nameConverter); + + $violation = $this->createMock(ConstraintViolationInterface::class); + $violation->expects($this->once()) + ->method('getMessage') + ->willReturn($message); + $violation->expects($this->once()) + ->method('getPropertyPath') + ->willReturn($propertyPath); + $violation->expects($this->once()) + ->method('getCode') + ->willReturn($code); + + $result = $normalizer->normalize($violation); + + $this->assertArrayHasKey('message', $result); + $this->assertEquals($message, $result['message']); + $this->assertArrayHasKey('propertyPath', $result); + $this->assertEquals($propertyPath, $result['propertyPath']); + $this->assertArrayHasKey('code', $result); + $this->assertEquals($code, $result['code']); + } + /** * Test Supports Normalization. */