Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 98a4fa0

Browse filesBrowse files
committed
bug #16445 [Serializer] Wrote test case for allowed attributes as objects or strings.
1 parent 0b96b43 commit 98a4fa0
Copy full SHA for 98a4fa0

File tree

2 files changed

+151
-0
lines changed
Filter options

2 files changed

+151
-0
lines changed
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
15+
16+
/**
17+
* Provides a dummy Normalizer which extends the AbstractNormalizer.
18+
*
19+
* @author Konstantin S. M. Möllers <ksm.moellers@gmail.com>
20+
*/
21+
class AbstractNormalizerDummy extends AbstractNormalizer
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
27+
{
28+
return parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function normalize($object, $format = null, array $context = array())
35+
{
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function supportsNormalization($data, $format = null)
42+
{
43+
return true;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function denormalize($data, $class, $format = null, array $context = array())
50+
{
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function supportsDenormalization($data, $type, $format = null)
57+
{
58+
return true;
59+
}
60+
}
+91Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Normalizer;
4+
5+
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
6+
use Symfony\Component\Serializer\Mapping\ClassMetadata;
7+
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
8+
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
9+
10+
/**
11+
* Provides a dummy Normalizer which extends the AbstractNormalizer.
12+
*
13+
* @author Konstantin S. M. Möllers <ksm.moellers@gmail.com>
14+
*/
15+
class AbstractNormalizerTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var AbstractNormalizerDummy
19+
*/
20+
private $normalizer;
21+
22+
/**
23+
* @var ClassMetadataFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $classMetadata;
26+
27+
protected function setUp()
28+
{
29+
$loader = $this->getMock('Symfony\Component\Serializer\Mapping\Loader\LoaderChain', [], [[]]);
30+
$this->classMetadata = $this->getMock('Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory', [], [$loader]);
31+
$this->normalizer = new AbstractNormalizerDummy($this->classMetadata);
32+
}
33+
34+
public function testGetAllowedAttributesAsString()
35+
{
36+
$classMetadata = new ClassMetadata('c');
37+
38+
$a1 = new AttributeMetadata('a1');
39+
$classMetadata->addAttributeMetadata($a1);
40+
41+
$a2 = new AttributeMetadata('a2');
42+
$a2->addGroup('test');
43+
$classMetadata->addAttributeMetadata($a2);
44+
45+
$a3 = new AttributeMetadata('a3');
46+
$a3->addGroup('other');
47+
$classMetadata->addAttributeMetadata($a3);
48+
49+
$a4 = new AttributeMetadata('a4');
50+
$a4->addGroup('test');
51+
$a4->addGroup('other');
52+
$classMetadata->addAttributeMetadata($a4);
53+
54+
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
55+
56+
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['test']], true);
57+
$this->assertEquals(['a2', 'a4'], $result);
58+
59+
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['other']], true);
60+
$this->assertEquals(['a3', 'a4'], $result);
61+
}
62+
63+
public function testGetAllowedAttributesAsObjects()
64+
{
65+
$classMetadata = new ClassMetadata('c');
66+
67+
$a1 = new AttributeMetadata('a1');
68+
$classMetadata->addAttributeMetadata($a1);
69+
70+
$a2 = new AttributeMetadata('a2');
71+
$a2->addGroup('test');
72+
$classMetadata->addAttributeMetadata($a2);
73+
74+
$a3 = new AttributeMetadata('a3');
75+
$a3->addGroup('other');
76+
$classMetadata->addAttributeMetadata($a3);
77+
78+
$a4 = new AttributeMetadata('a4');
79+
$a4->addGroup('test');
80+
$a4->addGroup('other');
81+
$classMetadata->addAttributeMetadata($a4);
82+
83+
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
84+
85+
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['test']], false);
86+
$this->assertEquals([$a2, $a4], $result);
87+
88+
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['other']], false);
89+
$this->assertEquals([$a3, $a4], $result);
90+
}
91+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.