From 240cf0aaa8d8bbb0180ed1c762709741cb1a65b9 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 30 May 2016 10:20:49 +0200 Subject: [PATCH] [#18838] add a test to avoid regressions --- .../Tests/Normalizer/ObjectNormalizerTest.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 5d91a9fd1e..8a09e516cd 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -545,6 +545,28 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase $serializer->denormalize(array('date' => 'foo'), ObjectOuter::class); } + + public function testExtractAttributesRespectsFormat() + { + $normalizer = new FormatAndContextAwareNormalizer(); + + $data = new ObjectDummy(); + $data->setFoo('bar'); + $data->bar = 'foo'; + + $this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, 'foo_and_bar_included')); + } + + public function testExtractAttributesRespectsContext() + { + $normalizer = new FormatAndContextAwareNormalizer(); + + $data = new ObjectDummy(); + $data->setFoo('bar'); + $data->bar = 'foo'; + + $this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, null, array('include_foo_and_bar' => true))); + } } class ObjectDummy @@ -744,3 +766,19 @@ class ObjectInner public $foo; public $bar; } + +class FormatAndContextAwareNormalizer extends ObjectNormalizer +{ + protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array()) + { + if (in_array($attribute, array('foo', 'bar')) && 'foo_and_bar_included' === $format) { + return true; + } + + if (in_array($attribute, array('foo', 'bar')) && isset($context['include_foo_and_bar']) && true === $context['include_foo_and_bar']) { + return true; + } + + return false; + } +}