|
| 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