test(mapper): add unit tests for Brand, Employee, Inco, IncoCompo and IncoShort mappers

This commit is contained in:
Jérémy Munsch 2026-09-16 12:12:16 +02:00
parent 4bce9ffc92
commit fbd5f5829f
Signed by: forge-joniko
GPG key ID: D7EFE2F5CB6CC0E4
6 changed files with 904 additions and 0 deletions

View file

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Mapper;
require_once __DIR__ . '/PaginationMetaForTests.php';
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Brand;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
use Uwil\ArcadiaApiPhpClient\Mapper\BrandMapper;
use Uwil\ArcadiaKiotaOpenApiClient\Models\Brand as KiotaBrand;
use Uwil\ArcadiaKiotaOpenApiClient\Models\BrandListResponse;
describe('BrandMapper test', function () {
it('can instanciate BrandMapper', function () {
// GIVEN / WHEN
$brandMapper = new BrandMapper();
// THEN
expect($brandMapper)->toBeInstanceOf(BrandMapper::class);
});
it('can map kiota model to a brand dto', function () {
// GIVEN
$kiotaModel = new KiotaBrand();
$kiotaModel->setName('Test Brand');
$kiotaModel->setCode('LGL39');
$kiotaModel->setArcadiaId('arcadia-id');
$kiotaModel->setCreatedAt(new \DateTime());
// WHEN
$brandDto = (new BrandMapper())->toDto($kiotaModel);
// THEN
expect($brandDto)->toBeInstanceOf(Brand::class);
expect($brandDto->label)->toBe('Test Brand');
expect($brandDto->code->value)->toBe('LGL39');
expect($brandDto->arcadiaId)->toBe('arcadia-id');
expect($brandDto->createdAt)->toBeInstanceOf(\DateTime::class);
});
it('can map all properties from kiota model to brand dto', function () {
// GIVEN
[
$pagination,
$paginationMeta,
] = generaterFirstPagePaginationMeta();
$response = new BrandListResponse();
$response->setBrands([]);
$response->setPagination($paginationMeta);
// WHEN
$brandPageDto = (new BrandMapper())->toPage($response, $pagination);
// THEN
expect($brandPageDto)->toBeInstanceOf(PageOf::class);
expect($brandPageDto->items())->toBeArray();
expect($brandPageDto->pagination())->toBeInstanceOf(PaginationDto::class);
});
});

View file

@ -0,0 +1,143 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Mapper;
require_once __DIR__ . '/PaginationMetaForTests.php';
use Microsoft\Kiota\Abstractions\Types\Date;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Employee;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ArcadiaEmployeeId;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\EstablishmentCode;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ServiceId;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\SiteId;
use Uwil\ArcadiaApiPhpClient\Mapper\EmployeeMapper;
use Uwil\ArcadiaKiotaOpenApiClient\Models\Employee as KiotaEmployee;
use Uwil\ArcadiaKiotaOpenApiClient\Models\EmployeeListResponse;
describe('EmployeeMapper test', function () {
it('can instanciate EmployeeMapper', function () {
// GIVEN / WHEN
$employeeMapper = new EmployeeMapper();
// THEN
expect($employeeMapper)->toBeInstanceOf(EmployeeMapper::class);
});
it('can map kiota model to an employee dto', function () {
// GIVEN
$kiotaModel = new KiotaEmployee();
$kiotaModel->setMatricule('1234567');
$kiotaModel->setLastName('Dupont');
$kiotaModel->setFirstName('Jean');
$kiotaModel->setStartDate(new Date('2024-01-15'));
$kiotaModel->setEndDate(new Date('2024-12-31'));
$kiotaModel->setPlannedEndDate(new Date('2025-06-30'));
$kiotaModel->setEstablishment('EST01');
$kiotaModel->setSite('LDCSER');
$kiotaModel->setService('097');
$kiotaModel->setCreatedAt(new \DateTime('2024-01-01 10:00:00'));
$kiotaModel->setUpdatedAt(new \DateTime('2024-06-01 12:00:00'));
// WHEN
$employeeDto = (new EmployeeMapper())->toDto($kiotaModel);
// THEN
expect($employeeDto)->toBeInstanceOf(Employee::class);
expect($employeeDto->matriculePublique)->toBeInstanceOf(ArcadiaEmployeeId::class);
expect($employeeDto->matriculePublique->value)->toBe('1234567');
expect($employeeDto->lastname)->toBe('Dupont');
expect($employeeDto->firstname)->toBe('Jean');
expect($employeeDto->start_date)->toBeInstanceOf(\DateTimeInterface::class);
expect($employeeDto->start_date->format('Y-m-d'))->toBe('2024-01-15');
expect($employeeDto->end_date)->toBeInstanceOf(\DateTimeInterface::class);
expect($employeeDto->end_date?->format('Y-m-d'))->toBe('2024-12-31');
expect($employeeDto->planned_end_date)->toBeInstanceOf(\DateTimeInterface::class);
expect($employeeDto->planned_end_date?->format('Y-m-d'))->toBe('2025-06-30');
expect($employeeDto->establishment)->toBeInstanceOf(EstablishmentCode::class);
expect($employeeDto->establishment->value)->toBe('EST01');
expect($employeeDto->site)->toBeInstanceOf(SiteId::class);
expect($employeeDto->site->value)->toBe('LDCSER');
expect($employeeDto->service)->toBeInstanceOf(ServiceId::class);
expect($employeeDto->service->value)->toBe('097');
expect($employeeDto->created_at)->toBeInstanceOf(\DateTimeInterface::class);
expect($employeeDto->created_at->format('Y-m-d H:i:s'))->toBe('2024-01-01 10:00:00');
expect($employeeDto->updated_at)->toBeInstanceOf(\DateTimeInterface::class);
expect($employeeDto->updated_at?->format('Y-m-d H:i:s'))->toBe('2024-06-01 12:00:00');
});
it('can map kiota model with null optional properties to an employee dto', function () {
// GIVEN
$kiotaModel = new KiotaEmployee();
$kiotaModel->setMatricule('12345678');
$kiotaModel->setEstablishment('EST01');
$kiotaModel->setSite('LDCSER');
$kiotaModel->setService('097');
// WHEN
$employeeDto = (new EmployeeMapper())->toDto($kiotaModel);
// THEN
expect($employeeDto)->toBeInstanceOf(Employee::class);
expect($employeeDto->matriculePublique->value)->toBe('12345678');
expect($employeeDto->lastname)->toBe('');
expect($employeeDto->firstname)->toBe('');
expect($employeeDto->start_date)->toBeInstanceOf(\DateTimeInterface::class);
expect($employeeDto->start_date->format('Y-m-d'))->toBe((new \DateTimeImmutable('today'))->format('Y-m-d'));
expect($employeeDto->end_date)->toBeNull();
expect($employeeDto->planned_end_date)->toBeNull();
expect($employeeDto->establishment->value)->toBe('EST01');
expect($employeeDto->site->value)->toBe('LDCSER');
expect($employeeDto->service->value)->toBe('097');
expect($employeeDto->created_at)->toBeInstanceOf(\DateTimeInterface::class);
expect($employeeDto->updated_at)->toBeNull();
});
it('can map all properties from kiota model to employee dto', function () {
// GIVEN
[
$pagination,
$paginationMeta,
] = generaterFirstPagePaginationMeta();
$response = new EmployeeListResponse();
$response->setEmployees([]);
$response->setPagination($paginationMeta);
// WHEN
$employeePageDto = (new EmployeeMapper())->toPage($response, $pagination);
// THEN
expect($employeePageDto)->toBeInstanceOf(PageOf::class);
expect($employeePageDto->items())->toBeArray();
expect($employeePageDto->pagination())->toBeInstanceOf(PaginationDto::class);
});
it('can map list response with items to page of employee dto', function () {
// GIVEN
[
$pagination,
$paginationMeta,
] = generaterFirstPagePaginationMeta();
$kiotaEmployee = new KiotaEmployee();
$kiotaEmployee->setMatricule('1234567');
$kiotaEmployee->setEstablishment('EST01');
$kiotaEmployee->setSite('LDCSER');
$kiotaEmployee->setService('097');
$response = new EmployeeListResponse();
$response->setEmployees([$kiotaEmployee]);
$response->setPagination($paginationMeta);
// WHEN
$employeePageDto = (new EmployeeMapper())->toPage($response, $pagination);
// THEN
expect($employeePageDto)->toBeInstanceOf(PageOf::class);
expect($employeePageDto->items())->toHaveCount(1);
expect($employeePageDto->items()[0])->toBeInstanceOf(Employee::class);
expect($employeePageDto->items()[0]->matriculePublique->value)->toBe('1234567');
});
});

View file

@ -0,0 +1,229 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Mapper;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\AcideGras;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\AcidesAmines;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\ApportsReference;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\ComposesSpeciaux;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\Energie;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\FruitLegumeNoix;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\GlucidesDetail;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\Identification;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\MacronutrimentsPortion;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\MacronutrimentsRef;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\Mineraux;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\NutriScore;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\PointsNutriScore;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\Vitamines;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoCompoId;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId;
use Uwil\ArcadiaApiPhpClient\Mapper\IncoCompoMapper;
use Uwil\ArcadiaKiotaOpenApiClient\Models\EnergyValues;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompo as KiotaIncoCompo;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompoAminoAcids;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompoEnergy;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompoMinerals;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompoNutriScore;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompoNutriScorePoints;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompositionIdentification;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompoSpecialCompounds;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompoVitamins;
describe('IncoCompoMapper test', function () {
it('can instanciate IncoCompoMapper', function () {
// GIVEN / WHEN
$mapper = new IncoCompoMapper();
// THEN
expect($mapper)->toBeInstanceOf(IncoCompoMapper::class);
});
it('can map full kiota model to an inco compo dto', function () {
// GIVEN
$kiotaModel = new KiotaIncoCompo();
$kiotaModel->setIncoCompoId('10');
$kiotaModel->setIncoId('20');
$ident = new IncoCompositionIdentification();
$ident->setName('Composition Poulet');
$ident->setNumber('COMP-001');
$ident->setSource('Laboratoire LDC');
$kiotaModel->setIdentification($ident);
$energy = new IncoCompoEnergy();
$ref = new EnergyValues();
$ref->setSize('100g');
$ref->setEnergyKj('650');
$ref->setEnergyKcal('155');
$energy->setReference($ref);
$portion = new EnergyValues();
$portion->setSize('150g');
$portion->setEnergyKj('975');
$portion->setEnergyKcal('232.5');
$energy->setPortion($portion);
$kiotaModel->setEnergy($energy);
$ns = new IncoCompoNutriScore();
$ns->setNegativeComponent('5');
$ns->setPositiveComponent('2');
$ns->setScore('3');
$ns->setGrade('B');
$kiotaModel->setNutriScore($ns);
$nsp = new IncoCompoNutriScorePoints();
$nsp->setEnergyPoints('1');
$nsp->setSaturatedFatPoints('1');
$nsp->setSugarsPoints('0');
$nsp->setSaltPoints('1');
$nsp->setFiberPoints('0');
$nsp->setProteinPoints('4');
$nsp->setFreshFnlPoints('0');
$kiotaModel->setNutriScorePoints($nsp);
$minerals = new IncoCompoMinerals();
$minerals->setCalcium('12');
$minerals->setIron('1.5');
$kiotaModel->setMinerals($minerals);
$vitamins = new IncoCompoVitamins();
$vitamins->setVitaminA('20');
$vitamins->setVitaminC('5');
$kiotaModel->setVitamins($vitamins);
$aa = new IncoCompoAminoAcids();
$aa->setEssentialAminoAcids('15');
$aa->setCaseins('0');
$aa->setWheyProtein('0');
$kiotaModel->setAminoAcids($aa);
$sc = new IncoCompoSpecialCompounds();
$sc->setCholine('30');
$sc->setCarnitine('10');
$sc->setTaurine('5');
$sc->setNucleotides('2');
$kiotaModel->setSpecialCompounds($sc);
// WHEN
$dto = (new IncoCompoMapper())->toDto($kiotaModel);
// THEN
expect($dto)->toBeInstanceOf(IncoCompo::class);
expect($dto->id)->toBeInstanceOf(IncoCompoId::class);
expect($dto->id->value)->toBe('10');
expect($dto->idInco)->toBeInstanceOf(IncoId::class);
expect($dto->idInco->value)->toBe('20');
// Identification
expect($dto->identification)->toBeInstanceOf(Identification::class);
expect($dto->identification->nom)->toBe('Composition Poulet');
expect($dto->identification->numero)->toBe('COMP-001');
expect($dto->identification->source)->toBe('Laboratoire LDC');
// Energie
expect($dto->energie)->toBeInstanceOf(Energie::class);
expect($dto->energie->tailleReference)->toBe('100g');
expect($dto->energie->valEnergKjReference)->toBe('650');
expect($dto->energie->valEnergKcalReference)->toBe('155');
expect($dto->energie->taillePortion)->toBe('150g');
expect($dto->energie->valEnergKjPortion)->toBe('975');
expect($dto->energie->valEnergKcalPortion)->toBe('232.5');
// NutriScore
expect($dto->nutriScore)->toBeInstanceOf(NutriScore::class);
expect($dto->nutriScore->composanteN)->toBe('5');
expect($dto->nutriScore->composanteP)->toBe('2');
expect($dto->nutriScore->scoreNutri)->toBe('3');
expect($dto->nutriScore->classement)->toBe('B');
// PointsNutriScore
expect($dto->pointsNutriScore)->toBeInstanceOf(PointsNutriScore::class);
expect($dto->pointsNutriScore->pointsEnergie)->toBe('1');
expect($dto->pointsNutriScore->pointsAcideGrasSature)->toBe('1');
expect($dto->pointsNutriScore->pointsProteines)->toBe('4');
// Mineraux & Vitamines
expect($dto->mineraux)->toBeInstanceOf(Mineraux::class);
expect($dto->mineraux->nutCa)->toBe('12');
expect($dto->mineraux->nutFe)->toBe('1.5');
expect($dto->vitamines)->toBeInstanceOf(Vitamines::class);
expect($dto->vitamines->nutVita)->toBe('20');
expect($dto->vitamines->nutVitc)->toBe('5');
// AcidesAmines & ComposesSpeciaux
expect($dto->acidesAmines)->toBeInstanceOf(AcidesAmines::class);
expect($dto->acidesAmines->nutAae)->toBe('15');
expect($dto->composesSpeciaux)->toBeInstanceOf(ComposesSpeciaux::class);
expect($dto->composesSpeciaux->nutCholn)->toBe('30');
// Sub-DTOs instantiated
expect($dto->macronutrimentsRef)->toBeInstanceOf(MacronutrimentsRef::class);
expect($dto->macronutrimentsPortion)->toBeInstanceOf(MacronutrimentsPortion::class);
expect($dto->fnl)->toBeInstanceOf(FruitLegumeNoix::class);
expect($dto->apportsReference)->toBeInstanceOf(ApportsReference::class);
expect($dto->glucidesDetail)->toBeInstanceOf(GlucidesDetail::class);
expect($dto->acideGras)->toBeInstanceOf(AcideGras::class);
});
it('can map minimal kiota model to an inco compo dto with default values', function () {
// GIVEN
$kiotaModel = new KiotaIncoCompo();
// WHEN
$dto = (new IncoCompoMapper())->toDto($kiotaModel);
// THEN
expect($dto)->toBeInstanceOf(IncoCompo::class);
expect($dto->id->value)->toBe('0');
expect($dto->idInco->value)->toBe('0');
expect($dto->identification->nom)->toBe('');
expect($dto->identification->numero)->toBe('');
expect($dto->identification->source)->toBe('');
expect($dto->energie->tailleReference)->toBe('');
expect($dto->energie->valEnergKjReference)->toBe('');
expect($dto->nutriScore->scoreNutri)->toBe('');
expect($dto->pointsNutriScore->pointsEnergie)->toBe('');
expect($dto->mineraux->nutCa)->toBe('');
expect($dto->vitamines->nutVita)->toBe('');
expect($dto->acidesAmines->nutAae)->toBe('');
expect($dto->composesSpeciaux->nutCholn)->toBe('');
});
it('verifies humanReadableValues and units on mapped compo parts', function () {
// GIVEN
$kiotaModel = new KiotaIncoCompo();
$energy = new IncoCompoEnergy();
$ref = new EnergyValues();
$ref->setSize('100');
$ref->setEnergyKj('650');
$ref->setEnergyKcal('155');
$energy->setReference($ref);
$kiotaModel->setEnergy($energy);
// WHEN
$dto = (new IncoCompoMapper())->toDto($kiotaModel);
// THEN
$humanValues = $dto->energie->humanReadableValues();
expect($humanValues)->toBeArray();
expect($humanValues)->toHaveKey('Taille de référence');
expect($humanValues['Taille de référence'])->toBe('100');
expect($humanValues)->toHaveKey('Énergie (kJ) / 100g');
expect($humanValues['Énergie (kJ) / 100g'])->toBe('650');
expect($humanValues)->toHaveKey('Énergie (kcal) / 100g');
expect($humanValues['Énergie (kcal) / 100g'])->toBe('155');
$units = Energie::units();
expect($units)->toBeArray();
expect($units['valEnergKjReference'])->toBe('kJ');
expect($units['valEnergKcalReference'])->toBe('kcal');
$humanUnits = Energie::humanReadableUnits();
expect($humanUnits['Énergie (kJ) / 100g'])->toBe('kJ');
expect($humanUnits['Énergie (kcal) / 100g'])->toBe('kcal');
});
});

View file

@ -0,0 +1,314 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Mapper;
require_once __DIR__ . '/PaginationMetaForTests.php';
use DateTime;
use DateTimeInterface;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Brand;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Additive;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Allegations;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Allergen;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\IncoWeight;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Ingredients;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Nutriscore;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\PortionAmount;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\PortionType;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\StorageCondition;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\BrandCode;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoCompoId;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId;
use Uwil\ArcadiaApiPhpClient\Mapper\IncoMapper;
use Uwil\ArcadiaKiotaOpenApiClient\Models\Inco as KiotaInco;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoAdditive;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoAllegations;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoBrand;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoIngredients;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse_incoSheets;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoNutriscore;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoNutriscore_grade;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoPortionAmount;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoStorageCondition;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoWeight as KiotaIncoWeight;
describe('IncoMapper test', function () {
it('can instanciate IncoMapper', function () {
// GIVEN / WHEN
$mapper = new IncoMapper();
// THEN
expect($mapper)->toBeInstanceOf(IncoMapper::class);
});
it('can map full kiota model to an inco dto', function () {
// GIVEN
$kiotaModel = new KiotaInco();
$kiotaModel->setIncoId('100');
$kiotaModel->setName('Poulet fermier');
$kiotaModel->setLabel('Poulet fermier Label Rouge');
$kiotaModel->setDescription('Poulet élevé en plein air 1.2kg');
$kiotaModel->setSellArguments('Origine France');
$kiotaModel->setPreparationAdvices('Cuire au four à 180°C pendant 1h15');
$kiotaModel->setTips('Arroser régulièrement');
$kiotaModel->setSellFamilly('Volaille');
$kiotaModel->setIsSeasonal(true);
$kiotaModel->setIncoCompoId('200');
$kiotaModel->setCreatedAt(new DateTime('2024-01-01 10:00:00'));
$kiotaModel->setUpdatedAt(new DateTime('2024-06-01 12:00:00'));
$brand = new IncoBrand();
$brand->setCode('LGL39');
$brand->setLabel('Le Gaulois');
$kiotaModel->setBrand($brand);
$allegations = new IncoAllegations();
$allegations->setAllegations(['Sans OGM']);
$allegations->setHealth(['Riche en protéines']);
$allegations->setNutrition(['Faible en matières grasses']);
$allegations->setOther(['Label Rouge']);
$kiotaModel->setAllegations($allegations);
$ingredients = new IncoIngredients();
$ingredients->setAdditionalData([
'groupe1' => ['Poulet fermier 98%', 'Sel 2%'],
]);
$kiotaModel->setIngredients($ingredients);
$portions = new IncoPortionAmount();
$portions->setLowPart(4);
$portions->setHighPart(6);
$portions->setApproximative(true);
$kiotaModel->setPortions($portions);
$kiotaModel->setAllergens(['Gluten', 'Soja']);
$weight = new KiotaIncoWeight();
$weight->setNetWeight(1200);
$weight->setGrossWeight(1350);
$weight->setTheoreticalAverage(1250);
$kiotaModel->setWeight($weight);
$nutriscore = new IncoNutriscore();
$nutriscore->setGrade(new IncoNutriscore_grade(IncoNutriscore_grade::A));
$kiotaModel->setNutriscore($nutriscore);
$storageCondition = new IncoStorageCondition();
$storageCondition->setInstructions('Conserver entre 0°C et 4°C');
$kiotaModel->setStorageCondition($storageCondition);
$additive = new IncoAdditive();
$additive->setCode('E300');
$additive->setLabel('Acide ascorbique');
$kiotaModel->setAdditives([$additive]);
// WHEN
$dto = (new IncoMapper())->toDto($kiotaModel);
// THEN
expect($dto)->toBeInstanceOf(Inco::class);
expect($dto->id)->toBeInstanceOf(IncoId::class);
expect($dto->id->value)->toBe('100');
expect($dto->name)->toBe('Poulet fermier');
expect($dto->libelle)->toBe('Poulet fermier Label Rouge');
expect($dto->description)->toBe('Poulet élevé en plein air 1.2kg');
expect($dto->sellArguments)->toBe('Origine France');
expect($dto->preparationAdvices)->toBe('Cuire au four à 180°C pendant 1h15');
expect($dto->tips)->toBe('Arroser régulièrement');
expect($dto->sellFamilly)->toBe('Volaille');
expect($dto->isSeasonal)->toBeTrue();
// Allegations
expect($dto->allegations)->toBeInstanceOf(Allegations::class);
expect($dto->allegations->allegations())->toBe(['Sans OGM']);
expect($dto->allegations->health())->toBe(['Riche en protéines']);
expect($dto->allegations->nutrition())->toBe(['Faible en matières grasses']);
expect($dto->allegations->other())->toBe(['Label Rouge']);
// Ingredients
expect($dto->ingredients)->toBeInstanceOf(Ingredients::class);
expect($dto->ingredients->groups)->toHaveKey('groupe1');
expect($dto->ingredients->groups['groupe1'])->toBe(['Poulet fermier 98%', 'Sel 2%']);
// Portions
expect($dto->portions)->toBeInstanceOf(PortionAmount::class);
expect($dto->portions->getLowValue())->toBe(4.0);
expect($dto->portions->getHighValue())->toBe(6.0);
expect($dto->portions->getType())->toBe(PortionType::approximative);
// Allergens
expect($dto->allergens)->toHaveCount(2);
expect($dto->allergens[0])->toBeInstanceOf(Allergen::class);
expect($dto->allergens[0]->code)->toBe(1);
expect($dto->allergens[0]->label)->toBe('Gluten');
expect($dto->allergens[1]->code)->toBe(2);
expect($dto->allergens[1]->label)->toBe('Soja');
// Brand
expect($dto->brand)->toBeInstanceOf(Brand::class);
expect($dto->brand->code)->toBeInstanceOf(BrandCode::class);
expect($dto->brand->code->value)->toBe('LGL39');
expect($dto->brand->label)->toBe('Le Gaulois');
// Weight
expect($dto->weight)->toBeInstanceOf(IncoWeight::class);
expect($dto->weight->netUvc)->toBe(1200);
expect($dto->weight->brutUvc)->toBe(1350);
expect($dto->weight->moyTheo)->toBe(1250);
// Nutriscore
expect($dto->nutriscore)->toBeInstanceOf(Nutriscore::class);
expect($dto->nutriscore->grade)->toBe('A');
// StorageCondition
expect($dto->storageCondition)->toBeInstanceOf(StorageCondition::class);
expect($dto->storageCondition->instructions)->toBe('Conserver entre 0°C et 4°C');
// Additives
expect($dto->additives)->toHaveCount(1);
expect($dto->additives[0])->toBeInstanceOf(Additive::class);
expect($dto->additives[0]->code)->toBe('E300');
expect($dto->additives[0]->label)->toBe('Acide ascorbique');
// Compo & Timestamps
expect($dto->compo)->toBeInstanceOf(IncoCompoId::class);
expect($dto->compo?->value)->toBe('200');
expect($dto->createdAt)->toBeInstanceOf(DateTimeInterface::class);
expect($dto->createdAt?->format('Y-m-d H:i:s'))->toBe('2024-01-01 10:00:00');
expect($dto->updatedAt)->toBeInstanceOf(DateTimeInterface::class);
expect($dto->updatedAt?->format('Y-m-d H:i:s'))->toBe('2024-06-01 12:00:00');
});
it('can map minimal kiota model to an inco dto with default/null values', function () {
// GIVEN
$kiotaModel = new KiotaInco();
$brand = new IncoBrand();
$brand->setCode('LGL39');
$kiotaModel->setBrand($brand);
// WHEN
$dto = (new IncoMapper())->toDto($kiotaModel);
// THEN
expect($dto)->toBeInstanceOf(Inco::class);
expect($dto->id->value)->toBe('0');
expect($dto->name)->toBe('');
expect($dto->libelle)->toBe('');
expect($dto->description)->toBe('');
expect($dto->sellArguments)->toBe('');
expect($dto->preparationAdvices)->toBe('');
expect($dto->tips)->toBeNull();
expect($dto->sellFamilly)->toBeNull();
expect($dto->isSeasonal)->toBeFalse();
expect($dto->allegations->allegations())->toBe([]);
expect($dto->ingredients->groups)->toBe([]);
expect($dto->portions)->toBeNull();
expect($dto->allergens)->toBe([]);
expect($dto->brand->code->value)->toBe('LGL39');
expect($dto->brand->label)->toBe('Marque');
expect($dto->weight)->toBeNull();
expect($dto->nutriscore)->toBeNull();
expect($dto->storageCondition)->toBeNull();
expect($dto->additives)->toBe([]);
expect($dto->compo)->toBeNull();
expect($dto->createdAt)->toBeNull();
expect($dto->updatedAt)->toBeNull();
});
it('handles null nutriscore grade gracefully', function () {
// GIVEN
$kiotaModel = new KiotaInco();
$brand = new IncoBrand();
$brand->setCode('LGL39');
$kiotaModel->setBrand($brand);
$nutriscore = new IncoNutriscore();
$nutriscore->setGrade(null);
$kiotaModel->setNutriscore($nutriscore);
// WHEN
$dto = (new IncoMapper())->toDto($kiotaModel);
// THEN
expect($dto->nutriscore)->toBeNull();
});
it('handles invalid additive gracefully', function () {
// GIVEN
$kiotaModel = new KiotaInco();
$brand = new IncoBrand();
$brand->setCode('LGL39');
$kiotaModel->setBrand($brand);
$invalidAdditive = new IncoAdditive();
$invalidAdditive->setCode('INVALID_E');
$invalidAdditive->setLabel('Invalid Additive');
$validAdditive = new IncoAdditive();
$validAdditive->setCode('E300');
$validAdditive->setLabel('Acide ascorbique');
$kiotaModel->setAdditives([$invalidAdditive, $validAdditive]);
// WHEN
$dto = (new IncoMapper())->toDto($kiotaModel);
// THEN
expect($dto->additives)->toHaveCount(1);
expect($dto->additives[0]->code)->toBe('E300');
});
it('can map all properties from kiota model to inco dto', function () {
// GIVEN
[
$pagination,
$paginationMeta,
] = generaterFirstPagePaginationMeta();
$response = new IncoListResponse();
$response->setIncoSheets([]);
$response->setPagination($paginationMeta);
// WHEN
$incoPageDto = (new IncoMapper())->toPage($response, $pagination);
// THEN
expect($incoPageDto)->toBeInstanceOf(PageOf::class);
expect($incoPageDto->items())->toBeArray();
expect($incoPageDto->pagination())->toBeInstanceOf(PaginationDto::class);
});
it('can map list response with items to page of inco dto', function () {
// GIVEN
[
$pagination,
$paginationMeta,
] = generaterFirstPagePaginationMeta();
$kiotaInco = new KiotaInco();
$kiotaInco->setIncoId('100');
$brand = new IncoBrand();
$brand->setCode('LGL39');
$kiotaInco->setBrand($brand);
$wrapper = new IncoListResponse_incoSheets();
$wrapper->setInco($kiotaInco);
$response = new IncoListResponse();
$response->setIncoSheets([$wrapper]);
$response->setPagination($paginationMeta);
// WHEN
$incoPageDto = (new IncoMapper())->toPage($response, $pagination);
// THEN
expect($incoPageDto)->toBeInstanceOf(PageOf::class);
expect($incoPageDto->items())->toHaveCount(1);
expect($incoPageDto->items()[0])->toBeInstanceOf(Inco::class);
expect($incoPageDto->items()[0]->id->value)->toBe('100');
});
});

View file

@ -0,0 +1,123 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Mapper;
require_once __DIR__ . '/PaginationMetaForTests.php';
use DateTime;
use DateTimeInterface;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoShort;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId;
use Uwil\ArcadiaApiPhpClient\Mapper\IncoShortMapper;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse_incoSheets;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoShort as KiotaIncoShort;
describe('IncoShortMapper test', function () {
it('can instanciate IncoShortMapper', function () {
// GIVEN / WHEN
$mapper = new IncoShortMapper();
// THEN
expect($mapper)->toBeInstanceOf(IncoShortMapper::class);
});
it('can map kiota model to an inco short dto', function () {
// GIVEN
$kiotaModel = new KiotaIncoShort();
$kiotaModel->setIncoId('123');
$kiotaModel->setName('Poulet rôti');
$kiotaModel->setLabel('Poulet rôti 1kg');
$kiotaModel->setDescription('Délicieux poulet rôti aux herbes');
$kiotaModel->setCreatedAt(new DateTime('2024-01-01 10:00:00'));
$kiotaModel->setUpdatedAt(new DateTime('2024-06-01 12:00:00'));
// WHEN
$incoShortDto = (new IncoShortMapper())->toDto($kiotaModel);
// THEN
expect($incoShortDto)->toBeInstanceOf(IncoShort::class);
expect($incoShortDto->id)->toBeInstanceOf(IncoId::class);
expect($incoShortDto->id->value)->toBe('123');
expect($incoShortDto->name)->toBe('Poulet rôti');
expect($incoShortDto->libelle)->toBe('Poulet rôti 1kg');
expect($incoShortDto->description)->toBe('Délicieux poulet rôti aux herbes');
expect($incoShortDto->created_at)->toBeInstanceOf(DateTimeInterface::class);
expect($incoShortDto->created_at?->format('Y-m-d H:i:s'))->toBe('2024-01-01 10:00:00');
expect($incoShortDto->updated_at)->toBeInstanceOf(DateTimeInterface::class);
expect($incoShortDto->updated_at?->format('Y-m-d H:i:s'))->toBe('2024-06-01 12:00:00');
});
it('can map kiota model with null optional properties to an inco short dto', function () {
// GIVEN
$kiotaModel = new KiotaIncoShort();
// WHEN
$incoShortDto = (new IncoShortMapper())->toDto($kiotaModel);
// THEN
expect($incoShortDto)->toBeInstanceOf(IncoShort::class);
expect($incoShortDto->id)->toBeInstanceOf(IncoId::class);
expect($incoShortDto->id->value)->toBe('0');
expect($incoShortDto->name)->toBe('');
expect($incoShortDto->libelle)->toBe('');
expect($incoShortDto->description)->toBe('');
expect($incoShortDto->created_at)->toBeNull();
expect($incoShortDto->updated_at)->toBeNull();
});
it('can map all properties from kiota model to inco short dto', function () {
// GIVEN
[
$pagination,
$paginationMeta,
] = generaterFirstPagePaginationMeta();
$response = new IncoListResponse();
$response->setIncoSheets([]);
$response->setPagination($paginationMeta);
// WHEN
$incoShortPageDto = (new IncoShortMapper())->toPage($response, $pagination);
// THEN
expect($incoShortPageDto)->toBeInstanceOf(PageOf::class);
expect($incoShortPageDto->items())->toBeArray();
expect($incoShortPageDto->pagination())->toBeInstanceOf(PaginationDto::class);
});
it('can map list response with items to page of inco short dto', function () {
// GIVEN
[
$pagination,
$paginationMeta,
] = generaterFirstPagePaginationMeta();
$kiotaIncoShort = new KiotaIncoShort();
$kiotaIncoShort->setIncoId('456');
$kiotaIncoShort->setName('Nuggets');
$kiotaIncoShort->setLabel('Nuggets de poulet');
$kiotaIncoShort->setDescription('Nuggets croustillants');
$wrapper = new IncoListResponse_incoSheets();
$wrapper->setIncoShort($kiotaIncoShort);
$response = new IncoListResponse();
$response->setIncoSheets([$wrapper]);
$response->setPagination($paginationMeta);
// WHEN
$incoShortPageDto = (new IncoShortMapper())->toPage($response, $pagination);
// THEN
expect($incoShortPageDto)->toBeInstanceOf(PageOf::class);
expect($incoShortPageDto->items())->toHaveCount(1);
expect($incoShortPageDto->items()[0])->toBeInstanceOf(IncoShort::class);
expect($incoShortPageDto->items()[0]->id->value)->toBe('456');
expect($incoShortPageDto->items()[0]->name)->toBe('Nuggets');
expect($incoShortPageDto->items()[0]->libelle)->toBe('Nuggets de poulet');
expect($incoShortPageDto->items()[0]->description)->toBe('Nuggets croustillants');
});
});

View file

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Mapper;
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\Pagination;
use Uwil\ArcadiaKiotaOpenApiClient\Models\PaginationLinks;
use Uwil\ArcadiaKiotaOpenApiClient\Models\PaginationMeta;
/** @return array{0: Pagination, 1: PaginationMeta} */
function generaterFirstPagePaginationMeta(): array
{
$pagination = Pagination::firstPage();
$paginationLinks = new PaginationLinks();
$paginationLinks->setFirst('first-link');
$paginationLinks->setLast('last-link');
$paginationLinks->setPrev('prev-link');
$paginationLinks->setNext('next-link');
$paginationLinks->setEscapedSelf('self-link');
$paginationMeta = new PaginationMeta();
$paginationMeta->setLinks($paginationLinks);
$paginationMeta->setPage(1);
$paginationMeta->setPageSize(0);
$paginationMeta->setTotalItems(0);
$paginationMeta->setTotalPages(0);
return [
$pagination,
$paginationMeta,
];
}