4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Tests\Mapping;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Serializer\Mapping\ClassMetadata;
18 * @author Kévin Dunglas <dunglas@gmail.com>
20 class ClassMetadataTest extends TestCase
22 public function testInterface()
24 $classMetadata = new ClassMetadata('name');
25 $this->assertInstanceOf('Symfony\Component\Serializer\Mapping\ClassMetadataInterface', $classMetadata);
28 public function testAttributeMetadata()
30 $classMetadata = new ClassMetadata('c');
32 $a1 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
33 $a1->method('getName')->willReturn('a1');
35 $a2 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
36 $a2->method('getName')->willReturn('a2');
38 $classMetadata->addAttributeMetadata($a1);
39 $classMetadata->addAttributeMetadata($a2);
41 $this->assertEquals(array('a1' => $a1, 'a2' => $a2), $classMetadata->getAttributesMetadata());
44 public function testMerge()
46 $classMetadata1 = new ClassMetadata('c1');
47 $classMetadata2 = new ClassMetadata('c2');
49 $ac1 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
50 $ac1->method('getName')->willReturn('a1');
51 $ac1->method('getGroups')->willReturn(array('a', 'b'));
53 $ac2 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
54 $ac2->method('getName')->willReturn('a1');
55 $ac2->method('getGroups')->willReturn(array('b', 'c'));
57 $classMetadata1->addAttributeMetadata($ac1);
58 $classMetadata2->addAttributeMetadata($ac2);
60 $classMetadata1->merge($classMetadata2);
62 $ac1->method('getGroups')->willReturn('a', 'b', 'c');
64 $this->assertEquals(array('a1' => $ac1), $classMetadata2->getAttributesMetadata());
67 public function testSerialize()
69 $classMetadata = new ClassMetadata('a');
71 $a1 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
72 $a1->method('getName')->willReturn('b1');
74 $a2 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
75 $a2->method('getName')->willReturn('b2');
77 $classMetadata->addAttributeMetadata($a1);
78 $classMetadata->addAttributeMetadata($a2);
80 $serialized = serialize($classMetadata);
81 $this->assertEquals($classMetadata, unserialize($serialized));