diff --git a/tests/Controllers/Index/IndexRelatedFeatureTest.php b/tests/Controllers/Index/IndexRelatedFeatureTest.php index 78e659df..50bf2874 100644 --- a/tests/Controllers/Index/IndexRelatedFeatureTest.php +++ b/tests/Controllers/Index/IndexRelatedFeatureTest.php @@ -33,7 +33,7 @@ protected function setUp(): void { parent::setUp(); - $this->app->singletonIf(RelatedDto::class, fn ($app) => new RelatedDto()); + $this->app->singletonIf(RelatedDto::class, fn($app) => new RelatedDto()); } public function test_can_retrieve_nested_relationships(): void @@ -43,7 +43,7 @@ public function test_can_retrieve_nested_relationships(): void ->andReturn([ 'owner', 'users' => HasMany::make('users', UserRepository::class), - 'extraData' => fn () => ['country' => 'Romania'], + 'extraData' => fn() => ['country' => 'Romania'], 'extraMeta' => new InvokableExtraMeta(), ]); @@ -70,7 +70,7 @@ public function test_can_retrieve_nested_relationships(): void $this->withoutExceptionHandling()->getJson(CompanyRepository::route(query: [ 'related' => 'users.companies.users, users.posts, users.roles, extraData, extraMeta, owner', ]))->assertJson( - fn (AssertableJson $json) => $json + fn(AssertableJson $json) => $json ->where('data.0.type', 'companies') ->has('data.0.relationships') ->has('data.0.relationships.users') @@ -110,7 +110,7 @@ public function test_can_load_nested_parent_from_the_same_table(): void $this->getJson(CommentRepository::route(query: [ 'related' => 'parent, children', ]))->assertJson( - fn (AssertableJson $json) => $json + fn(AssertableJson $json) => $json ->where('data.2.attributes.comment', 'Root comment') ->has('data.2.relationships.parent') ->missing('data.2.relationships.parent.relationships.parent') @@ -147,7 +147,7 @@ public function test_index_related_doesnt_load_for_nested_relationships_that_did $this->getJson(CommentRepository::route(query: [ 'related' => 'user, post.user', ]))->assertJson( - fn (AssertableJson $json) => $json + fn(AssertableJson $json) => $json ->where('data.0.id', '2') ->has('data.0.relationships.user') ->has('data.0.relationships.post') @@ -164,7 +164,7 @@ public function test_index_related_doesnt_load_for_nested_relationships_that_did $this->getJson(CommentRepository::route(query: [ 'related' => 'user, post', ]))->assertJson( - fn (AssertableJson $json) => $json + fn(AssertableJson $json) => $json ->has('data.0.relationships.user') ->has('data.0.relationships.post') ->missing('data.0.relationships.post.relationships.user') @@ -191,7 +191,7 @@ public function test_repository_can_resolve_related_using_callables(): void $this->getJson(PostRepository::route(query: [ 'related' => 'user', ]))->assertJson( - fn (AssertableJson $json) => $json + fn(AssertableJson $json) => $json ->where('data.0.relationships.user', 'foo') ->etc() ); @@ -221,12 +221,37 @@ public function it_can_paginate_keeping_relationships(): void 'page' => 2, ])) ->assertJson( - fn (AssertableJson $json) => $json + fn(AssertableJson $json) => $json ->count('data', 1) ->where('data.0.relationships.user.name', $owner) ->etc() ); } + + /** * @test */ + public function it_will_call_fields_method_for_related(): void + { + UserRepository::partialMock() + ->shouldReceive('fields') + ->once() + ->andReturn([ + field('name') + ]); + + PostRepository::$related = [ + 'user' => BelongsTo::make('user', UserRepository::class), + ]; + + PostFactory::one(); + + $this->getJson(PostRepository::route(query: [ + 'related' => 'user', + ]))->assertJson( + fn(AssertableJson $json) => $json + ->has('data.0.relationships.user.attributes.name') + ->etc() + ); + } } class InvokableExtraMeta diff --git a/tests/Fixtures/User/UserRepository.php b/tests/Fixtures/User/UserRepository.php index eeec5a25..92a2d783 100644 --- a/tests/Fixtures/User/UserRepository.php +++ b/tests/Fixtures/User/UserRepository.php @@ -3,6 +3,9 @@ namespace Binaryk\LaravelRestify\Tests\Fixtures\User; use Binaryk\LaravelRestify\Contracts\RestifySearchable; +use Binaryk\LaravelRestify\Http\Requests\IndexRepositoryActionRequest; +use Binaryk\LaravelRestify\Http\Requests\RepositoryIndexRequest; +use Binaryk\LaravelRestify\Http\Requests\RepositoryShowRequest; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; use Binaryk\LaravelRestify\Repositories\Repository; use Binaryk\LaravelRestify\Repositories\UserProfile;