forked from json-api-php/json-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompoundDocumentTest.php
264 lines (253 loc) · 9.4 KB
/
CompoundDocumentTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php declare(strict_types=1);
namespace JsonApiPhp\JsonApi\Test;
use JsonApiPhp\JsonApi\Attribute;
use JsonApiPhp\JsonApi\CompoundDocument;
use JsonApiPhp\JsonApi\Included;
use JsonApiPhp\JsonApi\Link\LastLink;
use JsonApiPhp\JsonApi\Link\NextLink;
use JsonApiPhp\JsonApi\Link\RelatedLink;
use JsonApiPhp\JsonApi\Link\SelfLink;
use JsonApiPhp\JsonApi\NullData;
use JsonApiPhp\JsonApi\PaginatedCollection;
use JsonApiPhp\JsonApi\Pagination;
use JsonApiPhp\JsonApi\ResourceCollection;
use JsonApiPhp\JsonApi\ResourceIdentifier;
use JsonApiPhp\JsonApi\ResourceIdentifierCollection;
use JsonApiPhp\JsonApi\ResourceObject;
use JsonApiPhp\JsonApi\ToMany;
use JsonApiPhp\JsonApi\ToOne;
class CompoundDocumentTest extends BaseTestCase
{
public function testOfficialDocsExample()
{
$dan = new ResourceObject(
'people',
'9',
new Attribute('first-name', 'Dan'),
new Attribute('last-name', 'Gebhardt'),
new Attribute('twitter', 'dgeb'),
new SelfLink('http://example.com/people/9')
);
$comment05 = new ResourceObject(
'comments',
'5',
new Attribute('body', 'First!'),
new SelfLink('http://example.com/comments/5'),
new ToOne('author', new ResourceIdentifier('people', '2'))
);
$comment12 = new ResourceObject(
'comments',
'12',
new Attribute('body', 'I like XML better'),
new SelfLink('http://example.com/comments/12'),
new ToOne('author', $dan->identifier())
);
$document = new CompoundDocument(
new PaginatedCollection(
new Pagination(
new NextLink('http://example.com/articles?page[offset]=2'),
new LastLink('http://example.com/articles?page[offset]=10')
),
new ResourceCollection(
new ResourceObject(
'articles',
'1',
new Attribute('title', 'JSON API paints my bikeshed!'),
new SelfLink('http://example.com/articles/1'),
new ToOne(
'author',
$dan->identifier(),
new SelfLink('http://example.com/articles/1/relationships/author'),
new RelatedLink('http://example.com/articles/1/author')
),
new ToMany(
'comments',
new ResourceIdentifierCollection(
$comment05->identifier(),
$comment12->identifier()
),
new SelfLink('http://example.com/articles/1/relationships/comments'),
new RelatedLink('http://example.com/articles/1/comments')
)
)
)
),
new Included($dan, $comment05, $comment12),
new SelfLink('http://example.com/articles')
);
$this->assertEncodesTo(
'
{
"links": {
"self": "http://example.com/articles",
"next": "http://example.com/articles?page[offset]=2",
"last": "http://example.com/articles?page[offset]=10"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"links": {
"self": "http://example.com/articles/1"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
},
"comments": {
"links": {
"self": "http://example.com/articles/1/relationships/comments",
"related": "http://example.com/articles/1/comments"
},
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
}
}],
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
}, {
"type": "comments",
"id": "5",
"attributes": {
"body": "First!"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "2" }
}
},
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"attributes": {
"body": "I like XML better"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "http://example.com/comments/12"
}
}]
}
',
$document
);
}
/**
* Compound documents require “full linkage”, meaning that every included resource MUST be identified
* by at least one resource identifier object in the same document.
* These resource identifier objects could either be primary data or represent resource linkage
* contained within primary or included resources.
*
* @dataProvider documentsWithoutFullLinkage
* @param callable $create_doc
*/
public function testFullLinkage(callable $create_doc)
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Full linkage required for apples:1');
$create_doc();
}
public function documentsWithoutFullLinkage(): array
{
$included = new Included(new ResourceObject('apples', '1'));
return [
[
function () use ($included) {
return new CompoundDocument(new NullData(), $included);
},
],
[
function () use ($included) {
return new CompoundDocument(new ResourceCollection(), $included);
},
],
[
function () use ($included) {
return new CompoundDocument(new ResourceIdentifier('oranges', '1'), $included);
},
],
[
function () use ($included) {
return new CompoundDocument(
new ResourceIdentifierCollection(
new ResourceIdentifier('oranges', '1'),
new ResourceIdentifier('oranges', '1')
),
$included
);
},
],
[
function () use ($included) {
return new CompoundDocument(
new ResourceCollection(new ResourceObject('oranges', '1'), new ResourceObject('oranges', '1')),
$included
);
},
],
];
}
public function testIncludedResourceMayBeIdentifiedByLinkageInPrimaryData()
{
$author = new ResourceObject('people', '9');
$article = new ResourceObject(
'articles',
'1',
new ToOne('author', $author->identifier())
);
$doc = new CompoundDocument($article, new Included($author));
$this->assertNotEmpty($doc);
}
public function testIncludedResourceMayBeIdentifiedByAnotherLinkedResource()
{
$writer = new ResourceObject('writers', '3', new Attribute('name', 'Eric Evans'));
$book = new ResourceObject(
'books',
'2',
new Attribute('name', 'Domain Driven Design'),
new ToOne('author', $writer->identifier())
);
$cart = new ResourceObject(
'shopping-carts',
'1',
new ToMany('contents', new ResourceIdentifierCollection($book->identifier()))
);
$doc = new CompoundDocument($cart, new Included($book, $writer));
$this->assertNotEmpty($doc);
}
/**
* A compound document MUST NOT include more than one resource object for each type and id pair.
*/
public function testCanNotBeManyIncludedResourcesWithEqualIdentifiers()
{
$this->expectException('LogicException');
$this->expectExceptionMessage('Resource apples:1 is already included');
$apple = new ResourceObject('apples', '1');
new CompoundDocument($apple->identifier(), new Included($apple, $apple));
}
}