Skip to content

Commit

Permalink
Remove minimal attributes with closure
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Oct 14, 2023
1 parent 3b0b42a commit 7768fe5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 56 deletions.
17 changes: 2 additions & 15 deletions src/Concerns/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
80 changes: 39 additions & 41 deletions tests/Feature/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function tearDown(): void
parent::tearDown();

JsonApiResource::resolveServerImplementationNormally();
JsonApiResource::maximalAttributes();
}

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

0 comments on commit 7768fe5

Please sign in to comment.