|
9 | 9 | use Tobyz\JsonApiServer\Schema\Field\ToMany;
|
10 | 10 | use Tobyz\JsonApiServer\Schema\Field\ToOne;
|
11 | 11 | use Tobyz\Tests\JsonApiServer\AbstractTestCase;
|
| 12 | +use Tobyz\Tests\JsonApiServer\MockCollection; |
12 | 13 | use Tobyz\Tests\JsonApiServer\MockResource;
|
13 | 14 |
|
14 | 15 | /**
|
@@ -182,4 +183,92 @@ public function test_relationship_inclusion_on_list_endpoint()
|
182 | 183 | $response->getBody(),
|
183 | 184 | );
|
184 | 185 | }
|
| 186 | + |
| 187 | + public function test_relationship_inclusion_for_polymorphic_relationship() |
| 188 | + { |
| 189 | + $api = new JsonApi(); |
| 190 | + |
| 191 | + $api->resource( |
| 192 | + new MockResource( |
| 193 | + 'users', |
| 194 | + models: [($user1 = (object) ['id' => '1', 'name' => 'Toby'])], |
| 195 | + fields: [Attribute::make('name')], |
| 196 | + ), |
| 197 | + ); |
| 198 | + |
| 199 | + $api->resource( |
| 200 | + new MockResource( |
| 201 | + 'posts', |
| 202 | + models: [($post1 = (object) ['id' => '1', 'author' => $user1])], |
| 203 | + fields: [ |
| 204 | + ToOne::make('author') |
| 205 | + ->type('users') |
| 206 | + ->includable(), |
| 207 | + ], |
| 208 | + ), |
| 209 | + ); |
| 210 | + |
| 211 | + $api->collection( |
| 212 | + new MockCollection('subjects', [ |
| 213 | + 'users' => [$user1], |
| 214 | + 'posts' => [$post1], |
| 215 | + ]), |
| 216 | + ); |
| 217 | + |
| 218 | + $api->resource( |
| 219 | + new MockResource( |
| 220 | + 'notifications', |
| 221 | + models: [ |
| 222 | + ((object) ['id' => '1', 'subject' => $post1]), |
| 223 | + ((object) ['id' => '2', 'subject' => $user1]), |
| 224 | + ], |
| 225 | + endpoints: [Index::make()], |
| 226 | + fields: [ |
| 227 | + ToOne::make('subject') |
| 228 | + ->collection('subjects') |
| 229 | + ->includable(), |
| 230 | + ], |
| 231 | + ), |
| 232 | + ); |
| 233 | + |
| 234 | + $response = $api->handle( |
| 235 | + $this->buildRequest('GET', '/notifications?include=subject.author'), |
| 236 | + ); |
| 237 | + |
| 238 | + $this->assertJsonApiDocumentSubset( |
| 239 | + [ |
| 240 | + 'data' => [ |
| 241 | + [ |
| 242 | + 'type' => 'notifications', |
| 243 | + 'id' => '1', |
| 244 | + 'relationships' => [ |
| 245 | + 'subject' => [ |
| 246 | + 'data' => ['type' => 'posts', 'id' => '1'], |
| 247 | + ], |
| 248 | + ], |
| 249 | + ], |
| 250 | + [ |
| 251 | + 'type' => 'notifications', |
| 252 | + 'id' => '2', |
| 253 | + 'relationships' => [ |
| 254 | + 'subject' => [ |
| 255 | + 'data' => ['type' => 'users', 'id' => '1'], |
| 256 | + ], |
| 257 | + ], |
| 258 | + ], |
| 259 | + ], |
| 260 | + 'included' => [ |
| 261 | + [ |
| 262 | + 'type' => 'posts', |
| 263 | + 'id' => '1', |
| 264 | + ], |
| 265 | + [ |
| 266 | + 'type' => 'users', |
| 267 | + 'id' => '1', |
| 268 | + ], |
| 269 | + ], |
| 270 | + ], |
| 271 | + $response->getBody(), |
| 272 | + ); |
| 273 | + } |
185 | 274 | }
|
0 commit comments