SerializerBuilderTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\Serializer\Tests;
  18. use JMS\Serializer\SerializerBuilder;
  19. use Symfony\Component\Filesystem\Filesystem;
  20. use JMS\Serializer\Handler\HandlerRegistry;
  21. use JMS\Serializer\JsonSerializationVisitor;
  22. use JMS\Serializer\Naming\CamelCaseNamingStrategy;
  23. class SerializerBuilderTest extends \PHPUnit_Framework_TestCase
  24. {
  25. /** @var SerializerBuilder */
  26. private $builder;
  27. private $fs;
  28. private $tmpDir;
  29. public function testBuildWithoutAnythingElse()
  30. {
  31. $serializer = $this->builder->build();
  32. $this->assertEquals('"foo"', $serializer->serialize('foo', 'json'));
  33. $this->assertEquals('<?xml version="1.0" encoding="UTF-8"?>
  34. <result><![CDATA[foo]]></result>
  35. ', $serializer->serialize('foo', 'xml'));
  36. $this->assertEquals('foo
  37. ', $serializer->serialize('foo', 'yml'));
  38. $this->assertEquals('foo', $serializer->deserialize('"foo"', 'string', 'json'));
  39. $this->assertEquals('foo', $serializer->deserialize('<?xml version="1.0" encoding="UTF-8"?><result><![CDATA[foo]]></result>', 'string', 'xml'));
  40. }
  41. public function testWithCache()
  42. {
  43. $this->assertFileNotExists($this->tmpDir);
  44. $this->assertSame($this->builder, $this->builder->setCacheDir($this->tmpDir));
  45. $serializer = $this->builder->build();
  46. $this->assertFileExists($this->tmpDir);
  47. $this->assertFileExists($this->tmpDir.'/annotations');
  48. $this->assertFileExists($this->tmpDir.'/metadata');
  49. $factory = $this->getField($serializer, 'factory');
  50. $this->assertAttributeSame(false, 'debug', $factory);
  51. $this->assertAttributeNotSame(null, 'cache', $factory);
  52. }
  53. public function testDoesAddDefaultHandlers()
  54. {
  55. $serializer = $this->builder->build();
  56. $this->assertEquals('"2020-04-16T00:00:00+0000"', $serializer->serialize(new \DateTime('2020-04-16', new \DateTimeZone('UTC')), 'json'));
  57. }
  58. public function testDoesNotAddDefaultHandlersWhenExplicitlyConfigured()
  59. {
  60. $this->assertSame($this->builder, $this->builder->configureHandlers(function(HandlerRegistry $registry) {
  61. }));
  62. $this->assertEquals('{}', $this->builder->build()->serialize(new \DateTime('2020-04-16'), 'json'));
  63. }
  64. /**
  65. * @expectedException JMS\Serializer\Exception\UnsupportedFormatException
  66. * @expectedExceptionMessage The format "xml" is not supported for serialization.
  67. */
  68. public function testDoesNotAddOtherVisitorsWhenConfiguredExplicitly()
  69. {
  70. $this->assertSame(
  71. $this->builder,
  72. $this->builder->setSerializationVisitor('json', new JsonSerializationVisitor(new CamelCaseNamingStrategy()))
  73. );
  74. $this->builder->build()->serialize('foo', 'xml');
  75. }
  76. public function testIncludeInterfaceMetadata()
  77. {
  78. $this->assertFalse(
  79. $this->getIncludeInterfaces($this->builder),
  80. 'Interface metadata are not included by default'
  81. );
  82. $this->assertTrue(
  83. $this->getIncludeInterfaces($this->builder->includeInterfaceMetadata(true)),
  84. 'Force including interface metadata'
  85. );
  86. $this->assertFalse(
  87. $this->getIncludeInterfaces($this->builder->includeInterfaceMetadata(false)),
  88. 'Force not including interface metadata'
  89. );
  90. $this->assertSame(
  91. $this->builder,
  92. $this->builder->includeInterfaceMetadata(true)
  93. );
  94. }
  95. protected function setUp()
  96. {
  97. $this->builder = SerializerBuilder::create();
  98. $this->fs = new Filesystem();
  99. $this->tmpDir = sys_get_temp_dir().'/serializer';
  100. $this->fs->remove($this->tmpDir);
  101. clearstatcache();
  102. }
  103. protected function tearDown()
  104. {
  105. $this->fs->remove($this->tmpDir);
  106. }
  107. private function getField($obj, $name)
  108. {
  109. $ref = new \ReflectionProperty($obj, $name);
  110. $ref->setAccessible(true);
  111. return $ref->getValue($obj);
  112. }
  113. private function getIncludeInterfaces(SerializerBuilder $builder)
  114. {
  115. $factory = $this->getField($builder->build(), 'factory');
  116. return $this->getField($factory, 'includeInterfaces');
  117. }
  118. }