minor #18914 [#18838] add a test to avoid regressions (xabbuh)

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:
Christophe Coevoet 2016-05-30 10:42:44 +02:00
commit 691f19ff34
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;
}
}