From 42794960e445794723f2261c06a2fe76d68ccb75 Mon Sep 17 00:00:00 2001 From: Mohammad Alavi Date: Tue, 26 Nov 2024 20:58:03 +0330 Subject: [PATCH] Update RepositoryTest.php --- .../Abstracts/Repositories/RepositoryTest.php | 83 ++++++++++++++----- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/tests/Unit/Abstracts/Repositories/RepositoryTest.php b/tests/Unit/Abstracts/Repositories/RepositoryTest.php index b7d8747e2..4172b052d 100644 --- a/tests/Unit/Abstracts/Repositories/RepositoryTest.php +++ b/tests/Unit/Abstracts/Repositories/RepositoryTest.php @@ -3,6 +3,8 @@ namespace Apiato\Core\Tests\Unit\Abstracts\Repositories; use Apiato\Core\Abstracts\Repositories\Repository; +use Apiato\Core\Tests\Infrastructure\Doubles\Book; +use Apiato\Core\Tests\Infrastructure\Doubles\BookFactory; use Apiato\Core\Tests\Infrastructure\Doubles\User; use Apiato\Core\Tests\Infrastructure\Doubles\UserFactory; use Apiato\Core\Tests\Infrastructure\Doubles\UserRepository; @@ -14,39 +16,74 @@ #[CoversClass(Repository::class)] final class RepositoryTest extends UnitTestCase { - protected function setUp(): void - { - parent::setUp(); - config()->set('apiato.requests.params.include', 'include'); - } - public static function includeDataProvider(): array { return [ - 'single relation' => ['parent', ['parent'], ['children']], - 'multiple relations' => ['parent,children', ['parent', 'children'], []], - 'single nested relation' => ['children.parent', ['children'], []], - 'multiple nested relations' => ['parent.children,children', ['parent', 'children'], []], - 'multiple and single nested relations' => ['parent.children,children.parent', ['parent', 'children'], []], + 'single relation' => [ + 'books', + ['books'], + [], + ['children', 'parent'], + ], + 'works with duplicate include' => [ + 'books,books', + ['books'], + [], + ['children', 'parent'], + ], + 'multiple relations' => [ + 'books,children', + ['books', 'children'], + [], + ['parent'], + ], + 'single nested relation' => [ + 'books.author', + ['books'], + ['author'], + ['children', 'parent'], + ], + 'multiple nested relations' => [ + 'books.author.children,children.parent', + ['books', 'children'], + ['author'], + ['parent'], + ], + 'multiple and single nested relations' => [ + 'parent,books.author', + ['parent', 'books'], + ['author'], + ['children'], + ], ]; } #[DataProvider('includeDataProvider')] - public function testEagerLoadSingleRelationRequestedViaRequest(string $include, array $mustLoadRelations, array $mustNotLoadRelations): void - { + public function testEagerLoadSingleRelationRequestedViaRequest( + string $include, + array $userMustLoadRelations, + array $booksMustLoadRelations, + array $mustNotLoadRelations, + ): void { request()->merge(compact('include')); - $parent = UserFactory::new()->has( - UserFactory::new()->count(3), - 'children', - )->count(3)->create(); + UserFactory::new() + ->has(UserFactory::new()->has(BookFactory::new())->count(3), 'children') + ->has(BookFactory::new())->count(3) + ->createOne(); + $repository = app(UserRepository::class); $result = $repository->all(); - $result->each(function (User $user) use ($mustLoadRelations, $mustNotLoadRelations) { - foreach ($mustLoadRelations as $relation) { + $result->each(function (User $user) use ($userMustLoadRelations, $booksMustLoadRelations, $mustNotLoadRelations) { + foreach ($userMustLoadRelations as $relation) { $this->assertTrue($user->relationLoaded($relation)); } + foreach ($booksMustLoadRelations as $relation) { + $user->books->each(function (Book $book) use ($relation) { + $this->assertTrue($book->relationLoaded($relation)); + }); + } foreach ($mustNotLoadRelations as $relation) { $this->assertFalse($user->relationLoaded($relation)); } @@ -56,7 +93,7 @@ public function testEagerLoadSingleRelationRequestedViaRequest(string $include, public function testMultipleEagerLoadAppliesAllEagerLoads(): void { $parent = UserFactory::new()->createOne(); - $children = UserFactory::new()->count(3)->create(['parent_id' => $parent->id]); + UserFactory::new()->count(3)->create(['parent_id' => $parent->id]); $repository = app(UserRepository::class); /** @var Collection $result */ @@ -67,4 +104,10 @@ public function testMultipleEagerLoadAppliesAllEagerLoads(): void $this->assertTrue($user->relationLoaded('children')); }); } + + protected function setUp(): void + { + parent::setUp(); + config()->set('apiato.requests.params.include', 'include'); + } }