builder->build(); $this->assertEquals('"foo"', $serializer->serialize('foo', 'json')); $this->assertEquals(' ', $serializer->serialize('foo', 'xml')); $this->assertEquals('foo ', $serializer->serialize('foo', 'yml')); $this->assertEquals('foo', $serializer->deserialize('"foo"', 'string', 'json')); $this->assertEquals('foo', $serializer->deserialize('', 'string', 'xml')); } public function testWithCache() { $this->assertFileNotExists($this->tmpDir); $this->assertSame($this->builder, $this->builder->setCacheDir($this->tmpDir)); $serializer = $this->builder->build(); $this->assertFileExists($this->tmpDir); $this->assertFileExists($this->tmpDir.'/annotations'); $this->assertFileExists($this->tmpDir.'/metadata'); $factory = $this->getField($serializer, 'factory'); $this->assertAttributeSame(false, 'debug', $factory); $this->assertAttributeNotSame(null, 'cache', $factory); } public function testDoesAddDefaultHandlers() { $serializer = $this->builder->build(); $this->assertEquals('"2020-04-16T00:00:00+0000"', $serializer->serialize(new \DateTime('2020-04-16', new \DateTimeZone('UTC')), 'json')); } public function testDoesNotAddDefaultHandlersWhenExplicitlyConfigured() { $this->assertSame($this->builder, $this->builder->configureHandlers(function(HandlerRegistry $registry) { })); $this->assertEquals('[]', $this->builder->build()->serialize(new \DateTime('2020-04-16'), 'json')); } /** * @expectedException RuntimeException */ public function testDoesNotAddOtherVisitorsWhenConfiguredExplicitly() { $this->assertSame( $this->builder, $this->builder->setSerializationVisitor('json', new JsonSerializationVisitor(new CamelCaseNamingStrategy())) ); $this->builder->build()->serialize('foo', 'xml'); } private function getField($obj, $name) { $ref = new \ReflectionProperty($obj, $name); $ref->setAccessible(true); return $ref->getValue($obj); } protected function setUp() { $this->builder = SerializerBuilder::create(); $this->fs = new Filesystem(); $this->tmpDir = sys_get_temp_dir().'/serializer'; $this->fs->remove($this->tmpDir); clearstatcache(); } protected function tearDown() { $this->fs->remove($this->tmpDir); } }