forked from json-api-php/json-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompoundDocumentTest.php
More file actions
147 lines (136 loc) · 4.86 KB
/
CompoundDocumentTest.php
File metadata and controls
147 lines (136 loc) · 4.86 KB
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
<?php
/**
*
* * This file is part of JSON:API implementation for PHP.
* *
* * (c) Alexey Karapetov <karapetov@gmail.com>
* *
* * For the full copyright and license information, please view the LICENSE
* * file that was distributed with this source code.
*
*/
declare(strict_types=1);
namespace JsonApiPhp\JsonApi\Test\Document;
use JsonApiPhp\JsonApi\Document\Document;
use JsonApiPhp\JsonApi\Document\Resource\NullData;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Linkage;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
use JsonApiPhp\JsonApi\Test\HasAssertEqualsAsJson;
use PHPUnit\Framework\TestCase;
/**
* To reduce the number of HTTP requests, servers MAY allow responses that include related resources
* along with the requested primary resources. Such responses are called “compound documents”.
*
* In a compound document, all included resources MUST be represented as an array
* of resource objects in a top-level included member.
*
* 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.
*
* The only exception to the full linkage requirement is when relationship fields
* that would otherwise contain linkage data are excluded via sparse fieldsets.
*
* Note: Full linkage ensures that included resources are related to either the primary data
* (which could be resource objects or resource identifier objects) or to each other.
*
* @see http://jsonapi.org/format/#document-compound-documents
*/
class CompoundDocumentTest extends TestCase
{
use HasAssertEqualsAsJson;
public function testIncludedResourcesRepresentedAsArray()
{
$apple = new ResourceObject('apples', '1');
$apple->setAttribute('color', 'red');
$orange = new ResourceObject('oranges', '1');
$orange->setAttribute('color', 'orange');
$basket = new ResourceObject('basket', '1');
$basket->setRelationship(
'fruits',
Relationship::fromLinkage(
Linkage::fromManyResourceIds(
$apple->toId(),
$orange->toId()
)
)
);
$doc = Document::fromResource($basket);
$doc->setIncluded($apple, $orange);
$this->assertEquals(
[
[
'type' => 'apples',
'id' => '1',
'attributes' => [
'color' => 'red',
],
],
[
'type' => 'oranges',
'id' => '1',
'attributes' => [
'color' => 'orange',
],
],
],
$this->convertToArray($doc)['included']
);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Full linkage is required for apples:1
*/
public function testFullLinkageIsRequired()
{
$doc = Document::fromResource(new NullData);
$doc->setIncluded(new ResourceObject('apples', '1'));
json_encode($doc);
}
public function testFullLinkageIsNotRequiredIfSparse()
{
$doc = Document::fromResource(new NullData);
$doc->markSparse();
$doc->setIncluded(new ResourceObject('apples', '1'));
$this->assertCanBeBuilt($doc);
}
public function testIncludedResourceMayBeIdentifiedByPrimaryData()
{
$apple = new ResourceObject('apples', '1');
$apple->setAttribute('color', 'red');
$doc = Document::fromResource($apple->toId());
$doc->setIncluded($apple);
$this->assertCanBeBuilt($doc);
}
public function testIncludedResourceMayBeIdentifiedByAnotherIncludedResource()
{
/**
* BasketID identifies included BasketObject
* BasketObject identifies included AppleObject
*/
$apple = new ResourceObject('apples', '1');
$apple->setAttribute('color', 'red');
$basket = new ResourceObject('basket', '1');
$basket->setRelationship(
'fruits',
Relationship::fromLinkage(
Linkage::fromManyResourceIds(
$apple->toId()
)
)
);
$doc = Document::fromResource($basket->toId());
$doc->setIncluded($apple, $basket);
$this->assertCanBeBuilt($doc);
}
private function convertToArray($object): array
{
return json_decode(json_encode($object), true);
}
private function assertCanBeBuilt($doc)
{
$this->assertInternalType('string', json_encode($doc));
}
}