feat : DTO et mapper pour un bovin résumé (identification + présences)
This commit is contained in:
16
src/Bovin/Dto/AnimalSummaryDto.php
Normal file
16
src/Bovin/Dto/AnimalSummaryDto.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Dto;
|
||||
|
||||
final readonly class AnimalSummaryDto
|
||||
{
|
||||
/**
|
||||
* @param list<PresencePeriodDto> $presencePeriods
|
||||
*/
|
||||
public function __construct(
|
||||
public ?BovinIdentificationDto $identification,
|
||||
public array $presencePeriods,
|
||||
) {}
|
||||
}
|
||||
32
src/Bovin/Mapper/AnimalSummaryMapper.php
Normal file
32
src/Bovin/Mapper/AnimalSummaryMapper.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Mapper;
|
||||
|
||||
use Malio\EdnotifBundle\Bovin\Dto\AnimalSummaryDto;
|
||||
|
||||
final class AnimalSummaryMapper
|
||||
{
|
||||
use BovinNodeMappingTrait;
|
||||
|
||||
public function map(object $bovinNode): AnimalSummaryDto
|
||||
{
|
||||
$identificationNode = $bovinNode->IdentiteBovin ?? null;
|
||||
$identification = is_object($identificationNode) ? $this->mapIdentification($identificationNode) : null;
|
||||
|
||||
$presencePeriods = [];
|
||||
$presencePeriodsNode = $bovinNode->PeriodesPresences->PeriodePresence ?? null;
|
||||
foreach ($this->normalizeToList($presencePeriodsNode) as $presencePeriodNode) {
|
||||
if (!is_object($presencePeriodNode)) {
|
||||
continue;
|
||||
}
|
||||
$presencePeriods[] = $this->mapPresencePeriod($presencePeriodNode);
|
||||
}
|
||||
|
||||
return new AnimalSummaryDto(
|
||||
identification: $identification,
|
||||
presencePeriods: $presencePeriods,
|
||||
);
|
||||
}
|
||||
}
|
||||
70
tests/Unit/Bovin/Mapper/AnimalSummaryMapperTest.php
Normal file
70
tests/Unit/Bovin/Mapper/AnimalSummaryMapperTest.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Tests\Unit\Bovin\Mapper;
|
||||
|
||||
use Malio\EdnotifBundle\Bovin\Dto\AnimalSummaryDto;
|
||||
use Malio\EdnotifBundle\Bovin\Mapper\AnimalSummaryMapper;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
#[CoversClass(AnimalSummaryMapper::class)]
|
||||
final class AnimalSummaryMapperTest extends TestCase
|
||||
{
|
||||
public function testMapReturnsIdentificationAndPresencePeriods(): void
|
||||
{
|
||||
$node = $this->makeBovinNode();
|
||||
|
||||
$summary = new AnimalSummaryMapper()->map($node);
|
||||
|
||||
self::assertInstanceOf(AnimalSummaryDto::class, $summary);
|
||||
self::assertNotNull($summary->identification);
|
||||
self::assertSame('FR1234567890', $summary->identification->bovin?->nationalNumber);
|
||||
self::assertSame('F', $summary->identification->sex);
|
||||
self::assertCount(2, $summary->presencePeriods);
|
||||
}
|
||||
|
||||
public function testMapHandlesMissingOptionalNodes(): void
|
||||
{
|
||||
$summary = new AnimalSummaryMapper()->map(new stdClass());
|
||||
|
||||
self::assertNull($summary->identification);
|
||||
self::assertSame([], $summary->presencePeriods);
|
||||
}
|
||||
|
||||
private function makeBovinNode(): object
|
||||
{
|
||||
$node = new stdClass();
|
||||
$node->IdentiteBovin = new stdClass();
|
||||
$node->IdentiteBovin->Sexe = 'F';
|
||||
$node->IdentiteBovin->Bovin = new stdClass();
|
||||
$node->IdentiteBovin->Bovin->CodePays = 'FR';
|
||||
$node->IdentiteBovin->Bovin->NumeroNational = 'FR1234567890';
|
||||
|
||||
$node->PeriodesPresences = new stdClass();
|
||||
$node->PeriodesPresences->PeriodePresence = [
|
||||
$this->makePresencePeriod('2024-01-10', '2024-06-01'),
|
||||
$this->makePresencePeriod('2024-06-02', null),
|
||||
];
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function makePresencePeriod(string $entryDate, ?string $exitDate): object
|
||||
{
|
||||
$period = new stdClass();
|
||||
$period->Entree = new stdClass();
|
||||
$period->Entree->DateEntree = $entryDate;
|
||||
if (null !== $exitDate) {
|
||||
$period->Sortie = new stdClass();
|
||||
$period->Sortie->DateSortie = $exitDate;
|
||||
}
|
||||
|
||||
return $period;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user