[#18838] add a test to avoid regressions

This commit is contained in:
Christian Flothmann 2016-05-30 10:20:49 +02:00
parent 8c65c0e6ec
commit 240cf0aaa8
1 changed files with 38 additions and 0 deletions

View File

@ -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;
}
}