ldc-arcadia-api/tests/Feature/Api/V1/IncoSheets/IncoSheetsSearchTest.php

167 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\V1\IncoSheets;
use Domain\Models\ValueObjects\Brand\ArcadiaId;
use Domain\Services\BrandResolver;
use Infra\Database\Models\ArcadiaBrand;
use Infra\Database\Models\IncoSheet;
describe('IncoSheets Search Feature API', function (): void {
beforeEach(function (): void {
IncoSheet::factory()->create([
'id' => '1000101',
'name' => 'Poulet Rôti 1kg',
'brand' => ['code' => 1, 'label' => 'Standard'],
]);
IncoSheet::factory()->create([
'id' => '1000102',
'name' => 'Poulet Fermier',
'brand' => ['code' => 2, 'label' => 'Bio'],
]);
});
it('returns 200 OK for valid inco-sheets search request with incoIdLike pattern', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
$query = urlencode('%1000101%');
// WHEN
$response = $test->getJson("/api/v1/inco-sheets/search?incoIdLike={$query}");
// THEN
$response->assertStatus(200);
$response->assertJsonStructure([
'incoSheets',
'pagination' => [
'page',
'pageSize',
'totalItems',
'totalPages',
'links' => [
'self',
'first',
'prev',
'next',
'last',
],
],
]);
$response->assertJsonCount(1, 'incoSheets');
});
it('returns 200 OK with matching sheets filtered by BrandCode', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
$query = urlencode('%10001%');
ArcadiaBrand::query()->firstOrCreate(['code' => 1], ['libelle' => 'Standard']);
$brand = app(BrandResolver::class)->resolve(new ArcadiaId('1'));
// WHEN
$response = $test->getJson("/api/v1/inco-sheets/search?incoIdLike={$query}&brands[]={$brand->code->value}");
// THEN
$response->assertStatus(200);
$response->assertJsonCount(1, 'incoSheets');
$response->assertJsonPath('incoSheets.0.id', '1000101')
->assertJsonPath('incoSheets.0.brand.code', $brand->code->value);
});
it('returns 422 Unprocessable Entity when brand code format is invalid', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
$query = urlencode('%10001%');
// WHEN
$response = $test->getJson("/api/v1/inco-sheets/search?incoIdLike={$query}&brands[]=invalid");
// THEN
$response->assertStatus(422);
});
it('returns 200 OK with short form inco sheets when short parameter is true', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
$query = urlencode('%1000101%');
// WHEN
$response = $test->getJson("/api/v1/inco-sheets/search?incoIdLike={$query}&short=true");
// THEN
$response->assertStatus(200);
$response->assertJsonStructure([
'incoSheets' => [
'*' => [
'id',
'name',
'label',
'createdAt',
'updatedAt',
],
],
'pagination',
]);
$firstItem = $response->json('incoSheets.0');
expect($firstItem)
->toHaveKeys(['id', 'name', 'label', 'createdAt', 'updatedAt'])
->and($firstItem)->not->toHaveKey('description')
->and($firstItem)->not->toHaveKey('allegations')
->and($firstItem)->not->toHaveKey('ingredients')
->and($firstItem)->not->toHaveKey('brand');
});
it('returns 200 OK with short form inco sheets when short parameter is numeric 1', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
$query = urlencode('%10001%');
// WHEN
$response = $test->getJson("/api/v1/inco-sheets/search?incoIdLike={$query}&short=1");
// THEN
$response->assertStatus(200);
$response->assertJsonStructure([
'incoSheets' => [
'*' => [
'id',
'name',
'label',
'createdAt',
'updatedAt',
],
],
'pagination',
]);
$firstItem = $response->json('incoSheets.0');
expect($firstItem)
->toHaveKeys(['id', 'name', 'label', 'createdAt', 'updatedAt'])
->and($firstItem)->not->toHaveKey('description')
->and($firstItem)->not->toHaveKey('ingredients');
});
it('returns 200 OK with empty incoSheets list when no matching inco sheets found', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
$query = urlencode('%9999999%');
// WHEN
$response = $test->getJson("/api/v1/inco-sheets/search?incoIdLike={$query}");
// THEN
$response->assertStatus(200);
$response->assertJsonCount(0, 'incoSheets');
$response->assertJsonPath('pagination.totalItems', 0);
});
});