8000 [#18838] add a test to avoid regressions · symfony/symfony@240cf0a · GitHub
[go: up one dir, main page]

Skip to content

Commit 240cf0a

Browse files
committed
[#18838] add a test to avoid regressions
1 parent 8c65c0e commit 240cf0a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,28 @@ public function testRejectInvalidType()
545545

546546
$serializer->denormalize(array('date' => 'foo'), ObjectOuter::class);
547547
}
548+
549+
public function testExtractAttributesRespectsFormat()
550+
{
551+
$normalizer = new FormatAndContextAwareNormalizer();
552+
553+
$data = new ObjectDummy();
554+
$data->setFoo('bar');
555+
$data->bar = 'foo';
556+
557+
$this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, 'foo_and_bar_included'));
558+
}
559+
560+
public function testExtractAttributesRespectsContext()
561+
{
562+
$normalizer = new FormatAndContextAwareNormalizer();
563+
564+
$data = new ObjectDummy();
565+
$data->setFoo('bar');
566+
$data->bar = 'foo';
567+
568+
$this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, null, array('include_foo_and_bar' => true)));
569+
}
548570
}
549571

550572
class ObjectDummy
@@ -744,3 +766,19 @@ class ObjectInner
744766
public $foo;
745767
public $bar;
746768
}
769+
770+
class FormatAndContextAwareNormalizer extends ObjectNormalizer
771+
{
772+
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
773+
{
774+
if (in_array($attribute, array('foo', 'bar')) && 'foo_and_bar_included' === $format) {
775+
return true;
776+
}
777+
778+
if (in_array($attribute, array('foo', 'bar')) && isset($context['include_foo_and_bar']) && true === $context['include_foo_and_bar']) {
779+
return true;
780+
}
781+
782+
return false;
783+
}
784+
}

0 commit comments

Comments
 (0)
0