Skip to content

Commit 7768fe5

Browse files
committed
Remove minimal attributes with closure
1 parent 3b0b42a commit 7768fe5

File tree

3 files changed

+42
-56
lines changed

3 files changed

+42
-56
lines changed

src/Concerns/Attributes.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,11 @@ trait Attributes
1717
private static bool $minimalAttributes = false;
1818

1919
/**
20-
* @api
21-
*
22-
* @param (callable(): void)|null $callback
2320
* @return void
2421
*/
25-
public static function minimalAttributes(callable|null $callback = null)
22+
public static function minimalAttributes($value = true)
2623
{
27-
self::$minimalAttributes = true;
28-
29-
if ($callback === null) {
30-
return;
31-
}
32-
33-
try {
34-
$callback();
35-
} finally {
36-
self::$minimalAttributes = false;
37-
}
24+
self::$minimalAttributes = $value;
3825
}
3926

4027
/**

tests/Feature/AttributesTest.php

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -211,56 +211,54 @@ public function testItThrowsWhenFieldsParameterIsNotAStringValue(): void
211211

212212
public function testItCanSpecifyMinimalAttributes(): void
213213
{
214-
JsonApiResource::minimalAttributes(function () {
215-
$user = (new BasicModel([
216-
'id' => 'user-id',
217-
'name' => 'user-name',
218-
]));
219-
Route::get('test-route', fn () => UserResource::make($user));
214+
JsonApiResource::minimalAttributes();
215+
$user = (new BasicModel([
216+
'id' => 'user-id',
217+
'name' => 'user-name',
218+
]));
219+
Route::get('test-route', fn () => UserResource::make($user));
220220

221-
$response = $this->getJson('test-route');
221+
$response = $this->getJson('test-route');
222222

223-
$response->assertOk();
224-
$response->assertExactJson([
225-
'data' => [
226-
'type' => 'basicModels',
227-
'id' => 'user-id',
228-
],
229-
'jsonapi' => [
230-
'version' => '1.0',
231-
],
232-
]);
233-
$this->assertValidJsonApi($response);
234-
});
223+
$response->assertOk();
224+
$response->assertExactJson([
225+
'data' => [
226+
'type' => 'basicModels',
227+
'id' => 'user-id',
228+
],
229+
'jsonapi' => [
230+
'version' => '1.0',
231+
],
232+
]);
233+
$this->assertValidJsonApi($response);
235234
}
236235

237236
public function testItCanRequestAttributesWhenUsingMinimalAttributes()
238237
{
239-
JsonApiResource::minimalAttributes(function () {
240-
$user = (new BasicModel([
241-
'id' => 'user-id',
242-
'name' => 'user-name',
243-
'location' => 'Melbourne',
244-
]));
245-
Route::get('test-route', fn () => UserResource::make($user));
238+
JsonApiResource::minimalAttributes();
239+
$user = (new BasicModel([
240+
'id' => 'user-id',
241+
'name' => 'user-name',
242+
'location' => 'Melbourne',
243+
]));
244+
Route::get('test-route', fn () => UserResource::make($user));
246245

247-
$response = $this->getJson('test-route?fields[basicModels]=name');
246+
$response = $this->getJson('test-route?fields[basicModels]=name');
248247

249-
$response->assertOk();
250-
$response->assertExactJson([
251-
'data' => [
252-
'type' => 'basicModels',
253-
'id' => 'user-id',
254-
'attributes' => [
255-
'name' => 'user-name',
256-
],
257-
],
258-
'jsonapi' => [
259-
'version' => '1.0',
248+
$response->assertOk();
249+
$response->assertExactJson([
250+
'data' => [
251+
'type' => 'basicModels',
252+
'id' => 'user-id',
253+
'attributes' => [
254+
'name' => 'user-name',
260255
],
261-
]);
262-
$this->assertValidJsonApi($response);
263-
});
256+
],
257+
'jsonapi' => [
258+
'version' => '1.0',
259+
],
260+
]);
261+
$this->assertValidJsonApi($response);
264262
}
265263

266264
public function testItCanUseSparseFieldsetsWithIncludedCollections(): void

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected function tearDown(): void
2929
parent::tearDown();
3030

3131
JsonApiResource::resolveServerImplementationNormally();
32+
JsonApiResource::maximalAttributes();
3233
}
3334

3435
protected function assertValidJsonApi(TestResponse|string|array $data): void

0 commit comments

Comments
 (0)