Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[#18838] add a test to avoid regressions #18914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,28 @@ public function testRejectInvalidType()

$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
Expand Down Expand Up @@ -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;
}
}