Skip to content

Commit

Permalink
Update RepositoryTest.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Nov 26, 2024
1 parent 53a77b6 commit 4279496
Showing 1 changed file with 63 additions and 20 deletions.
83 changes: 63 additions & 20 deletions tests/Unit/Abstracts/Repositories/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
Expand All @@ -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<int, User> $result */
Expand All @@ -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');
}
}

0 comments on commit 4279496

Please sign in to comment.