Skip to content
Draft
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions tests/Http/Resources/JsonApi/JsonApiResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
namespace Illuminate\Tests\Http\Resources\JsonApi;

use BadMethodCallException;
use Illuminate\Container\Container;
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\JsonApi\JsonApiResource;
use Illuminate\Routing\Redirector;
use Illuminate\Routing\ResponseFactory;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class JsonApiResourceTest extends TestCase
Expand All @@ -13,6 +22,8 @@ protected function tearDown(): void
{
JsonResource::flushState();
JsonApiResource::flushState();
Container::getInstance()->flush();
Relation::morphMap([], false);
}

public function testResponseWrapperIsHardCodedToData()
Expand All @@ -37,4 +48,62 @@ public function testUnableToUnsetWrapper()

JsonApiResource::withoutWrapping();
}

public function testResourceTypeIsPickedFromMorph()
{
Relation::morphMap([JsonApiModel::class => 'json-api-model']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CleanShot 2025-10-31 at 11 57 41@2x

Should it be ['json-api-model' => JsonApiModel::class]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah, that's what I get for doing it during work break. Will get back to this in a bit 👍

$model = new JsonApiModel(['id' => 1, 'name' => 'User']);

$responseData = $this->modelToJsonApiData($model)['data'];

$this->assertArrayHasKey('type', $responseData);
$this->assertSame('json-api-model', $responseData['type']);
}

public function testIncludedResourceDoesNotContainPrimaryKey()
{
Relation::morphMap([JsonApiModel::class => 'json-api-model']);
$model = new JsonApiModel(['id' => 1, 'name' => 'User']);
$model->setRelation('manager', new JsonApiModel(['id' => 2, 'name' => 'Manager']));
$model->setRelation('deputy', new JsonApiModel(['id' => 2, 'email' => '[email protected]']));

$responseData = $this->modelToJsonApiData($model);
$this->assertArrayNotHasKey('id', $responseData['included'][0]['attributes']);
}

public function testIncludedMatchingResourceAttributesAreMerged()
{
Relation::morphMap([JsonApiModel::class => 'json-api-model']);
$model = new JsonApiModel(['id' => 1, 'name' => 'User']);
$model->setRelation('manager', new JsonApiModel(['id' => 2, 'name' => 'Manager']));
$model->setRelation('deputy', new JsonApiModel(['id' => 2, 'email' => '[email protected]']));

$responseData = $this->modelToJsonApiData($model);

$this->assertEquals([
'id' => '2',
'type' => 'json-api-model',
'attributes' => [
'name' => 'Manager',
'email' => '[email protected]',
],
], $responseData['included'][0]);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timacdonald need your input on this and how it should be affected, especially when the resource (e.g., UserResource) needs id, name, and email values to be available, but the relationship only loads partial values.


protected function modelToJsonApiData(Model $model): array
{
Container::getInstance()->instance(ResponseFactoryContract::class, new ResponseFactory(
m::mock(ViewFactory::class),
m::mock(Redirector::class)
));

return (new JsonApiResource($model))
->toResponse(new Request)
->getData(true);
}
}

class JsonApiModel extends Model
{
protected $guarded = [];
}
Loading