@@ -64,45 +64,43 @@ private function getPostObject()
64
64
*/
65
65
public function testItWillRenamePropertiesAndHideFromClass ()
66
66
{
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 ();
89
68
90
69
$ expected = <<<JSON
91
70
{
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"
98
95
},
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"
102
98
}
99
+ }
103
100
}
104
101
JSON ;
105
102
103
+
106
104
$ this ->assertEquals (
107
105
json_decode ($ expected , true ),
108
106
json_decode ((new JsonSerializer (new Mapper ($ mappings )))->serialize ($ this ->getPostObject ()), true )
@@ -111,28 +109,7 @@ public function testItWillRenamePropertiesAndHideFromClass()
111
109
112
110
public function testItCanSerializeArrays ()
113
111
{
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 ();
136
113
137
114
$ expected = <<<JSON
138
115
[
@@ -144,6 +121,20 @@ public function testItCanSerializeArrays()
144
121
"user_id": 1,
145
122
"name": "Post Author"
146
123
},
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
+ ],
147
138
"links": {
148
139
"self": {
149
140
"href": "http://example.com/posts/9"
@@ -161,6 +152,20 @@ public function testItCanSerializeArrays()
161
152
"user_id": 1,
162
153
"name": "Post Author"
163
154
},
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
+ ],
164
169
"links": {
165
170
"self": {
166
171
"href": "http://example.com/posts/9"
@@ -186,4 +191,72 @@ public function testGetTransformer()
186
191
187
192
$ this ->assertInstanceOf (JsonTransformer::class, $ serializer ->getTransformer ());
188
193
}
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
+ }
189
262
}
0 commit comments