@@ -126,4 +126,174 @@ public function testParseResourceObject()
126
126
$ this ->assertSame ($ data ->getType (), 'people ' );
127
127
$ this ->assertSame ($ data ->getId (), '9 ' );
128
128
}
129
+
130
+ /**
131
+ * @test
132
+ */
133
+ public function testParseCompleteResourceObjectWithMultipleRelationships ()
134
+ {
135
+ $ string = $ this ->getJsonString ('04_complete_document_with_multiple_relationships.js ' );
136
+ $ document = Helper::parse ($ string );
137
+
138
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Document ' , $ document );
139
+ $ this ->assertFalse ($ document ->hasErrors ());
140
+ $ this ->assertFalse ($ document ->hasMeta ());
141
+ $ this ->assertFalse ($ document ->hasJsonapi ());
142
+ $ this ->assertFalse ($ document ->hasLinks ());
143
+ $ this ->assertTrue ($ document ->hasIncluded ());
144
+ $ this ->assertTrue ($ document ->hasData ());
145
+
146
+ $ resources = $ document ->getData ();
147
+
148
+ $ this ->assertTrue (is_array ($ resources ));
149
+ $ this ->assertTrue (count ($ resources ) === 1 );
150
+
151
+ $ resource = $ resources [0 ];
152
+
153
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Resource ' , $ resource );
154
+ $ this ->assertFalse ($ resource ->hasMeta ());
155
+ $ this ->assertSame ($ resource ->getType (), 'articles ' );
156
+ $ this ->assertSame ($ resource ->getId (), '1 ' );
157
+ $ this ->assertTrue ($ resource ->hasAttributes ());
158
+ $ this ->assertTrue ($ resource ->hasRelationships ());
159
+ $ this ->assertTrue ($ resource ->hasLinks ());
160
+
161
+ $ attributes = $ resource ->getAttributes ();
162
+
163
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Attributes ' , $ attributes );
164
+ $ this ->assertTrue ($ attributes ->__isset ('title ' ));
165
+ $ this ->assertSame ($ attributes ->get ('title ' ), 'JSON API paints my bikeshed! ' );
166
+
167
+ $ collection = $ resource ->getRelationships ();
168
+
169
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\RelationshipCollection ' , $ collection );
170
+ $ this ->assertTrue ($ collection ->__isset ('author ' ));
171
+ $ this ->assertTrue ($ collection ->__isset ('comments ' ));
172
+
173
+ $ author = $ collection ->get ('author ' );
174
+
175
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Relationship ' , $ author );
176
+ $ this ->assertTrue ($ author ->hasLinks ());
177
+ $ this ->assertTrue ($ author ->hasData ());
178
+
179
+ $ links = $ author ->getLinks ();
180
+
181
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\RelationshipLink ' , $ links );
182
+ $ this ->assertTrue ($ links ->__isset ('self ' ));
183
+ $ this ->assertSame ($ links ->get ('self ' ), 'http://example.com/articles/1/relationships/author ' );
184
+ $ this ->assertTrue ($ links ->__isset ('related ' ));
185
+ $ this ->assertSame ($ links ->get ('related ' ), 'http://example.com/articles/1/author ' );
186
+
187
+ $ data = $ author ->getData ();
188
+
189
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\ResourceIdentifier ' , $ data );
190
+ $ this ->assertSame ($ data ->getType (), 'people ' );
191
+ $ this ->assertSame ($ data ->getId (), '9 ' );
192
+
193
+ $ comments = $ collection ->get ('comments ' );
194
+
195
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Relationship ' , $ comments );
196
+ $ this ->assertTrue ($ comments ->hasLinks ());
197
+ $ this ->assertTrue ($ comments ->hasData ());
198
+
199
+ $ links = $ comments ->getLinks ();
200
+
201
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\RelationshipLink ' , $ links );
202
+ $ this ->assertTrue ($ links ->__isset ('self ' ));
203
+ $ this ->assertSame ($ links ->get ('self ' ), 'http://example.com/articles/1/relationships/comments ' );
204
+ $ this ->assertTrue ($ links ->__isset ('related ' ));
205
+ $ this ->assertSame ($ links ->get ('related ' ), 'http://example.com/articles/1/comments ' );
206
+
207
+ $ data_array = $ comments ->getData ();
208
+
209
+ $ this ->assertTrue (is_array ($ data_array ));
210
+ $ this ->assertTrue (count ($ data_array ) === 2 );
211
+
212
+ $ identifier = $ data_array [0 ];
213
+
214
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\ResourceIdentifier ' , $ identifier );
215
+ $ this ->assertSame ($ identifier ->getType (), 'comments ' );
216
+ $ this ->assertSame ($ identifier ->getId (), '5 ' );
217
+
218
+ $ identifier = $ data_array [1 ];
219
+
220
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\ResourceIdentifier ' , $ identifier );
221
+ $ this ->assertSame ($ identifier ->getType (), 'comments ' );
222
+ $ this ->assertSame ($ identifier ->getId (), '12 ' );
223
+
224
+ $ links = $ resource ->getLinks ();
225
+
226
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Link ' , $ links );
227
+ $ this ->assertTrue ($ links ->__isset ('self ' ));
228
+ $ this ->assertSame ($ links ->get ('self ' ), 'http://example.com/articles/1 ' );
229
+
230
+ $ includes = $ document ->getIncluded ();
231
+
232
+ $ this ->assertTrue (is_array ($ includes ));
233
+ $ this ->assertTrue (count ($ includes ) === 3 );
234
+
235
+ $ include = $ includes [0 ];
236
+
237
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Resource ' , $ include );
238
+ $ this ->assertSame ($ include ->getType (), 'people ' );
239
+ $ this ->assertSame ($ include ->getId (), '9 ' );
240
+ $ this ->assertTrue ($ include ->hasAttributes ());
241
+ $ this ->assertTrue ($ include ->hasLinks ());
242
+
243
+ $ attributes = $ include ->getAttributes ();
244
+
245
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Attributes ' , $ attributes );
246
+ $ this ->assertTrue ($ attributes ->__isset ('first-name ' ));
247
+ $ this ->assertSame ($ attributes ->get ('first-name ' ), 'Dan ' );
248
+ $ this ->assertTrue ($ attributes ->__isset ('last-name ' ));
249
+ $ this ->assertSame ($ attributes ->get ('last-name ' ), 'Gebhardt ' );
250
+ $ this ->assertTrue ($ attributes ->__isset ('twitter ' ));
251
+ $ this ->assertSame ($ attributes ->get ('twitter ' ), 'dgeb ' );
252
+
253
+ $ links = $ include ->getLinks ();
254
+
255
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Link ' , $ links );
256
+ $ this ->assertTrue ($ links ->__isset ('self ' ));
257
+ $ this ->assertSame ($ links ->get ('self ' ), 'http://example.com/people/9 ' );
258
+
259
+ $ include = $ includes [1 ];
260
+
261
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Resource ' , $ include );
262
+ $ this ->assertSame ($ include ->getType (), 'comments ' );
263
+ $ this ->assertSame ($ include ->getId (), '5 ' );
264
+ $ this ->assertTrue ($ include ->hasAttributes ());
265
+ $ this ->assertTrue ($ include ->hasLinks ());
266
+
267
+ $ attributes = $ include ->getAttributes ();
268
+
269
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Attributes ' , $ attributes );
270
+ $ this ->assertTrue ($ attributes ->__isset ('body ' ));
271
+ $ this ->assertSame ($ attributes ->get ('body ' ), 'First! ' );
272
+
273
+ $ links = $ include ->getLinks ();
274
+
275
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Link ' , $ links );
276
+ $ this ->assertTrue ($ links ->__isset ('self ' ));
277
+ $ this ->assertSame ($ links ->get ('self ' ), 'http://example.com/comments/5 ' );
278
+
279
+ $ include = $ includes [2 ];
280
+
281
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Resource ' , $ include );
282
+ $ this ->assertSame ($ include ->getType (), 'comments ' );
283
+ $ this ->assertSame ($ include ->getId (), '12 ' );
284
+ $ this ->assertTrue ($ include ->hasAttributes ());
285
+ $ this ->assertTrue ($ include ->hasLinks ());
286
+
287
+ $ attributes = $ include ->getAttributes ();
288
+
289
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Attributes ' , $ attributes );
290
+ $ this ->assertTrue ($ attributes ->__isset ('body ' ));
291
+ $ this ->assertSame ($ attributes ->get ('body ' ), 'I like XML better ' );
292
+
293
+ $ links = $ include ->getLinks ();
294
+
295
+ $ this ->assertInstanceOf ('Art4\JsonApiClient\Link ' , $ links );
296
+ $ this ->assertTrue ($ links ->__isset ('self ' ));
297
+ $ this ->assertSame ($ links ->get ('self ' ), 'http://example.com/comments/12 ' );
298
+ }
129
299
}
0 commit comments