Skip to content

Commit 7df2587

Browse files
committed
Add test with multiple relationships
1 parent e4a11f2 commit 7df2587

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"data": [{
3+
"type": "articles",
4+
"id": "1",
5+
"attributes": {
6+
"title": "JSON API paints my bikeshed!"
7+
},
8+
"links": {
9+
"self": "http://example.com/articles/1"
10+
},
11+
"relationships": {
12+
"author": {
13+
"links": {
14+
"self": "http://example.com/articles/1/relationships/author",
15+
"related": "http://example.com/articles/1/author"
16+
},
17+
"data": { "type": "people", "id": "9" }
18+
},
19+
"comments": {
20+
"links": {
21+
"self": "http://example.com/articles/1/relationships/comments",
22+
"related": "http://example.com/articles/1/comments"
23+
},
24+
"data": [
25+
{ "type": "comments", "id": "5" },
26+
{ "type": "comments", "id": "12" }
27+
]
28+
}
29+
}
30+
}],
31+
"included": [{
32+
"type": "people",
33+
"id": "9",
34+
"attributes": {
35+
"first-name": "Dan",
36+
"last-name": "Gebhardt",
37+
"twitter": "dgeb"
38+
},
39+
"links": {
40+
"self": "http://example.com/people/9"
41+
}
42+
}, {
43+
"type": "comments",
44+
"id": "5",
45+
"attributes": {
46+
"body": "First!"
47+
},
48+
"links": {
49+
"self": "http://example.com/comments/5"
50+
}
51+
}, {
52+
"type": "comments",
53+
"id": "12",
54+
"attributes": {
55+
"body": "I like XML better"
56+
},
57+
"links": {
58+
"self": "http://example.com/comments/12"
59+
}
60+
}]
61+
}

tests/integration/IntegrationTest.php

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,174 @@ public function testParseResourceObject()
126126
$this->assertSame($data->getType(), 'people');
127127
$this->assertSame($data->getId(), '9');
128128
}
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+
}
129299
}

0 commit comments

Comments
 (0)