Skip to content

Commit

Permalink
fixes and tests for JsonApiResponse::toArray method
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Oct 6, 2022
1 parent 0cb272c commit f927708
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
23 changes: 22 additions & 1 deletion src/Http/JsonApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Traits\ForwardsCalls;
use OpenSoutheners\LaravelApiable\Contracts\ViewableBuilder;
use OpenSoutheners\LaravelApiable\Contracts\ViewQueryable;
use OpenSoutheners\LaravelApiable\Http\Resources\JsonApiCollection;
use OpenSoutheners\LaravelApiable\Support\Facades\Apiable;
use function OpenSoutheners\LaravelHelpers\Classes\class_implement;

Expand Down Expand Up @@ -172,6 +173,10 @@ protected function getResults()
*/
public function toResponse($request)
{
if ($request->hasMacro('inertia') && $request->inertia()) {
return $this->toArray($request);
}

return $this->getResults()->toResponse($request);
}

Expand All @@ -182,7 +187,23 @@ public function toResponse($request)
*/
public function toArray()
{
return (array) $this->getResults();
$results = $this->getResults();

if (! ($results instanceof JsonApiCollection)) {
return $results->toArray($this->getRequest());
}

$responseArray = ['data' => $results->collection->map->toArray($this->getRequest())];

foreach (array_filter($results->with) as $key => $value) {
$responseArray[$key] = $value;
}

if (! empty($results->additional)) {
$responseArray = array_merge_recursive($responseArray, $results->additional);
}

return $responseArray;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Http/RequestQueryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ public function setQuery($query)
return $this;
}

/**
* Get the underlying request object.
*
* @return Request|null
*/
public function getRequest()
{
return $this->request;
}

/**
* Allows the following user operations.
*
Expand Down
12 changes: 1 addition & 11 deletions src/Http/Resources/Json/ResourceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,7 @@ public function count(): int
*/
public function toArray($request)
{
$responseArray = ['data' => $this->collection->map->toArray($request)];

foreach (array_filter($this->with) as $key => $value) {
$responseArray[$key] = $value;
}

if (! empty($this->additional)) {
$responseArray = array_merge($responseArray, array_filter($this->additional));
}

return $responseArray;
return $this->collection->map->toArray($request);
}

/**
Expand Down
54 changes: 54 additions & 0 deletions tests/Http/JsonApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OpenSoutheners\LaravelApiable\Tests\Http;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use OpenSoutheners\LaravelApiable\Http\AllowedAppends;
use OpenSoutheners\LaravelApiable\Http\AllowedFields;
Expand Down Expand Up @@ -294,4 +295,57 @@ public function testListPerformingFulltextSearch()
$assert->hasSize(1)->hasAttribute('title', 'Y esto en español');
});
}

public function testResponseAsArrayGetsAllContent()
{
// Yeah, we need to enforce this macro to "fake" Inertia so force toArray response behaviour
Request::macro('inertia', fn () => true);

config(['apiable.responses.include_allowed' => true]);

Route::get('/', function () {
return response()->json(JsonApiResponse::from(Post::with('tags'))->allowing([
AllowedFilter::exact('status', ['Active', 'Archived']),
]));
});

$response = $this->getJson('/');

$response->assertJsonCount(4, 'data');
$response->assertJsonCount(1, 'meta.allowed_filters');
$response->assertJsonFragment([
'allowed_filters' => [
'status' => [
'operator' => '=',
'values' => ['Active', 'Archived'],
],
],
]);
$response->assertJsonFragment([
'id' => '1',
'type' => 'post',
'relationships' => [
'tags' => [
'data' => [
[
'id' => '1',
'type' => 'label'
],
[
'id' => '3',
'type' => 'label'
],
[
'id' => '4',
'type' => 'label'
]
]
]
]
]);
$response->assertJsonFragment([
'id' => '1',
'type' => 'post',
]);
}
}

0 comments on commit f927708

Please sign in to comment.