serialize($object)); } public function getFirstClassMapCollectionsValues() { return [ [[1, 2, 3], $this->getContent('inline_map')], [[], $this->getContent('inline_empty_map')], [['a' => 'b', 'c' => 'd', 5], $this->getContent('inline_deserialization_map')], ]; } /** * @param array $items * @param array $expected * * @dataProvider getFirstClassMapCollectionsValues */ public function testFirstClassMapCollections($items, $expected): void { $collection = new FirstClassMapCollection($items); self::assertSame($expected, $this->serialize($collection)); self::assertEquals( $collection, $this->deserialize($expected, get_class($collection)) ); } public function testAddLinksToOutput() { $this->dispatcher->addListener('serializer.post_serialize', static function (Event $event) { self::assertFalse($event->getVisitor()->hasData('_links')); }, 'JMS\Serializer\Tests\Fixtures\Author', 'json'); $this->dispatcher->addSubscriber(new LinkAddingSubscriber()); $this->dispatcher->addListener('serializer.post_serialize', static function (Event $event) { self::assertTrue($event->getVisitor()->hasData('_links')); }, 'JMS\Serializer\Tests\Fixtures\Author', 'json'); $this->handlerRegistry->registerHandler( GraphNavigatorInterface::DIRECTION_SERIALIZATION, 'JMS\Serializer\Tests\Fixtures\AuthorList', 'json', static function (SerializationVisitorInterface $visitor, AuthorList $data, array $type, Context $context) { return $visitor->visitArray(iterator_to_array($data), $type); } ); $list = new AuthorList(); $list->add(new Author('foo')); $list->add(new Author('bar')); self::assertEquals('[{"full_name":"foo","_links":{"details":"http:\/\/foo.bar\/details\/foo","comments":"http:\/\/foo.bar\/details\/foo\/comments"}},{"full_name":"bar","_links":{"details":"http:\/\/foo.bar\/details\/bar","comments":"http:\/\/foo.bar\/details\/bar\/comments"}}]', $this->serialize($list)); } public function testReplaceNameInOutput() { $this->dispatcher->addSubscriber(new ReplaceNameSubscriber()); $this->handlerRegistry->registerHandler( GraphNavigatorInterface::DIRECTION_SERIALIZATION, 'JMS\Serializer\Tests\Fixtures\AuthorList', 'json', static function (SerializationVisitorInterface $visitor, AuthorList $data, array $type, Context $context) { return $visitor->visitArray(iterator_to_array($data), $type); } ); $list = new AuthorList(); $list->add(new Author('foo')); $list->add(new Author('bar')); self::assertEquals('[{"full_name":"new name"},{"full_name":"new name"}]', $this->serialize($list)); } /** * @expectedException RuntimeException * @expectedExceptionMessage Invalid data "baz" (string), expected "JMS\Serializer\Tests\Fixtures\Author". */ public function testDeserializingObjectWithObjectPropertyWithNoArrayToObject() { $content = $this->getContent('object_with_object_property_no_array_to_author'); $object = $this->deserialize($content, 'JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty'); self::assertEquals('bar', $object->getFoo()); self::assertInstanceOf('JMS\Serializer\Tests\Fixtures\Author', $object->getAuthor()); } public function testDeserializingObjectWithObjectProperty() { $content = $this->getContent('object_with_object_property'); $object = $this->deserialize($content, 'JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty'); self::assertEquals('bar', $object->getFoo()); self::assertInstanceOf('JMS\Serializer\Tests\Fixtures\Author', $object->getAuthor()); self::assertEquals('baz', $object->getAuthor()->getName()); } public function getPrimitiveTypes() { return [ [ 'type' => 'boolean', 'data' => true, ], [ 'type' => 'integer', 'data' => 123, ], [ 'type' => 'string', 'data' => 'hello', ], [ 'type' => 'double', 'data' => 0.1234, ], ]; } /** * @dataProvider getPrimitiveTypes */ public function testPrimitiveTypes($primitiveType, $data) { $navigator = $this->getMockBuilder(GraphNavigatorInterface::class)->getMock(); $factory = new JsonSerializationVisitorFactory(); $visitor = $factory->getVisitor(); $visitor->setNavigator($navigator); $functionToCall = 'visit' . ucfirst($primitiveType); $result = $visitor->$functionToCall($data, [], $this->getMockBuilder(SerializationContext::class)->getMock()); if ('double' === $primitiveType) { $primitiveType = 'float'; } self::assertInternalType($primitiveType, $result); } /** * @group empty-object */ public function testSerializeEmptyObject() { self::assertEquals('{}', $this->serialize(new Author(null))); } /** * @group encoding * @expectedException RuntimeException * @expectedExceptionMessage Your data could not be encoded because it contains invalid UTF8 characters. */ public function testSerializeWithNonUtf8EncodingWhenDisplayErrorsOff() { ini_set('display_errors', '1'); $this->serialize(['foo' => 'bar', 'bar' => pack('H*', 'c32e')]); } /** * @group encoding * @expectedException RuntimeException * @expectedExceptionMessage Your data could not be encoded because it contains invalid UTF8 characters. */ public function testSerializeWithNonUtf8EncodingWhenDisplayErrorsOn() { ini_set('display_errors', '0'); $this->serialize(['foo' => 'bar', 'bar' => pack('H*', 'c32e')]); } public function testSerializeArrayWithEmptyObject() { self::assertEquals('[{}]', $this->serialize([new \stdClass()])); } public function testInlineArray() { $object = new ObjectWithInlineArray(['a' => 'b', 'c' => 'd']); $serialized = $this->serialize($object); self::assertEquals('{"a":"b","c":"d"}', $serialized); self::assertEquals($object, $this->deserialize($serialized, ObjectWithInlineArray::class)); } public function testSerializeRootArrayWithDefinedKeys() { $author1 = new Author('Jim'); $author2 = new Author('Mark'); $data = [ 'jim' => $author1, 'mark' => $author2, ]; self::assertEquals('{"jim":{"full_name":"Jim"},"mark":{"full_name":"Mark"}}', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array'))); self::assertEquals('[{"full_name":"Jim"},{"full_name":"Mark"}]', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array'))); self::assertEquals('{"jim":{"full_name":"Jim"},"mark":{"full_name":"Mark"}}', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array'))); $data = [ $author1, $author2, ]; self::assertEquals('[{"full_name":"Jim"},{"full_name":"Mark"}]', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array'))); self::assertEquals('{"0":{"full_name":"Jim"},"1":{"full_name":"Mark"}}', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array'))); self::assertEquals('{"0":{"full_name":"Jim"},"1":{"full_name":"Mark"}}', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array'))); } public function getTypeHintedArrays() { return [ [[1, 2], '[1,2]', null], [['a', 'b'], '["a","b"]', null], [['a' => 'a', 'b' => 'b'], '{"a":"a","b":"b"}', null], [[], '[]', null], [[], '[]', SerializationContext::create()->setInitialType('array')], [[], '[]', SerializationContext::create()->setInitialType('array')], [[], '{}', SerializationContext::create()->setInitialType('array')], [[1, 2], '[1,2]', SerializationContext::create()->setInitialType('array')], [[1 => 1, 2 => 2], '{"1":1,"2":2}', SerializationContext::create()->setInitialType('array')], [[1 => 1, 2 => 2], '[1,2]', SerializationContext::create()->setInitialType('array')], [['a', 'b'], '["a","b"]', SerializationContext::create()->setInitialType('array')], [[1 => 'a', 2 => 'b'], '["a","b"]', SerializationContext::create()->setInitialType('array')], [['a' => 'a', 'b' => 'b'], '["a","b"]', SerializationContext::create()->setInitialType('array')], [[1, 2], '{"0":1,"1":2}', SerializationContext::create()->setInitialType('array')], [[1, 2], '{"0":1,"1":2}', SerializationContext::create()->setInitialType('array')], [[1, 2], '{"0":"1","1":"2"}', SerializationContext::create()->setInitialType('array')], [['a', 'b'], '{"0":"a","1":"b"}', SerializationContext::create()->setInitialType('array')], [['a' => 'a', 'b' => 'b'], '{"a":"a","b":"b"}', SerializationContext::create()->setInitialType('array')], ]; } /** * @param array $array * @param string $expected * @param SerializationContext|null $context * * @dataProvider getTypeHintedArrays */ public function testTypeHintedArraySerialization(array $array, $expected, $context = null) { self::assertEquals($expected, $this->serialize($array, $context)); } public function getTypeHintedArraysAndStdClass() { $c1 = new \stdClass(); $c2 = new \stdClass(); $c2->foo = 'bar'; $tag = new Tag('tag'); $c3 = new \stdClass(); $c3->foo = $tag; return [ [[$c1], '[{}]', SerializationContext::create()->setInitialType('array')], [[$c2], '[{"foo":"bar"}]', SerializationContext::create()->setInitialType('array')], [[$tag], '[{"name":"tag"}]', SerializationContext::create()->setInitialType('array')], [[$c1], '{"0":{}}', SerializationContext::create()->setInitialType('array')], [[$c2], '{"0":{"foo":"bar"}}', SerializationContext::create()->setInitialType('array')], [[$c3], '{"0":{"foo":{"name":"tag"}}}', SerializationContext::create()->setInitialType('array')], [[$c3], '[{"foo":{"name":"tag"}}]', SerializationContext::create()->setInitialType('array')], [[$tag], '{"0":{"name":"tag"}}', SerializationContext::create()->setInitialType('array')], ]; } /** * @param array $array * @param string $expected * @param SerializationContext|null $context * * @dataProvider getTypeHintedArraysAndStdClass */ public function testTypeHintedArrayAndStdClassSerialization(array $array, $expected, $context = null) { self::assertEquals($expected, $this->serialize($array, $context)); } protected function getFormat() { return 'json'; } } class LinkAddingSubscriber implements EventSubscriberInterface { public function onPostSerialize(Event $event) { $author = $event->getObject(); $event->getVisitor()->setData('_links', [ 'details' => 'http://foo.bar/details/' . $author->getName(), 'comments' => 'http://foo.bar/details/' . $author->getName() . '/comments', ]); } public static function getSubscribedEvents() { return [ ['event' => 'serializer.post_serialize', 'method' => 'onPostSerialize', 'format' => 'json', 'class' => 'JMS\Serializer\Tests\Fixtures\Author'], ]; } } class ReplaceNameSubscriber implements EventSubscriberInterface { public function onPostSerialize(Event $event) { $event->getVisitor()->setData('full_name', 'new name'); } public static function getSubscribedEvents() { return [ ['event' => 'serializer.post_serialize', 'method' => 'onPostSerialize', 'format' => 'json', 'class' => 'JMS\Serializer\Tests\Fixtures\Author'], ]; } }