-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[JSON:API] add tests for type, included merge and id in attributes #57589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: json-api-resource
Are you sure you want to change the base?
Changes from 2 commits
cbd49c0
8c927db
f43f79a
c39aa68
32fdd03
89f5e2d
a3e4aa3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -13,6 +22,8 @@ protected function tearDown(): void | |
| { | ||
| JsonResource::flushState(); | ||
| JsonApiResource::flushState(); | ||
| Container::getInstance()->flush(); | ||
| Relation::morphMap([], false); | ||
| } | ||
|
|
||
| public function testResponseWrapperIsHardCodedToData() | ||
|
|
@@ -37,4 +48,62 @@ public function testUnableToUnsetWrapper() | |
|
|
||
| JsonApiResource::withoutWrapping(); | ||
| } | ||
|
|
||
| public function testResourceTypeIsPickedFromMorph() | ||
| { | ||
| Relation::morphMap([JsonApiModel::class => 'json-api-model']); | ||
|
||
| $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]); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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., |
||
|
|
||
| protected function modelToJsonApiData(Model $model): array | ||
| { | ||
| Container::getInstance()->instance(ResponseFactoryContract::class, new ResponseFactory( | ||
| m::mock(ViewFactory::class), | ||
| m::mock(Redirector::class) | ||
| )); | ||
crynobone marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return (new JsonApiResource($model)) | ||
| ->toResponse(new Request) | ||
| ->getData(true); | ||
| } | ||
| } | ||
|
|
||
| class JsonApiModel extends Model | ||
| { | ||
| protected $guarded = []; | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.