ldc-arcadia-api/tests/Feature/Api/V1/IncoSheets/IncoSheetsSearchTest.php
Jérémy Munsch 7383c3e01f
test(IncoSheets): update tests for short form inco sheets response
Enhance tests to verify the structure of inco sheets when the short parameter is true or numeric 1.
2026-08-14 15:57:58 +02:00

147 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\V1\IncoSheets;
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' => 'M01', 'label' => 'Standard'],
]);
IncoSheet::factory()->create([
'id' => '1000102',
'name' => 'Poulet Fermier',
'brand' => ['code' => 'M02', '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 brand', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
$query = urlencode('%10001%');
// WHEN
$response = $test->getJson("/api/v1/inco-sheets/search?incoIdLike={$query}&brands[]=M01");
// THEN
$response->assertStatus(200);
$response->assertJsonCount(1, 'incoSheets');
$response->assertJsonPath('incoSheets.0.id', '1000101');
});
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);
});
});