Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit 81396e2

Browse files
committed
Tests improvements
1 parent dfe0b36 commit 81396e2

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

Diff for: tests/JsonApiCollectionTest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace SkoreLabs\JsonApi\Tests;
44

55
use Illuminate\Support\Facades\Route;
6-
use SkoreLabs\JsonApi\Http\Resources\JsonApiCollection;
6+
use SkoreLabs\JsonApi\Support\JsonApi;
77
use SkoreLabs\JsonApi\Testing\Assert;
88
use SkoreLabs\JsonApi\Tests\Fixtures\Post;
99

@@ -18,8 +18,10 @@ protected function setUp(): void
1818
{
1919
parent::setUp();
2020

21+
$this->bypassPolicies();
22+
2123
Route::get('/', function () {
22-
return new JsonApiCollection(collect([
24+
return JsonApi::format(collect([
2325
new Post([
2426
'id' => 5,
2527
'title' => 'Test Title',
@@ -29,7 +31,7 @@ protected function setUp(): void
2931
'id' => 6,
3032
'title' => 'Test Title 2',
3133
]),
32-
]), true);
34+
]));
3335
});
3436
}
3537

Diff for: tests/JsonApiResourceTest.php

+28-14
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
namespace SkoreLabs\JsonApi\Tests;
44

55
use Illuminate\Support\Facades\Route;
6-
use SkoreLabs\JsonApi\Http\Resources\JsonApiResource;
6+
use SkoreLabs\JsonApi\Support\JsonApi;
77
use SkoreLabs\JsonApi\Testing\Assert;
88
use SkoreLabs\JsonApi\Tests\Fixtures\Post;
99

1010
class JsonApiResourceTest extends TestCase
1111
{
1212
public function testResourcesMayBeConvertedToJsonApi()
1313
{
14+
$this->bypassPolicies();
15+
1416
Route::get('/', function () {
15-
return new JsonApiResource(new Post([
17+
return JsonApi::format(new Post([
1618
'id' => 5,
1719
'title' => 'Test Title',
1820
'abstract' => 'Test abstract',
19-
]), true);
21+
]));
2022
});
2123

2224
$response = $this->get('/', ['Accept' => 'application/json']);
@@ -37,12 +39,14 @@ public function testResourcesMayBeConvertedToJsonApi()
3739

3840
public function testResourcesHasIdentifier()
3941
{
42+
$this->bypassPolicies();
43+
4044
Route::get('/', function () {
41-
return new JsonApiResource(new Post([
45+
return JsonApi::format(new Post([
4246
'id' => 5,
4347
'title' => 'Test Title',
4448
'abstract' => 'Test abstract',
45-
]), true);
49+
]));
4650
});
4751

4852
$this->get('/', ['Accept' => 'application/json'])->assertJsonApi(function (Assert $json) {
@@ -52,12 +56,14 @@ public function testResourcesHasIdentifier()
5256

5357
public function testResourcesHasAttribute()
5458
{
59+
$this->bypassPolicies();
60+
5561
Route::get('/', function () {
56-
return new JsonApiResource(new Post([
62+
return JsonApi::format(new Post([
5763
'id' => 5,
5864
'title' => 'Test Title',
5965
'abstract' => 'Test abstract',
60-
]), true);
66+
]));
6167
});
6268

6369
$this->get('/', ['Accept' => 'application/json'])->assertJsonApi(function (Assert $json) {
@@ -67,12 +73,14 @@ public function testResourcesHasAttribute()
6773

6874
public function testResourcesHasAttributes()
6975
{
76+
$this->bypassPolicies();
77+
7078
Route::get('/', function () {
71-
return new JsonApiResource(new Post([
79+
return JsonApi::format(new Post([
7280
'id' => 5,
7381
'title' => 'Test Title',
7482
'abstract' => 'Test abstract',
75-
]), true);
83+
]));
7684
});
7785

7886
$this->get('/', ['Accept' => 'application/json'])->assertJsonApi(function (Assert $json) {
@@ -86,7 +94,7 @@ public function testResourcesHasAttributes()
8694
// FIXME: Not available in Laravel 6, wait to support removal
8795
// public function testResourcesMayBeConvertedToJsonApiWithToJsonMethod()
8896
// {
89-
// $resource = new JsonApiResource(new Post([
97+
// $resource = JsonApi::format(new Post([
9098
// 'id' => 5,
9199
// 'title' => 'Test Title',
92100
// 'abstract' => 'Test abstract',
@@ -97,6 +105,8 @@ public function testResourcesHasAttributes()
97105

98106
public function testResourcesWithRelationshipsMayBeConvertedToJsonApi()
99107
{
108+
$this->bypassPolicies();
109+
100110
Route::get('/', function () {
101111
$post = new Post([
102112
'id' => 5,
@@ -109,7 +119,7 @@ public function testResourcesWithRelationshipsMayBeConvertedToJsonApi()
109119
'title' => 'Test Parent Title',
110120
]));
111121

112-
return new JsonApiResource($post, true);
122+
return JsonApi::format($post);
113123
});
114124

115125
$response = $this->get('/', ['Accept' => 'application/json']);
@@ -147,6 +157,8 @@ public function testResourcesWithRelationshipsMayBeConvertedToJsonApi()
147157

148158
public function testResourcesHasRelationshipWith()
149159
{
160+
$this->bypassPolicies();
161+
150162
Route::get('/', function () {
151163
$post = new Post([
152164
'id' => 5,
@@ -159,7 +171,7 @@ public function testResourcesHasRelationshipWith()
159171
'title' => 'Test Parent Title',
160172
]));
161173

162-
return new JsonApiResource($post, true);
174+
return JsonApi::format($post);
163175
});
164176

165177
$this->get('/', ['Accept' => 'application/json'])->assertJsonApi(function (Assert $json) {
@@ -172,6 +184,8 @@ public function testResourcesHasRelationshipWith()
172184

173185
public function testResourcesAtRelationHasAttribute()
174186
{
187+
$this->bypassPolicies();
188+
175189
Route::get('/', function () {
176190
$post = new Post([
177191
'id' => 5,
@@ -184,7 +198,7 @@ public function testResourcesAtRelationHasAttribute()
184198
'title' => 'Test Parent Title',
185199
]));
186200

187-
return new JsonApiResource($post, true);
201+
return JsonApi::format($post);
188202
});
189203

190204
$this->get('/', ['Accept' => 'application/json'])->assertJsonApi(function (Assert $json) {
@@ -198,7 +212,7 @@ public function testResourcesAtRelationHasAttribute()
198212
public function testResourcesMayNotBeConvertedToJsonApiWithoutPermissions()
199213
{
200214
Route::get('/', function () {
201-
return new JsonApiResource(new Post([
215+
return JsonApi::format(new Post([
202216
'id' => 5,
203217
'title' => 'Test Title',
204218
]));

Diff for: tests/TestCase.php

+12
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,16 @@ protected function getPackageProviders($app)
1313
JsonApiServiceProvider::class,
1414
];
1515
}
16+
17+
protected function bypassPolicies()
18+
{
19+
config([
20+
'json-api.authorisable' => [
21+
'view' => true,
22+
'viewAny' => true
23+
]
24+
]);
25+
26+
return $this;
27+
}
1628
}

0 commit comments

Comments
 (0)