From d523b5c2b059bfe3487218eae838f2334640b690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nil=20Portugu=C3=A9s=20Calder=C3=B3?= Date: Thu, 16 Jun 2016 23:19:50 +0200 Subject: [PATCH] Fixed tests. Missing mappings --- README.md | 54 +++++------ tests/JsonSerializerTest.php | 179 ++++++++++++++++++++++++----------- 2 files changed, 153 insertions(+), 80 deletions(-) diff --git a/README.md b/README.md index 2b81462..4daf1f3 100644 --- a/README.md +++ b/README.md @@ -183,35 +183,35 @@ Content-type: application/json; charset=utf-8 ```json { - "post_id": 9, - "headline": "Hello World", - "body": "Your first post", - "author": { - "user_id": 1, - "name": "Post Author" + "post_id": 9, + "headline": "Hello World", + "body": "Your first post", + "author": { + "user_id": 1, + "name": "Post Author" + }, + "comments": [ + { + "comment_id": 1000, + "dates": { + "created_at": "2015-07-18T12:13:00+02:00", + "accepted_at": "2015-07-19T00:00:00+02:00" + }, + "comment": "Have no fear, sers, your king is safe.", + "user": { + "user_id": 2, + "name": "Barristan Selmy" + } + } + ], + "links": { + "self": { + "href": "http://example.com/posts/9" }, - "comments": [ - { - "comment_id": 1000, - "dates": { - "created_at": "2015-07-18T12:13:00+00:00", - "accepted_at": "2015-07-19T00:00:00+00:00" - }, - "comment": "Have no fear, sers, your king is safe.", - "user": { - "user_id": 2, - "name": "Barristan Selmy" - } - } - ], - "links": { - "comments": { - "href": "http://localhost:8000/post/9/comments" - }, - "self": { - "href": "http://localhost:8000/post/9" - } + "comments": { + "href": "http://example.com/posts/9/comments" } + } } ``` diff --git a/tests/JsonSerializerTest.php b/tests/JsonSerializerTest.php index 8ffb343..d906d2d 100644 --- a/tests/JsonSerializerTest.php +++ b/tests/JsonSerializerTest.php @@ -64,45 +64,43 @@ private function getPostObject() */ public function testItWillRenamePropertiesAndHideFromClass() { - $mappings = [ - [ - 'class' => Post::class, - 'alias' => 'Message', - 'aliased_properties' => [ - 'title' => 'headline', - 'content' => 'body', - ], - 'hide_properties' => [ - 'comments', - ], - 'id_properties' => [ - 'postId', - ], - 'urls' => [ - // Mandatory - 'self' => 'http://example.com/posts/{postId}', - // Optional - 'comments' => 'http://example.com/posts/{postId}/comments', - ], - ], - ]; + $mappings = $this->mappings(); $expected = <<assertEquals( json_decode($expected, true), json_decode((new JsonSerializer(new Mapper($mappings)))->serialize($this->getPostObject()), true) @@ -111,28 +109,7 @@ public function testItWillRenamePropertiesAndHideFromClass() public function testItCanSerializeArrays() { - $mappings = [ - [ - 'class' => Post::class, - 'alias' => 'Message', - 'aliased_properties' => [ - 'title' => 'headline', - 'content' => 'body', - ], - 'hide_properties' => [ - 'comments', - ], - 'id_properties' => [ - 'postId', - ], - 'urls' => [ - // Mandatory - 'self' => 'http://example.com/posts/{postId}', - // Optional - 'comments' => 'http://example.com/posts/{postId}/comments', - ], - ], - ]; + $mappings = $this->mappings(); $expected = <<assertInstanceOf(JsonTransformer::class, $serializer->getTransformer()); } + + + /** + * @return array + */ + protected function mappings() + { + return [ + [ + 'class' => Post::class, + 'alias' => 'Message', + 'aliased_properties' => [ + 'author' => 'author', + 'title' => 'headline', + 'content' => 'body', + ], + 'hide_properties' => [ + ], + 'id_properties' => [ + 'postId', + ], + 'urls' => [ + // Mandatory + 'self' => 'http://example.com/posts/{postId}', + // Optional + 'comments' => 'http://example.com/posts/{postId}/comments', + ], + ], + [ + 'class' => User::class, + 'alias' => '', + 'aliased_properties' => [], + 'hide_properties' => [], + 'id_properties' => [ + 'userId', + ], + 'urls' => [ + 'self' => 'http://example.com/users/{userId}', + 'friends' => 'http://example.com/users/{userId}/friends', + 'comments' => 'http://example.com/users/{userId}/comments', + ], + ], + [ + 'class' => Comment::class, + 'alias' => '', + 'aliased_properties' => [], + 'hide_properties' => [], + 'id_properties' => [ + 'commentId', + ], + 'urls' => [ + 'self' => 'http://example.com/comments/{commentId}', + ], + ], + [ + 'class' => CommentId::class, + 'alias' => '', + 'aliased_properties' => [], + 'hide_properties' => [], + 'id_properties' => [ + 'commentId', + ], + 'urls' => [ + 'self' => 'http://example.com/comments/{commentId}', + ], + ], + ]; + } }