Files
ednotif-bundle/tests/Unit/Bovin/Mapper/InventoryMapperTest.php

177 lines
7.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Tests\Unit\Bovin\Mapper;
use DateTimeImmutable;
use Malio\EdnotifBundle\Bovin\Dto\InventoryDto;
use Malio\EdnotifBundle\Bovin\Mapper\AnimalSummaryMapper;
use Malio\EdnotifBundle\Bovin\Mapper\InventoryMapper;
use Malio\EdnotifBundle\Shared\Mapper\StandardResponseMapper;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use stdClass;
/**
* @internal
*/
#[CoversClass(InventoryMapper::class)]
final class InventoryMapperTest extends TestCase
{
public function testMapFullInventory(): void
{
$mapper = new InventoryMapper(new AnimalSummaryMapper(), new StandardResponseMapper());
$inventory = $mapper->map($this->makeSoapResponse(), $this->makeUnzippedMessage());
self::assertInstanceOf(InventoryDto::class, $inventory);
self::assertTrue($inventory->standardResponse->result);
self::assertSame(2, $inventory->nbBovins);
self::assertEquals(new DateTimeImmutable('2026-01-01'), $inventory->startDate);
self::assertEquals(new DateTimeImmutable('2026-01-31'), $inventory->endDate);
self::assertTrue($inventory->includesEarTagStock);
self::assertCount(2, $inventory->animals);
self::assertCount(1, $inventory->earTagSeries);
self::assertSame('FR123', $inventory->animals[0]->identification?->bovin?->nationalNumber);
}
public function testMapInventoryWithoutMessageZipReturnsEmptyLists(): void
{
$mapper = new InventoryMapper(new AnimalSummaryMapper(), new StandardResponseMapper());
$soapResponse = new stdClass();
$soapResponse->ReponseStandard = new stdClass();
$soapResponse->ReponseStandard->Resultat = true;
$soapResponse->ReponseSpecifique = new stdClass();
$soapResponse->ReponseSpecifique->NbBovins = 0;
$inventory = $mapper->map($soapResponse, null);
self::assertSame(0, $inventory->nbBovins);
self::assertSame([], $inventory->animals);
self::assertSame([], $inventory->earTagSeries);
self::assertNull($inventory->startDate);
}
public function testMapInventoryWithStockBouclesAbsentYieldsNullFlag(): void
{
$mapper = new InventoryMapper(new AnimalSummaryMapper(), new StandardResponseMapper());
$message = new stdClass();
$message->InformationsMessage = new stdClass();
$message->InformationsMessage->DateDebut = '2026-01-01';
// StockBoucles deliberately omitted
$inventory = $mapper->map($this->makeSoapResponse(), $message);
self::assertNull($inventory->includesEarTagStock);
}
public function testMapInventoryWithStockBouclesZeroYieldsFalseFlag(): void
{
$mapper = new InventoryMapper(new AnimalSummaryMapper(), new StandardResponseMapper());
$message = new stdClass();
$message->InformationsMessage = new stdClass();
$message->InformationsMessage->StockBoucles = '0';
$inventory = $mapper->map($this->makeSoapResponse(), $message);
self::assertFalse($inventory->includesEarTagStock);
}
public function testMapInventoryWithDateFinAbsentYieldsNullEndDate(): void
{
$mapper = new InventoryMapper(new AnimalSummaryMapper(), new StandardResponseMapper());
$message = new stdClass();
$message->InformationsMessage = new stdClass();
$message->InformationsMessage->DateDebut = '2026-01-01';
// DateFin deliberately omitted
$inventory = $mapper->map($this->makeSoapResponse(), $message);
self::assertNull($inventory->endDate);
}
public function testMapInventoryWithSerieBouclesAsListPreservesAllEntries(): void
{
$mapper = new InventoryMapper(new AnimalSummaryMapper(), new StandardResponseMapper());
$serie1 = new stdClass();
$serie1->NumeroSerieDebut = 'A0001';
$serie2 = new stdClass();
$serie2->NumeroSerieDebut = 'B0001';
$message = new stdClass();
$message->Boucles = new stdClass();
$message->Boucles->SerieBoucles = [$serie1, $serie2];
$inventory = $mapper->map($this->makeSoapResponse(), $message);
self::assertCount(2, $inventory->earTagSeries);
self::assertSame('A0001', $inventory->earTagSeries[0]->rawNode->NumeroSerieDebut);
self::assertSame('B0001', $inventory->earTagSeries[1]->rawNode->NumeroSerieDebut);
}
public function testMapInventoryWithMissingNbBovinsDefaultsToZero(): void
{
$mapper = new InventoryMapper(new AnimalSummaryMapper(), new StandardResponseMapper());
$soapResponse = new stdClass();
$soapResponse->ReponseStandard = new stdClass();
$soapResponse->ReponseStandard->Resultat = true;
$soapResponse->ReponseSpecifique = new stdClass();
// NbBovins deliberately omitted
$inventory = $mapper->map($soapResponse, null);
self::assertSame(0, $inventory->nbBovins);
}
private function makeSoapResponse(): object
{
$response = new stdClass();
$response->ReponseStandard = new stdClass();
$response->ReponseStandard->Resultat = true;
$response->ReponseSpecifique = new stdClass();
$response->ReponseSpecifique->NbBovins = 2;
return $response;
}
private function makeUnzippedMessage(): object
{
$message = new stdClass();
$message->InformationsMessage = new stdClass();
$message->InformationsMessage->DateDebut = '2026-01-01';
$message->InformationsMessage->DateFin = '2026-01-31';
$message->InformationsMessage->StockBoucles = '1';
$message->Bovins = new stdClass();
$message->Bovins->Bovin = [
$this->makeAnimalNode('FR123'),
$this->makeAnimalNode('FR456'),
];
$message->Boucles = new stdClass();
$message->Boucles->SerieBoucles = new stdClass();
$message->Boucles->SerieBoucles->NumeroSerieDebut = 'A0001';
return $message;
}
private function makeAnimalNode(string $nationalNumber): object
{
$node = new stdClass();
$node->IdentiteBovin = new stdClass();
$node->IdentiteBovin->Bovin = new stdClass();
$node->IdentiteBovin->Bovin->NumeroNational = $nationalNumber;
$node->PeriodesPresences = new stdClass();
$node->PeriodesPresences->PeriodePresence = new stdClass();
$node->PeriodesPresences->PeriodePresence->Entree = new stdClass();
$node->PeriodesPresences->PeriodePresence->Entree->DateEntree = '2025-05-01';
return $node;
}
}