Skip to content

Commit

Permalink
fix: test it will call fields method when related request
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryk committed Jan 5, 2024
1 parent 1d41e72 commit a884a2c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
41 changes: 33 additions & 8 deletions tests/Controllers/Index/IndexRelatedFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
]);

Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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()
);
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixtures/User/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a884a2c

Please sign in to comment.