ArrayTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace JMS\Serializer\Tests\Serializer;
  3. use Doctrine\Common\Annotations\AnnotationReader;
  4. use JMS\Serializer\Construction\UnserializeObjectConstructor;
  5. use JMS\Serializer\Handler\HandlerRegistry;
  6. use JMS\Serializer\JsonDeserializationVisitor;
  7. use JMS\Serializer\JsonSerializationVisitor;
  8. use JMS\Serializer\Metadata\Driver\AnnotationDriver;
  9. use JMS\Serializer\Naming\CamelCaseNamingStrategy;
  10. use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
  11. use JMS\Serializer\Serializer;
  12. use JMS\Serializer\Tests\Fixtures\Author;
  13. use JMS\Serializer\Tests\Fixtures\AuthorList;
  14. use JMS\Serializer\Tests\Fixtures\Order;
  15. use JMS\Serializer\Tests\Fixtures\Price;
  16. use Metadata\MetadataFactory;
  17. use PhpCollection\Map;
  18. class ArrayTest extends \PHPUnit_Framework_TestCase
  19. {
  20. protected $serializer;
  21. public function setUp()
  22. {
  23. $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
  24. $this->serializer = new Serializer(
  25. new MetadataFactory(new AnnotationDriver(new AnnotationReader())),
  26. new HandlerRegistry(),
  27. new UnserializeObjectConstructor(),
  28. new Map(array('json' => new JsonSerializationVisitor($namingStrategy))),
  29. new Map(array('json' => new JsonDeserializationVisitor($namingStrategy)))
  30. );
  31. }
  32. public function testToArray()
  33. {
  34. $order = new Order(new Price(5));
  35. $expected = array(
  36. 'cost' => array(
  37. 'price' => 5
  38. )
  39. );
  40. $result = $this->serializer->toArray($order);
  41. $this->assertEquals($expected, $result);
  42. }
  43. /**
  44. * @dataProvider scalarValues
  45. */
  46. public function testToArrayWithScalar($input)
  47. {
  48. $this->setExpectedException('JMS\Serializer\Exception\RuntimeException', sprintf(
  49. 'The input data of type "%s" did not convert to an array, but got a result of type "%s".',
  50. gettype($input),
  51. gettype($input)
  52. ));
  53. $result = $this->serializer->toArray($input);
  54. $this->assertEquals(array($input), $result);
  55. }
  56. public function scalarValues()
  57. {
  58. return array(
  59. array(42),
  60. array(3.14159),
  61. array('helloworld'),
  62. array(true),
  63. );
  64. }
  65. public function testFromArray()
  66. {
  67. $data = array(
  68. 'cost' => array(
  69. 'price' => 2.5
  70. )
  71. );
  72. $expected = new Order(new Price(2.5));
  73. $result = $this->serializer->fromArray($data, 'JMS\Serializer\Tests\Fixtures\Order');
  74. $this->assertEquals($expected, $result);
  75. }
  76. public function testToArrayReturnsArrayObjectAsArray()
  77. {
  78. $result = $this->serializer->toArray(new Author(null));
  79. $this->assertSame(array(), $result);
  80. }
  81. public function testToArrayConversNestedArrayObjects()
  82. {
  83. $list = new AuthorList();
  84. $list->add(new Author(null));
  85. $result = $this->serializer->toArray($list);
  86. $this->assertSame(array('authors' => array(array())), $result);
  87. }
  88. }