Skip to content

Commit 1f6f719

Browse files
committed
[Tests] Add test for default include paths
1 parent c4892fa commit 1f6f719

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/*
3+
* Copyright 2021 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace LaravelJsonApi\Laravel\Tests\Acceptance\DefaultIncludePaths;
21+
22+
use App\JsonApi\V1\Posts\PostCollectionQuery;
23+
use App\Models\Post;
24+
use App\Models\Tag;
25+
use LaravelJsonApi\Laravel\Facades\JsonApiRoute;
26+
use LaravelJsonApi\Laravel\Http\Controllers\JsonApiController;
27+
use LaravelJsonApi\Laravel\Tests\Acceptance\TestCase;
28+
29+
class Test extends TestCase
30+
{
31+
32+
/**
33+
* @return void
34+
*/
35+
protected function setUp(): void
36+
{
37+
parent::setUp();
38+
39+
$this->app->bind(PostCollectionQuery::class, TestRequest::class);
40+
41+
JsonApiRoute::server('v1')->prefix('api/v1')->resources(function ($server) {
42+
$server->resource('posts', JsonApiController::class);
43+
});
44+
}
45+
46+
public function test(): void
47+
{
48+
$posts = Post::factory()->count(2)->create();
49+
$tag = Tag::factory()->create();
50+
$tag->posts()->save($posts[0]);
51+
52+
$response = $this
53+
->withoutExceptionHandling()
54+
->jsonApi('posts')
55+
->get('/api/v1/posts');
56+
57+
$response->assertFetchedMany($posts)->assertIncluded([
58+
['type' => 'users', 'id' => $posts[0]->author],
59+
['type' => 'users', 'id' => $posts[1]->author],
60+
['type' => 'tags', 'id' => $tag],
61+
]);
62+
}
63+
64+
/**
65+
* When a query request is using default include paths, we expect the client to be able
66+
* to ask for nothing to be included by providing an empty include path.
67+
*/
68+
public function testNoneIncluded(): void
69+
{
70+
$posts = Post::factory()->count(2)->create();
71+
72+
$response = $this
73+
->withoutExceptionHandling()
74+
->jsonApi('posts')
75+
->get('/api/v1/posts?include=');
76+
77+
$response->assertFetchedMany($posts);
78+
// @TODO assertNoneIncluded should be available on the response.
79+
$response->getDocument()->assertNoneIncluded();
80+
}
81+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
* Copyright 2021 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace LaravelJsonApi\Laravel\Tests\Acceptance\DefaultIncludePaths;
21+
22+
use App\JsonApi\V1\Posts\PostCollectionQuery;
23+
24+
class TestRequest extends PostCollectionQuery
25+
{
26+
27+
/**
28+
* @var string[]|null
29+
*/
30+
protected ?array $defaultIncludePaths = ['author', 'tags'];
31+
}

0 commit comments

Comments
 (0)