Skip to content

Commit

Permalink
test: move test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Mar 1, 2025
1 parent 18adec7 commit a07717e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 80 deletions.
87 changes: 87 additions & 0 deletions tests/Functional/Core/Repositories/RepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

use Apiato\Core\Repositories\Repository;
use Illuminate\Support\Collection;
use Pest\Expectation;
use Workbench\App\Containers\Identity\User\Data\Repositories\UserRepository;
use Workbench\App\Containers\Identity\User\Models\User;
use Workbench\App\Containers\MySection\Book\Models\Book;

describe(class_basename(Repository::class), function (): void {
it('can eager load single relation include', function (
string $include,
array $userMustLoadRelations,
array $booksMustLoadRelations,
array $mustNotLoadRelations,
): void {
config(['fractal.auto_includes.request_key' => 'include']);
request()->merge(['include' => $include]);
User::factory()
->has(
User::factory()
->has(Book::factory(3)),
'children',
)->has(Book::factory(3))
->createOne();
$repository = new class extends UserRepository {
public function shouldEagerLoadIncludes(): bool
{
return true;
}
};

$result = $repository->all();

expect($result)->toBeInstanceOf(Collection::class)
->each(function (Expectation $expectation) use ($userMustLoadRelations, $booksMustLoadRelations, $mustNotLoadRelations): void {
foreach ($userMustLoadRelations as $relation) {
$expectation->relationLoaded($relation)->toBeTrue();
}
foreach ($booksMustLoadRelations as $relation) {
$expectation->books->each(function (Expectation $expectation) use ($relation): void {
$expectation->relationLoaded($relation)->toBeTrue();
});
}
foreach ($mustNotLoadRelations as $relation) {
$expectation->relationLoaded($relation)->toBeFalse();
}
});
})->with([
'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'],
],
]);
})->covers(Repository::class);
80 changes: 0 additions & 80 deletions tests/Unit/Core/Repositories/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,86 +16,6 @@

describe(class_basename(Repository::class), function (): void {
describe('eager loading request includes', function (): void {
beforeEach(function (): void {
config(['fractal.auto_includes.request_key' => 'include']);
});

it('can eager load single relation include', function (
string $include,
array $userMustLoadRelations,
array $booksMustLoadRelations,
array $mustNotLoadRelations,
): void {
request()->merge(['include' => $include]);
User::factory()
->has(
User::factory()
->has(Book::factory(3)),
'children',
)->has(Book::factory(3))
->createOne();
$repository = new class extends UserRepository {
public function shouldEagerLoadIncludes(): bool
{
return true;
}
};

$result = $repository->all();

expect($result)->toBeInstanceOf(Collection::class)
->each(function (Expectation $expectation) use ($userMustLoadRelations, $booksMustLoadRelations, $mustNotLoadRelations): void {
foreach ($userMustLoadRelations as $relation) {
$expectation->relationLoaded($relation)->toBeTrue();
}
foreach ($booksMustLoadRelations as $relation) {
$expectation->books->each(function (Expectation $expectation) use ($relation): void {
$expectation->relationLoaded($relation)->toBeTrue();
});
}
foreach ($mustNotLoadRelations as $relation) {
$expectation->relationLoaded($relation)->toBeFalse();
}
});
})->with([
'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'],
],
]);

it('can eager load multiple includes', function (): void {
User::factory()
->has(
Expand Down

0 comments on commit a07717e

Please sign in to comment.