Skip to content

Commit d523b5c

Browse files
committed
Fixed tests. Missing mappings
1 parent 6c06960 commit d523b5c

File tree

2 files changed

+153
-80
lines changed

2 files changed

+153
-80
lines changed

README.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -183,35 +183,35 @@ Content-type: application/json; charset=utf-8
183183

184184
```json
185185
{
186-
"post_id": 9,
187-
"headline": "Hello World",
188-
"body": "Your first post",
189-
"author": {
190-
"user_id": 1,
191-
"name": "Post Author"
186+
"post_id": 9,
187+
"headline": "Hello World",
188+
"body": "Your first post",
189+
"author": {
190+
"user_id": 1,
191+
"name": "Post Author"
192+
},
193+
"comments": [
194+
{
195+
"comment_id": 1000,
196+
"dates": {
197+
"created_at": "2015-07-18T12:13:00+02:00",
198+
"accepted_at": "2015-07-19T00:00:00+02:00"
199+
},
200+
"comment": "Have no fear, sers, your king is safe.",
201+
"user": {
202+
"user_id": 2,
203+
"name": "Barristan Selmy"
204+
}
205+
}
206+
],
207+
"links": {
208+
"self": {
209+
"href": "http://example.com/posts/9"
192210
},
193-
"comments": [
194-
{
195-
"comment_id": 1000,
196-
"dates": {
197-
"created_at": "2015-07-18T12:13:00+00:00",
198-
"accepted_at": "2015-07-19T00:00:00+00:00"
199-
},
200-
"comment": "Have no fear, sers, your king is safe.",
201-
"user": {
202-
"user_id": 2,
203-
"name": "Barristan Selmy"
204-
}
205-
}
206-
],
207-
"links": {
208-
"comments": {
209-
"href": "http://localhost:8000/post/9/comments"
210-
},
211-
"self": {
212-
"href": "http://localhost:8000/post/9"
213-
}
211+
"comments": {
212+
"href": "http://example.com/posts/9/comments"
214213
}
214+
}
215215
}
216216
```
217217

tests/JsonSerializerTest.php

Lines changed: 126 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -64,45 +64,43 @@ private function getPostObject()
6464
*/
6565
public function testItWillRenamePropertiesAndHideFromClass()
6666
{
67-
$mappings = [
68-
[
69-
'class' => Post::class,
70-
'alias' => 'Message',
71-
'aliased_properties' => [
72-
'title' => 'headline',
73-
'content' => 'body',
74-
],
75-
'hide_properties' => [
76-
'comments',
77-
],
78-
'id_properties' => [
79-
'postId',
80-
],
81-
'urls' => [
82-
// Mandatory
83-
'self' => 'http://example.com/posts/{postId}',
84-
// Optional
85-
'comments' => 'http://example.com/posts/{postId}/comments',
86-
],
87-
],
88-
];
67+
$mappings = $this->mappings();
8968

9069
$expected = <<<JSON
9170
{
92-
"post_id": 9,
93-
"headline": "Hello World",
94-
"body": "Your first post",
95-
"author": {
96-
"user_id": 1,
97-
"name": "Post Author"
71+
"post_id": 9,
72+
"headline": "Hello World",
73+
"body": "Your first post",
74+
"author": {
75+
"user_id": 1,
76+
"name": "Post Author"
77+
},
78+
"comments": [
79+
{
80+
"comment_id": 1000,
81+
"dates": {
82+
"created_at": "2015-07-18T12:13:00+02:00",
83+
"accepted_at": "2015-07-19T00:00:00+02:00"
84+
},
85+
"comment": "Have no fear, sers, your king is safe.",
86+
"user": {
87+
"user_id": 2,
88+
"name": "Barristan Selmy"
89+
}
90+
}
91+
],
92+
"links": {
93+
"self": {
94+
"href": "http://example.com/posts/9"
9895
},
99-
"links": {
100-
"comments": {"href": "http://example.com/posts/9/comments" },
101-
"self": {"href": "http://example.com/posts/9" }
96+
"comments": {
97+
"href": "http://example.com/posts/9/comments"
10298
}
99+
}
103100
}
104101
JSON;
105102

103+
106104
$this->assertEquals(
107105
json_decode($expected, true),
108106
json_decode((new JsonSerializer(new Mapper($mappings)))->serialize($this->getPostObject()), true)
@@ -111,28 +109,7 @@ public function testItWillRenamePropertiesAndHideFromClass()
111109

112110
public function testItCanSerializeArrays()
113111
{
114-
$mappings = [
115-
[
116-
'class' => Post::class,
117-
'alias' => 'Message',
118-
'aliased_properties' => [
119-
'title' => 'headline',
120-
'content' => 'body',
121-
],
122-
'hide_properties' => [
123-
'comments',
124-
],
125-
'id_properties' => [
126-
'postId',
127-
],
128-
'urls' => [
129-
// Mandatory
130-
'self' => 'http://example.com/posts/{postId}',
131-
// Optional
132-
'comments' => 'http://example.com/posts/{postId}/comments',
133-
],
134-
],
135-
];
112+
$mappings = $this->mappings();
136113

137114
$expected = <<<JSON
138115
[
@@ -144,6 +121,20 @@ public function testItCanSerializeArrays()
144121
"user_id": 1,
145122
"name": "Post Author"
146123
},
124+
"comments": [
125+
{
126+
"comment_id": 1000,
127+
"dates": {
128+
"created_at": "2015-07-18T12:13:00+02:00",
129+
"accepted_at": "2015-07-19T00:00:00+02:00"
130+
},
131+
"comment": "Have no fear, sers, your king is safe.",
132+
"user": {
133+
"user_id": 2,
134+
"name": "Barristan Selmy"
135+
}
136+
}
137+
],
147138
"links": {
148139
"self": {
149140
"href": "http://example.com/posts/9"
@@ -161,6 +152,20 @@ public function testItCanSerializeArrays()
161152
"user_id": 1,
162153
"name": "Post Author"
163154
},
155+
"comments": [
156+
{
157+
"comment_id": 1000,
158+
"dates": {
159+
"created_at": "2015-07-18T12:13:00+02:00",
160+
"accepted_at": "2015-07-19T00:00:00+02:00"
161+
},
162+
"comment": "Have no fear, sers, your king is safe.",
163+
"user": {
164+
"user_id": 2,
165+
"name": "Barristan Selmy"
166+
}
167+
}
168+
],
164169
"links": {
165170
"self": {
166171
"href": "http://example.com/posts/9"
@@ -186,4 +191,72 @@ public function testGetTransformer()
186191

187192
$this->assertInstanceOf(JsonTransformer::class, $serializer->getTransformer());
188193
}
194+
195+
196+
/**
197+
* @return array
198+
*/
199+
protected function mappings()
200+
{
201+
return [
202+
[
203+
'class' => Post::class,
204+
'alias' => 'Message',
205+
'aliased_properties' => [
206+
'author' => 'author',
207+
'title' => 'headline',
208+
'content' => 'body',
209+
],
210+
'hide_properties' => [
211+
],
212+
'id_properties' => [
213+
'postId',
214+
],
215+
'urls' => [
216+
// Mandatory
217+
'self' => 'http://example.com/posts/{postId}',
218+
// Optional
219+
'comments' => 'http://example.com/posts/{postId}/comments',
220+
],
221+
],
222+
[
223+
'class' => User::class,
224+
'alias' => '',
225+
'aliased_properties' => [],
226+
'hide_properties' => [],
227+
'id_properties' => [
228+
'userId',
229+
],
230+
'urls' => [
231+
'self' => 'http://example.com/users/{userId}',
232+
'friends' => 'http://example.com/users/{userId}/friends',
233+
'comments' => 'http://example.com/users/{userId}/comments',
234+
],
235+
],
236+
[
237+
'class' => Comment::class,
238+
'alias' => '',
239+
'aliased_properties' => [],
240+
'hide_properties' => [],
241+
'id_properties' => [
242+
'commentId',
243+
],
244+
'urls' => [
245+
'self' => 'http://example.com/comments/{commentId}',
246+
],
247+
],
248+
[
249+
'class' => CommentId::class,
250+
'alias' => '',
251+
'aliased_properties' => [],
252+
'hide_properties' => [],
253+
'id_properties' => [
254+
'commentId',
255+
],
256+
'urls' => [
257+
'self' => 'http://example.com/comments/{commentId}',
258+
],
259+
],
260+
];
261+
}
189262
}

0 commit comments

Comments
 (0)