ClassMetadataTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace JMS\Serializer\Tests\Metadata;
  3. use JMS\Serializer\Metadata\ClassMetadata;
  4. use JMS\Serializer\Metadata\PropertyMetadata;
  5. class ClassMetadataTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function getAccessOrderCases()
  8. {
  9. return [
  10. [array('b', 'a'), array('b', 'a')],
  11. [array('a', 'b'), array('a', 'b')],
  12. [array('b'), array('b', 'a')],
  13. [array('a'), array('a', 'b')],
  14. [array('foo', 'bar'), array('b', 'a')],
  15. ];
  16. }
  17. public function testSerialization()
  18. {
  19. $meta = new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'b');
  20. $restoredMeta = unserialize(serialize($meta));
  21. $this->assertEquals($meta, $restoredMeta);
  22. }
  23. /**
  24. * @dataProvider getAccessOrderCases
  25. */
  26. public function testSetAccessorOrderCustom(array $order, array $expected)
  27. {
  28. $metadata = new ClassMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder');
  29. $metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'b'));
  30. $metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'a'));
  31. $this->assertEquals(array('b', 'a'), array_keys($metadata->propertyMetadata));
  32. $metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_CUSTOM, $order);
  33. $this->assertEquals($expected, array_keys($metadata->propertyMetadata));
  34. }
  35. public function testSetAccessorOrderAlphabetical()
  36. {
  37. $metadata = new ClassMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder');
  38. $metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'b'));
  39. $metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'a'));
  40. $this->assertEquals(array('b', 'a'), array_keys($metadata->propertyMetadata));
  41. $metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_ALPHABETICAL);
  42. $this->assertEquals(array('a', 'b'), array_keys($metadata->propertyMetadata));
  43. }
  44. /**
  45. * @dataProvider providerPublicMethodData
  46. */
  47. public function testAccessorTypePublicMethod($property, $getterInit, $setterInit, $getterName, $setterName)
  48. {
  49. $object = new PropertyMetadataPublicMethod();
  50. $metadata = new PropertyMetadata(get_class($object), $property);
  51. $metadata->setAccessor(PropertyMetadata::ACCESS_TYPE_PUBLIC_METHOD, $getterInit, $setterInit);
  52. $this->assertEquals($getterName, $metadata->getter);
  53. $this->assertEquals($setterName, $metadata->setter);
  54. $metadata->setValue($object, 'x');
  55. $this->assertEquals(sprintf('%1$s:%1$s:x', strtoupper($property)), $metadata->getValue($object));
  56. }
  57. /**
  58. * @dataProvider providerPublicMethodException
  59. */
  60. public function testAccessorTypePublicMethodException($getter, $setter, $message)
  61. {
  62. $this->setExpectedException('\JMS\Serializer\Exception\RuntimeException', $message);
  63. $object = new PropertyMetadataPublicMethod();
  64. $metadata = new PropertyMetadata(get_class($object), 'e');
  65. $metadata->setAccessor(PropertyMetadata::ACCESS_TYPE_PUBLIC_METHOD, $getter, $setter);
  66. }
  67. public function providerPublicMethodData()
  68. {
  69. return array(
  70. array('a', null, null, 'geta', 'seta'),
  71. array('b', null, null, 'isb', 'setb'),
  72. array('c', null, null, 'hasc', 'setc'),
  73. array('d', 'fetchd', 'saved', 'fetchd', 'saved')
  74. );
  75. }
  76. public function providerPublicMethodException()
  77. {
  78. return array(
  79. array(null, null, 'a public getE method, nor a public isE method, nor a public hasE method in class'),
  80. array(null, 'setx', 'a public getE method, nor a public isE method, nor a public hasE method in class'),
  81. array('getx', null, 'no public setE method in class'),
  82. );
  83. }
  84. }
  85. class PropertyMetadataOrder
  86. {
  87. private $b, $a;
  88. }
  89. class PropertyMetadataPublicMethod
  90. {
  91. private $a, $b, $c, $d, $e;
  92. public function getA()
  93. {
  94. return 'A:' . $this->a;
  95. }
  96. public function setA($a)
  97. {
  98. $this->a = 'A:' . $a;
  99. }
  100. public function isB()
  101. {
  102. return 'B:' . $this->b;
  103. }
  104. public function setB($b)
  105. {
  106. $this->b = 'B:' . $b;
  107. }
  108. public function hasC()
  109. {
  110. return 'C:' . $this->c;
  111. }
  112. public function setC($c)
  113. {
  114. $this->c = 'C:' . $c;
  115. }
  116. public function fetchD()
  117. {
  118. return 'D:' . $this->d;
  119. }
  120. public function saveD($d)
  121. {
  122. $this->d = 'D:' . $d;
  123. }
  124. }