40
40
*/
41
41
class CompoundDocumentTest extends BaseTestCase
42
42
{
43
+ /**
44
+ * In a compound document, all included resources MUST be represented as an array of resource objects
45
+ * in a top-level included member.
46
+ */
43
47
public function testIncludedResourcesRepresentedAsArray ()
44
48
{
45
49
$ apple = new ResourceObject ('apples ' , '1 ' );
46
50
$ apple ->setAttribute ('color ' , 'red ' );
47
51
$ orange = new ResourceObject ('oranges ' , '1 ' );
48
52
$ orange ->setAttribute ('color ' , 'orange ' );
49
- $ basket = new ResourceObject ('basket ' , '1 ' );
53
+ $ basket = new ResourceObject ('baskets ' , '1 ' );
50
54
$ basket ->setRelationship (
51
55
'fruits ' ,
52
56
Relationship::fromLinkage (
@@ -56,26 +60,48 @@ public function testIncludedResourcesRepresentedAsArray()
56
60
)
57
61
)
58
62
);
59
- $ doc = \ JsonApiPhp \ JsonApi \ Document::fromResource ($ basket );
63
+ $ doc = Document::fromResource ($ basket );
60
64
$ doc ->setIncluded ($ apple , $ orange );
61
- $ this ->assertEquals (
62
- [
63
- [
64
- 'type ' => 'apples ' ,
65
- 'id ' => '1 ' ,
66
- 'attributes ' => [
67
- 'color ' => 'red ' ,
68
- ],
69
- ],
70
- [
71
- 'type ' => 'oranges ' ,
72
- 'id ' => '1 ' ,
73
- 'attributes ' => [
74
- 'color ' => 'orange ' ,
75
- ],
76
- ],
77
- ],
78
- $ this ->convertToArray ($ doc )['included ' ]
65
+ $ this ->assertEncodesTo (
66
+ '
67
+ {
68
+ "data": {
69
+ "type": "baskets",
70
+ "id": "1",
71
+ "relationships": {
72
+ "fruits": {
73
+ "data": [
74
+ {
75
+ "type": "apples",
76
+ "id": "1"
77
+ },
78
+ {
79
+ "type": "oranges",
80
+ "id": "1"
81
+ }
82
+ ]
83
+ }
84
+ }
85
+ },
86
+ "included": [
87
+ {
88
+ "type": "apples",
89
+ "id": "1",
90
+ "attributes": {
91
+ "color": "red"
92
+ }
93
+ },
94
+ {
95
+ "type": "oranges",
96
+ "id": "1",
97
+ "attributes": {
98
+ "color": "orange"
99
+ }
100
+ }
101
+ ]
102
+ }
103
+ ' ,
104
+ $ doc
79
105
);
80
106
}
81
107
@@ -90,12 +116,28 @@ public function testFullLinkageIsRequired()
90
116
json_encode ($ doc );
91
117
}
92
118
119
+ /**
120
+ * A compound document must be explicitly marked as sparse. In this case full linkage is not required.
121
+ */
93
122
public function testFullLinkageIsNotRequiredIfSparse ()
94
123
{
95
- $ doc = \ JsonApiPhp \ JsonApi \ Document::fromResource (new NullResource );
124
+ $ doc = Document::fromResource (new NullResource );
96
125
$ doc ->markSparse ();
97
126
$ doc ->setIncluded (new ResourceObject ('apples ' , '1 ' ));
98
- $ this ->assertCanBeBuilt ($ doc );
127
+ $ this ->assertEncodesTo (
128
+ '
129
+ {
130
+ "data": null,
131
+ "included": [
132
+ {
133
+ "type": "apples",
134
+ "id": "1"
135
+ }
136
+ ]
137
+ }
138
+ ' ,
139
+ $ doc
140
+ );
99
141
}
100
142
101
143
public function testIncludedResourceMayBeIdentifiedByPrimaryData ()
@@ -104,7 +146,7 @@ public function testIncludedResourceMayBeIdentifiedByPrimaryData()
104
146
$ apple ->setAttribute ('color ' , 'red ' );
105
147
$ doc = Document::fromResource ($ apple ->toId ());
106
148
$ doc ->setIncluded ($ apple );
107
- $ this ->assertCanBeBuilt ( $ doc );
149
+ $ this ->assertJson ( json_encode ( $ doc) );
108
150
}
109
151
110
152
public function testIncludedResourceMayBeIdentifiedByAnotherIncludedResource ()
@@ -124,18 +166,8 @@ public function testIncludedResourceMayBeIdentifiedByAnotherIncludedResource()
124
166
)
125
167
)
126
168
);
127
- $ doc = \ JsonApiPhp \ JsonApi \ Document::fromResource ($ basket ->toId ());
169
+ $ doc = Document::fromResource ($ basket ->toId ());
128
170
$ doc ->setIncluded ($ apple , $ basket );
129
- $ this ->assertCanBeBuilt ($ doc );
130
- }
131
-
132
- private function convertToArray ($ object ): array
133
- {
134
- return json_decode (json_encode ($ object ), true );
135
- }
136
-
137
- private function assertCanBeBuilt ($ doc )
138
- {
139
- $ this ->assertInternalType ('string ' , json_encode ($ doc ));
171
+ $ this ->assertJson (json_encode ($ doc ));
140
172
}
141
173
}
0 commit comments