diff --git a/src/Concerns/Attributes.php b/src/Concerns/Attributes.php index 15e2b37..0dd3b3e 100644 --- a/src/Concerns/Attributes.php +++ b/src/Concerns/Attributes.php @@ -17,24 +17,11 @@ trait Attributes private static bool $minimalAttributes = false; /** - * @api - * - * @param (callable(): void)|null $callback * @return void */ - public static function minimalAttributes(callable|null $callback = null) + public static function minimalAttributes($value = true) { - self::$minimalAttributes = true; - - if ($callback === null) { - return; - } - - try { - $callback(); - } finally { - self::$minimalAttributes = false; - } + self::$minimalAttributes = $value; } /** diff --git a/tests/Feature/AttributesTest.php b/tests/Feature/AttributesTest.php index f79e56d..4934480 100644 --- a/tests/Feature/AttributesTest.php +++ b/tests/Feature/AttributesTest.php @@ -211,56 +211,54 @@ public function testItThrowsWhenFieldsParameterIsNotAStringValue(): void public function testItCanSpecifyMinimalAttributes(): void { - JsonApiResource::minimalAttributes(function () { - $user = (new BasicModel([ - 'id' => 'user-id', - 'name' => 'user-name', - ])); - Route::get('test-route', fn () => UserResource::make($user)); + JsonApiResource::minimalAttributes(); + $user = (new BasicModel([ + 'id' => 'user-id', + 'name' => 'user-name', + ])); + Route::get('test-route', fn () => UserResource::make($user)); - $response = $this->getJson('test-route'); + $response = $this->getJson('test-route'); - $response->assertOk(); - $response->assertExactJson([ - 'data' => [ - 'type' => 'basicModels', - 'id' => 'user-id', - ], - 'jsonapi' => [ - 'version' => '1.0', - ], - ]); - $this->assertValidJsonApi($response); - }); + $response->assertOk(); + $response->assertExactJson([ + 'data' => [ + 'type' => 'basicModels', + 'id' => 'user-id', + ], + 'jsonapi' => [ + 'version' => '1.0', + ], + ]); + $this->assertValidJsonApi($response); } public function testItCanRequestAttributesWhenUsingMinimalAttributes() { - JsonApiResource::minimalAttributes(function () { - $user = (new BasicModel([ - 'id' => 'user-id', - 'name' => 'user-name', - 'location' => 'Melbourne', - ])); - Route::get('test-route', fn () => UserResource::make($user)); + JsonApiResource::minimalAttributes(); + $user = (new BasicModel([ + 'id' => 'user-id', + 'name' => 'user-name', + 'location' => 'Melbourne', + ])); + Route::get('test-route', fn () => UserResource::make($user)); - $response = $this->getJson('test-route?fields[basicModels]=name'); + $response = $this->getJson('test-route?fields[basicModels]=name'); - $response->assertOk(); - $response->assertExactJson([ - 'data' => [ - 'type' => 'basicModels', - 'id' => 'user-id', - 'attributes' => [ - 'name' => 'user-name', - ], - ], - 'jsonapi' => [ - 'version' => '1.0', + $response->assertOk(); + $response->assertExactJson([ + 'data' => [ + 'type' => 'basicModels', + 'id' => 'user-id', + 'attributes' => [ + 'name' => 'user-name', ], - ]); - $this->assertValidJsonApi($response); - }); + ], + 'jsonapi' => [ + 'version' => '1.0', + ], + ]); + $this->assertValidJsonApi($response); } public function testItCanUseSparseFieldsetsWithIncludedCollections(): void diff --git a/tests/TestCase.php b/tests/TestCase.php index 34205b5..c0c51d6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -29,6 +29,7 @@ protected function tearDown(): void parent::tearDown(); JsonApiResource::resolveServerImplementationNormally(); + JsonApiResource::maximalAttributes(); } protected function assertValidJsonApi(TestResponse|string|array $data): void