This PR was merged into the 3.1 branch.
Discussion
----------
[#18838] add a test to avoid regressions
| Q | A
| ------------- | ---
| Branch? | 3.1
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #18838
| License | MIT
| Doc PR |
Commits
-------
240cf0a
[#18838] add a test to avoid regressions
This commit is contained in:
commit
691f19ff34
@ -545,6 +545,28 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$serializer->denormalize(array('date' => 'foo'), ObjectOuter::class);
|
$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
|
class ObjectDummy
|
||||||
@ -744,3 +766,19 @@ class ObjectInner
|
|||||||
public $foo;
|
public $foo;
|
||||||
public $bar;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user