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

Skip to content

Commit d311645

Browse files
committed
bug #16445 [Serializer] Wrote test case for allowed attributes as objects or strings.
1 parent 8ca2677 commit d311645

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 <[email protected]>
20+
*/
21+
class AbstractNormalizerDummy extends AbstractNormalizer
22+
{
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
28+
{
29+
return parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function normalize($object, $format = null, array $context = array())
36+
{
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function supportsNormalization($data, $format = null)
43+
{
44+
return true;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function denormalize($data, $class, $format = null, array $context = array())
51+
{
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function supportsDenormalization($data, $type, $format = null)
58+
{
59+
return true;
60+
}
61+
}
Lines 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 <[email protected]>
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)