ClassMetadataTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Tests\Mapping;
  11. use Symfony\Component\Serializer\Mapping\ClassMetadata;
  12. /**
  13. * @author Kévin Dunglas <dunglas@gmail.com>
  14. */
  15. class ClassMetadataTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testInterface()
  18. {
  19. $classMetadata = new ClassMetadata('name');
  20. $this->assertInstanceOf('Symfony\Component\Serializer\Mapping\ClassMetadataInterface', $classMetadata);
  21. }
  22. public function testAttributeMetadata()
  23. {
  24. $classMetadata = new ClassMetadata('c');
  25. $a1 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
  26. $a1->method('getName')->willReturn('a1');
  27. $a2 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
  28. $a2->method('getName')->willReturn('a2');
  29. $classMetadata->addAttributeMetadata($a1);
  30. $classMetadata->addAttributeMetadata($a2);
  31. $this->assertEquals(array('a1' => $a1, 'a2' => $a2), $classMetadata->getAttributesMetadata());
  32. }
  33. public function testMerge()
  34. {
  35. $classMetadata1 = new ClassMetadata('c1');
  36. $classMetadata2 = new ClassMetadata('c2');
  37. $ac1 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
  38. $ac1->method('getName')->willReturn('a1');
  39. $ac1->method('getGroups')->willReturn(array('a', 'b'));
  40. $ac2 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
  41. $ac2->method('getName')->willReturn('a1');
  42. $ac2->method('getGroups')->willReturn(array('b', 'c'));
  43. $classMetadata1->addAttributeMetadata($ac1);
  44. $classMetadata2->addAttributeMetadata($ac2);
  45. $classMetadata1->merge($classMetadata2);
  46. $ac1->method('getGroups')->willReturn('a', 'b', 'c');
  47. $this->assertEquals(array('a1' => $ac1), $classMetadata2->getAttributesMetadata());
  48. }
  49. public function testSerialize()
  50. {
  51. $classMetadata = new ClassMetadata('a');
  52. $a1 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
  53. $a1->method('getName')->willReturn('b1');
  54. $a2 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
  55. $a2->method('getName')->willReturn('b2');
  56. $classMetadata->addAttributeMetadata($a1);
  57. $classMetadata->addAttributeMetadata($a2);
  58. $serialized = serialize($classMetadata);
  59. $this->assertEquals($classMetadata, unserialize($serialized));
  60. }
  61. }