ldc-arcadia-api/tests/Integration/Infra/Adapters/Directories/EloquentIncoSheetDirectoryTest.php

160 lines
5.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\Infra\Adapters\Directories;
use Domain\Models\Entities\Inco\IncoSheet;
use Domain\Models\ValueObjects\Brand\ArcadiaId;
use Domain\Models\ValueObjects\Brand\BrandCode;
use Domain\Models\ValueObjects\Inco\IncoId;
use Domain\Models\ValueObjects\Inco\IncoIdQuery;
use Domain\Models\ValueObjects\Inco\IncoSheetFilter;
use Domain\Models\ValueObjects\Inco\IncoSheetSortColumn;
use Domain\Models\ValueObjects\Inco\IncoSheetSorter;
use Domain\Ports\Repositories\BrandRepository;
use Domain\Services\BrandResolver;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Infra\Adapters\Directories\EloquentIncoSheetDirectory;
use Infra\Database\Models\ArcadiaBrand;
use Infra\Database\Models\IncoSheet as IncoSheetModel;
use Shared\Pagination;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
covers(EloquentIncoSheetDirectory::class);
describe('EloquentIncoSheetDirectory Integration Tests', function (): void {
beforeEach(function (): void {
IncoSheetModel::query()->delete();
ArcadiaBrand::query()->firstOrCreate(['code' => 1], ['libelle' => 'Le Gaulois']);
ArcadiaBrand::query()->firstOrCreate(['code' => 2], ['libelle' => 'Maître CoQ']);
});
it('finds an inco sheet by IncoId and resolves its IncoBrand', function (): void {
// GIVEN
$resolver = app(BrandResolver::class);
$repository = app(BrandRepository::class);
$directory = new EloquentIncoSheetDirectory($resolver, $repository);
IncoSheetModel::factory()->create([
'id' => '1000101',
'name' => 'Poulet Rôti',
'brand' => ['code' => 1, 'label' => 'Le Gaulois'],
]);
$incoId = new IncoId('1000101');
// WHEN
$result = $directory->find($incoId);
// THEN
expect($result)->toBeInstanceOf(IncoSheet::class)
->and((string) $result?->id->value)->toBe('1000101')
->and($result?->name)->toBe('Poulet Rôti')
->and($result?->brand->name->value)->toBe('Le Gaulois')
->and($result?->brand->arcadiaBrandCode->value)->toBe('1')
->and($result?->brand->code->value)->toMatch('/^[A-Z]{3}\d{2}$/');
});
it('returns null when inco sheet is not found', function (): void {
// GIVEN
$resolver = app(BrandResolver::class);
$repository = app(BrandRepository::class);
$directory = new EloquentIncoSheetDirectory($resolver, $repository);
// WHEN
$result = $directory->find(new IncoId('9999999'));
// THEN
expect($result)->toBeNull();
});
it('finds all inco sheets filtered by BrandCode', function (): void {
// GIVEN
$resolver = app(BrandResolver::class);
$repository = app(BrandRepository::class);
$directory = new EloquentIncoSheetDirectory($resolver, $repository);
$brand1 = $resolver->resolve(new ArcadiaId('1'));
$brand2 = $resolver->resolve(new ArcadiaId('2'));
IncoSheetModel::factory()->create([
'id' => '1000101',
'brand' => ['code' => 1, 'label' => 'Le Gaulois'],
]);
IncoSheetModel::factory()->create([
'id' => '1000102',
'brand' => ['code' => 2, 'label' => 'Maître CoQ'],
]);
$pagination = Pagination::ofPage(1, 10);
$sorter = new IncoSheetSorter(IncoSheetSortColumn::incoSheetId, \SortDirection::Ascending);
$filter = new IncoSheetFilter(null, $brand1->code);
// WHEN
$result = $directory->findAll($pagination, $sorter, $filter);
// THEN
expect($result->getTotal())->toBe(1)
->and($result->getItems())->toHaveCount(1)
->and((string) $result->getItems()[0]->id->value)->toBe('1000101');
});
it('searches inco sheets by id query and BrandCode', function (): void {
// GIVEN
$resolver = app(BrandResolver::class);
$repository = app(BrandRepository::class);
$directory = new EloquentIncoSheetDirectory($resolver, $repository);
$brand1 = $resolver->resolve(new ArcadiaId('1'));
IncoSheetModel::factory()->create([
'id' => '1234501',
'brand' => ['code' => 1, 'label' => 'Le Gaulois'],
]);
IncoSheetModel::factory()->create([
'id' => '1234502',
'brand' => ['code' => 2, 'label' => 'Maître CoQ'],
]);
IncoSheetModel::factory()->create([
'id' => '9999901',
'brand' => ['code' => 1, 'label' => 'Le Gaulois'],
]);
$query = new IncoIdQuery('1234%');
$pagination = Pagination::ofPage(1, 10);
// WHEN
$result = $directory->search($query, $pagination, $brand1->code);
// THEN
expect($result->getTotal())->toBe(1)
->and($result->getItems())->toHaveCount(1)
->and((string) $result->getItems()[0]->id->value)->toBe('1234501');
});
it('returns empty paginated result when filtering by non-existent BrandCode', function (): void {
// GIVEN
$resolver = app(BrandResolver::class);
$repository = app(BrandRepository::class);
$directory = new EloquentIncoSheetDirectory($resolver, $repository);
IncoSheetModel::factory()->create([
'id' => '1000101',
'brand' => ['code' => 1, 'label' => 'Le Gaulois'],
]);
$unknownCode = new BrandCode('ZZZ01');
$pagination = Pagination::ofPage(1, 10);
$sorter = new IncoSheetSorter(IncoSheetSortColumn::incoSheetId, \SortDirection::Ascending);
$filter = new IncoSheetFilter(null, $unknownCode);
// WHEN
$result = $directory->findAll($pagination, $sorter, $filter);
// THEN
expect($result->getTotal())->toBe(0)
->and($result->getItems())->toBeEmpty();
});
});