Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ protected static function resourceTypeFromModel(Model $model): string

$morphMap = Relation::getMorphAlias($modelClassName);

return Str::of(
$morphMap !== $modelClassName ? $morphMap : class_basename($modelClassName)
)->snake()->pluralStudly();
if ($morphMap !== $modelClassName) {
return Str::of($morphMap)->pluralStudly();
}

return Str::of($modelClassName)->classBasename()->snake()->pluralStudly();
}
}
74 changes: 74 additions & 0 deletions tests/Http/Resources/JsonApi/JsonApiResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,40 @@
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
{
protected function setUp(): void
{
Relation::morphMap(['json-api-model' => JsonApiModel::class]);

$container = Container::setInstance(new Container);
$container->instance(ResponseFactoryContract::class, new ResponseFactory(
m::mock(ViewFactory::class),
m::mock(Redirector::class)
));
}

protected function tearDown(): void
{
JsonResource::flushState();
JsonApiResource::flushState();
Relation::morphMap([], false);

Container::setInstance(null);
m::close();
}

public function testResponseWrapperIsHardCodedToData()
Expand All @@ -37,4 +61,54 @@ public function testUnableToUnsetWrapper()

JsonApiResource::withoutWrapping();
}

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

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

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

public function testIncludedResourceDoesNotContainPrimaryKey()
{
$model = new JsonApiModel(['id' => 1, 'name' => 'User']);
$model->setRelation('manager', new JsonApiModel(['id' => 2, 'name' => 'Manager']));
$model->setRelation('deputy', new JsonApiModel(['id' => 2, 'email' => 'deputy@example.com']));

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

public function testIncludedMatchingResourceAttributesAreMerged()
{
$model = new JsonApiModel(['id' => 1, 'name' => 'User']);
$model->setRelation('manager', new JsonApiModel(['id' => 2, 'name' => 'Manager']));
$model->setRelation('deputy', new JsonApiModel(['id' => 2, 'email' => 'deputy@example.com']));

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

$this->assertEquals([
'id' => '2',
'type' => 'json-api-models',
'attributes' => [
'name' => 'Manager',
'email' => 'deputy@example.com',
],
], $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 fakeJsonApiResponseForModel(Model $model): array
{
return (new JsonApiResource($model))
->toResponse(new Request)
->getData(true);
}
}

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