[#ED-1] Ajout des API de lecture bovin #2

Merged
tristan merged 15 commits from feat/bovin-reads into develop 2026-04-21 08:14:37 +00:00
29 changed files with 3416 additions and 208 deletions

View File

@@ -33,3 +33,36 @@ Dans le docker-composer.yaml
volumes:
- ../ednotif-bundle:/var/www/html/ednotif-bundle
```
## Utilisation
Le bundle expose `Malio\EdnotifBundle\Bovin\Api\BovinApiInterface`. Injection standard par autowiring.
```php
use Malio\EdnotifBundle\Bovin\Api\BovinApiInterface;
final class MyController
{
public function __construct(private BovinApiInterface $ednotif) {}
public function example(): void
{
// Dossier d'un bovin
$file = $this->ednotif->getAnimalFile('FR1234567890');
// Inventaire du cheptel à une date
$inventory = $this->ednotif->getInventory(
startDate: new \DateTimeImmutable('2026-01-01'),
includeEarTagStock: true,
);
// Retours de notifications depuis une date
$returns = $this->ednotif->getReturnedDossiers(new \DateTimeImmutable('2026-03-01'));
// Sorties présumées par l'IPG (flux de rapprochement)
$presumed = $this->ednotif->getPresumedExits();
}
}
```
Toutes les méthodes lèvent `Malio\EdnotifBundle\Shared\Exception\EdnotifException` en cas de `Resultat=false` côté EDNOTIF.

View File

@@ -6,7 +6,13 @@ use Malio\EdnotifBundle\Auth\TokenProvider;
use Malio\EdnotifBundle\Bovin\Api\BovinApi;
use Malio\EdnotifBundle\Bovin\Api\BovinApiInterface;
use Malio\EdnotifBundle\Bovin\Mapper\AnimalFileMapper;
use Malio\EdnotifBundle\Bovin\Mapper\AnimalSummaryMapper;
use Malio\EdnotifBundle\Bovin\Mapper\InventoryMapper;
use Malio\EdnotifBundle\Bovin\Mapper\PresumedExitsMapper;
use Malio\EdnotifBundle\Bovin\Mapper\ReturnedDossiersMapper;
use Malio\EdnotifBundle\Shared\Mapper\StandardResponseMapper;
use Malio\EdnotifBundle\Shared\Soap\SoapClientFactory;
use Malio\EdnotifBundle\Shared\Soap\ZipMessageDecoder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
@@ -22,6 +28,8 @@ return static function (ContainerConfigurator $container): void {
->arg('$soapOptions', '%ednotif.soap_options%')
;
$services->set(StandardResponseMapper::class);
$services->set('ednotif.soap.guichet', SoapClient::class)
->factory([service(SoapClientFactory::class), 'create'])
->args(['%ednotif.guichet_wsdl%'])
@@ -32,7 +40,29 @@ return static function (ContainerConfigurator $container): void {
->args(['%ednotif.metier_wsdl%'])
;
$services->set(AnimalFileMapper::class);
$services->set(AnimalFileMapper::class)->args([service(StandardResponseMapper::class)]);
$services->set(ZipMessageDecoder::class);
$services->set(AnimalSummaryMapper::class);
$services->set(InventoryMapper::class)
->args([
service(AnimalSummaryMapper::class),
service(StandardResponseMapper::class),
])
;
$services->set(ReturnedDossiersMapper::class)
->args([
service(AnimalSummaryMapper::class),
service(StandardResponseMapper::class),
])
;
$services->set(PresumedExitsMapper::class)
->args([
service(StandardResponseMapper::class),
])
;
$services->set(TokenProvider::class)
->args([
@@ -52,6 +82,10 @@ return static function (ContainerConfigurator $container): void {
service(TokenProvider::class),
service('ednotif.soap.business'),
service(AnimalFileMapper::class),
service(InventoryMapper::class),
service(ReturnedDossiersMapper::class),
service(PresumedExitsMapper::class),
service(ZipMessageDecoder::class),
'%ednotif.exploitation_country_code%',
'%ednotif.exploitation_number%',
])

File diff suppressed because it is too large Load Diff

188
docs/ws-catalog.md Normal file
View File

@@ -0,0 +1,188 @@
# Catalogue des WebServices EDNOTIF
Inventaire des opérations exposées par les WSDL embarqués dans
`resources/ednotif-ws/`, avec le statut d'implémentation et une
recommandation de priorisation.
## Légende statut
- **Implémenté** — opération couverte par le bundle
- **À faire** — opération pertinente non encore implémentée
- **Optionnel** — opération hors périmètre probable, à confirmer selon le consommateur
---
## 1. wsIpBNotif — Notifications IPG Bovin
WS métier principal : déclarations réglementaires d'un cheptel bovin auprès de l'IPG.
### Lecture
| Opération | Statut | Description probable |
|---|---|---|
| `IpBGetDossierAnimal` | Implémenté | Dossier complet d'un bovin (identifications, mouvements, parents…) |
| `IpBGetInventaire` | Implémenté | Inventaire des animaux présents sur l'exploitation |
| `IpBGetRetourDossiers` | Implémenté | Retours de traitement des notifications envoyées |
| `IpBGetSortiesPresumees` | Implémenté | Animaux sortis selon l'IPG mais non déclarés par l'éleveur |
### Écriture
| Opération | Statut | Description probable |
|---|---|---|
| `IpBCreateEntree` | À faire | Déclaration d'entrée d'un bovin sur l'exploitation |
| `IpBCreateSortie` | À faire | Déclaration de sortie (vente, mort, abattage…) |
| `IpBCreateNaissance` | À faire | Déclaration de naissance |
| `IpBCreateMortNe` | À faire | Déclaration de mort-né |
| `IpBCreateAnimalEchange` | À faire | Échange intra-UE |
| `IpBCreateAnimalImporte` | À faire | Import pays tiers |
| `IpBCreateAvisAnimalImporte` | À faire | Avis d'import |
| `IpBCreateRebouclage` | À faire | Rebouclage / remplacement de boucle |
| `IpBCreateCommandeBoucles` | À faire | Commande de boucles |
| `IpBCreateInsemination` | À faire | Déclaration d'insémination |
---
## 2. wsDmB\* — Déclarations de Mouvement Bovin
Famille orientée gestion des mouvements / transporteurs.
### wsDmBConsultation
- `DmBConsultationGetListeDonneesIT` — consultation de données (IT = identifiants traces ?)
- `DmBConsultationGetListeStatutDeplacement` — statuts de déplacement
### wsDmBGestion
- `DmBGestionCreateDroitAccesListeAnimalActeur` — gestion droits d'accès
- `DmBGestionCreateListeICA` — création listes ICA (information chaîne alimentaire)
### wsDmBListe
- `DmBListeCreateListeBovins` / `DmBListeGetListeBovins` — listes de bovins (lots)
### wsDmBTransport
- `DmBTransportCreateChargement` — chargement camion
- `DmBTransportCreateDechargement` — déchargement camion
---
## 3. wsMdBEdel — Maîtrise des Données Bovin (Edel)
Consultation en lecture seule : génétique, lactation, IA, races.
| Opération | Rôle |
|---|---|
| `MdBGetDonneesGenetiquesAnimales` | Données génétiques d'animaux |
| `MdBGetDonneesMalesPublics` | Catalogue mâles reproducteurs publics |
| `MdBGetDonneesOrganismeHabilite` | Référentiel organismes habilités |
| `MdBGetDonneesOrganismeTiers` | Référentiel organismes tiers |
| `ClBGetDonneesCL` | Contrôle laitier |
| `CpBGetDonneesCPB` | Contrôle de performances bouchères |
| `IaBGetDonneesIA` | Données d'insémination |
| `OsBGetDonneesRAC` | Données race (RAC = race certifiée ?) |
| `TkBGetDonneesTE` | Transfert embryonnaire |
| `VaBGetDonneesCPV` | Contrôle performances veaux (?) |
---
## 4. wsMdCEdel — Maîtrise des Données Caprin
Équivalent caprin : CRUD + reproduction + lactation. 15 opérations (`MdCCreate*` pour l'écriture, `MdCGet*` avec variantes `MAJ` pour les deltas).
| Groupe | Opérations |
|---|---|
| Caprin | `MdCCreateCaprin`, `MdCGetDonneesCaprin`, `MdCGetDonneesCaprinMAJ` |
| Reproduction | `MdCCreateSaillie`, `MdCCreateFinGestation`, `MdCGetFinGestation[/MAJ]`, `MdCGetEvenementReproduction[/MAJ]` |
| Mouvement | `MdCCreateMouvement` |
| Contrôle laitier | `MdCGetCLDonneesBrutes[/MAJ]`, `MdCGetCLDonneesElaborees[/MAJ]` |
| Contrats | `MdCGetContratsExploitation` |
---
## 5. wsIpEdel — Identification Pérenne Edel
- `IpGetDonneesExploitation` — données descriptives de l'exploitation
---
## 6. wsMrAde — Échanges ICAR
Conforme aux standards ICAR (flux laitiers internationaux).
- `GetHerdList`
- `UpdateAnimal`
- `UpdateDevice`
- `UpdateLivestockLocation`
- `UpdateMilkingResults`
---
## 7. WsAnnuaire — Annuaire Guichet
Métadonnées techniques du guichet (pas du métier).
- `tkGetServices` — liste des WS disponibles
- `tkGetVersionsService` — versions d'un WS
- `tkGetOperationsServiceVersion` — opérations d'une version
- `tkGetUrl` — URL d'un service
---
## Recommandation de priorisation
Proposition d'ordre, à valider selon le périmètre réel du consommateur.
### Phase 1 — Compléter le bovin (priorité haute)
Continuer sur **wsIpBNotif**, en commençant par la **lecture** :
1. `IpBGetInventaire` — donne immédiatement la liste du cheptel, utile pour toute UI
2. `IpBGetRetourDossiers` — indispensable pour savoir si les notifs passent côté IPG
3. `IpBGetSortiesPresumees` — flux de rapprochement éleveur ↔ IPG
**Raison** : le dossier animal seul est peu utile sans l'inventaire qui permet de savoir *pour quels animaux* appeler `getAnimalFile`. Et sans `RetourDossiers`, toute écriture future est aveugle.
### Phase 2 — Écriture bovin (notifications obligatoires)
Implémenter les déclarations **dans l'ordre des cycles de vie d'un animal** :
4. `IpBCreateNaissance`
5. `IpBCreateEntree` / `IpBCreateSortie`
6. `IpBCreateMortNe`
7. `IpBCreateRebouclage` / `IpBCreateCommandeBoucles`
8. `IpBCreateAnimalEchange` / `IpBCreateAnimalImporte` / `IpBCreateAvisAnimalImporte` (si imports/échanges dans le périmètre)
9. `IpBCreateInsemination` (si non couvert par un autre outil)
### Phase 3 — Mouvements / transport
Si le consommateur gère du transport ou des lots :
10. `wsDmBListe` (lots bovins)
11. `wsDmBTransport` (chargement/déchargement)
12. `wsDmBConsultation` et `wsDmBGestion` selon besoin
### Phase 4 — Référentiels génétiques (optionnel)
Si le consommateur fait de la sélection / génétique :
13. `wsMdBEdel` — lectures ponctuelles, ne justifient une implémentation que s'il y a un usage métier concret
### Phase 5 — Caprin / ICAR (optionnel)
À activer uniquement si multi-espèces ou conformité ICAR requise.
### Hors priorité
- **wsIpEdel** : 1 op, à implémenter *en passant* si besoin ponctuel
- **WsAnnuaire** : utile pour du diagnostic / supervision, pas pour le métier
---
## Découpage structurel proposé
Pour garder un code cohérent, reproduire le pattern existant (`src/Bovin/`) par domaine :
```
src/
├── Auth/ (existant)
├── Bovin/ (IpBNotif + IpEdel exploitation)
├── Mouvement/ (DmB*)
├── Genetique/ (MdBEdel, optionnel)
├── Caprin/ (MdCEdel, optionnel)
├── Icar/ (MrAde, optionnel)
├── Annuaire/ (WsAnnuaire, optionnel)
└── Shared/ (existant)
```
Chaque domaine expose une `*ApiInterface` publique + une implémentation `readonly`, avec ses DTOs et mappers dédiés. Le `TokenProvider` et `SoapClientFactory` restent partagés via `Shared/`.

View File

@@ -86,4 +86,9 @@ php-cs-fixer-allow-risky:
$(EXEC_PHP_CS_FIXER) fix --config=.php-cs-fixer.dist.php --allow-risky=yes $(FILES)
wait:
sleep 10
sleep 10
# Lance la suite PHPUnit. Usage : make test (tout)
# make test FILES=<path> (un fichier/dossier)
test:
$(EXEC_PHP) php vendor/bin/phpunit $(FILES)

19
phpunit.xml.dist Normal file
View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
failOnWarning="true"
failOnRisky="true"
cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>

View File

@@ -4,10 +4,18 @@ declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Api;
use DateTimeInterface;
use Malio\EdnotifBundle\Auth\TokenProvider;
use Malio\EdnotifBundle\Bovin\Dto\AnimalFileDto;
use Malio\EdnotifBundle\Bovin\Dto\InventoryDto;
use Malio\EdnotifBundle\Bovin\Dto\PresumedExitsDto;
use Malio\EdnotifBundle\Bovin\Dto\ReturnedDossiersDto;
use Malio\EdnotifBundle\Bovin\Mapper\AnimalFileMapper;
use Malio\EdnotifBundle\Bovin\Mapper\InventoryMapper;
use Malio\EdnotifBundle\Bovin\Mapper\PresumedExitsMapper;
use Malio\EdnotifBundle\Bovin\Mapper\ReturnedDossiersMapper;
use Malio\EdnotifBundle\Shared\Exception\EdnotifException;
use Malio\EdnotifBundle\Shared\Soap\ZipMessageDecoder;
use RuntimeException;
use SoapClient;
use SoapFault;
@@ -18,6 +26,10 @@ final readonly class BovinApi implements BovinApiInterface
private TokenProvider $tokenProvider,
private SoapClient $businessClient,
private AnimalFileMapper $bovinDossierMapper,
private InventoryMapper $inventoryMapper,
private ReturnedDossiersMapper $returnedDossiersMapper,
private PresumedExitsMapper $presumedExitsMapper,
private ZipMessageDecoder $zipMessageDecoder,
private string $exploitationCountryCode,
private string $exploitationNumber,
) {}
@@ -45,20 +57,122 @@ final readonly class BovinApi implements BovinApiInterface
throw new RuntimeException('SOAP Fault on IpBGetDossierAnimal: '.$soapFault->getMessage(), 0, $soapFault);
}
// Throw uniquement si Resultat=false (erreur métier)
$standardResponseNode = $soapResponse->ReponseStandard ?? null;
$isOk = is_object($standardResponseNode) && (($standardResponseNode->Resultat ?? false) === true);
if (!$isOk) {
$anomalyNode = is_object($standardResponseNode) ? ($standardResponseNode->Anomalie ?? null) : null;
throw new EdnotifException(
codeAnomalie: (string) ($anomalyNode->Code ?? 'UNKNOWN'),
severite: (int) ($anomalyNode->Severite ?? 1),
message: (string) ($anomalyNode->Message ?? 'EDNOTIF error')
);
}
$this->assertSuccessfulResponse($soapResponse, 'IpBGetDossierAnimal');
return $this->bovinDossierMapper->map($soapResponse);
}
public function getInventory(
DateTimeInterface $startDate,
?DateTimeInterface $endDate = null,
bool $includeEarTagStock = false,
): InventoryDto {
$token = $this->tokenProvider->getToken();
$payload = [
'JetonAuthentification' => $token,
'Exploitation' => [
'CodePays' => $this->exploitationCountryCode,
'NumeroExploitation' => $this->exploitationNumber,
],
'DateDebut' => $startDate->format('Y-m-d'),
'StockBoucles' => $includeEarTagStock,
];
if (null !== $endDate) {
$payload['DateFin'] = $endDate->format('Y-m-d');
}
try {
/** @var object $soapResponse */
$soapResponse = $this->businessClient->__soapCall('IpBGetInventaire', [$payload]);
} catch (SoapFault $soapFault) {
throw new RuntimeException('SOAP Fault on IpBGetInventaire: '.$soapFault->getMessage(), 0, $soapFault);
}
$this->assertSuccessfulResponse($soapResponse, 'IpBGetInventaire');
$messageZip = $soapResponse->ReponseSpecifique->MessageZip ?? null;
$unzippedMessage = is_string($messageZip) && '' !== $messageZip
? $this->zipMessageDecoder->decode($messageZip)
: null;
return $this->inventoryMapper->map($soapResponse, $unzippedMessage);
}
public function getReturnedDossiers(DateTimeInterface $startDate): ReturnedDossiersDto
{
$token = $this->tokenProvider->getToken();
$payload = [
'JetonAuthentification' => $token,
'Exploitation' => [
'CodePays' => $this->exploitationCountryCode,
'NumeroExploitation' => $this->exploitationNumber,
],
'DateDebut' => $startDate->format('Y-m-d'),
];
try {
/** @var object $soapResponse */
$soapResponse = $this->businessClient->__soapCall('IpBGetRetourDossiers', [$payload]);
} catch (SoapFault $soapFault) {
throw new RuntimeException('SOAP Fault on IpBGetRetourDossiers: '.$soapFault->getMessage(), 0, $soapFault);
}
$this->assertSuccessfulResponse($soapResponse, 'IpBGetRetourDossiers');
$messageZip = $soapResponse->ReponseSpecifique->MessageZip ?? null;
$unzippedMessage = is_string($messageZip) && '' !== $messageZip
? $this->zipMessageDecoder->decode($messageZip)
: null;
return $this->returnedDossiersMapper->map($soapResponse, $unzippedMessage);
}
public function getPresumedExits(): PresumedExitsDto
{
$token = $this->tokenProvider->getToken();
$payload = [
'JetonAuthentification' => $token,
'Exploitation' => [
'CodePays' => $this->exploitationCountryCode,
'NumeroExploitation' => $this->exploitationNumber,
],
];
try {
/** @var object $soapResponse */
$soapResponse = $this->businessClient->__soapCall('IpBGetSortiesPresumees', [$payload]);
} catch (SoapFault $soapFault) {
throw new RuntimeException('SOAP Fault on IpBGetSortiesPresumees: '.$soapFault->getMessage(), 0, $soapFault);
}
$this->assertSuccessfulResponse($soapResponse, 'IpBGetSortiesPresumees');
$messageZip = $soapResponse->ReponseSpecifique->MessageZip ?? null;
$unzippedMessage = is_string($messageZip) && '' !== $messageZip
? $this->zipMessageDecoder->decode($messageZip)
: null;
return $this->presumedExitsMapper->map($soapResponse, $unzippedMessage);
}
private function assertSuccessfulResponse(object $soapResponse, string $operation): void
{
$standardResponseNode = $soapResponse->ReponseStandard ?? null;
$isOk = is_object($standardResponseNode) && (($standardResponseNode->Resultat ?? false) === true);
if ($isOk) {
return;
}
$anomalyNode = is_object($standardResponseNode) ? ($standardResponseNode->Anomalie ?? null) : null;
throw new EdnotifException(
codeAnomalie: (string) ($anomalyNode->Code ?? 'UNKNOWN'),
severite: (int) ($anomalyNode->Severite ?? 1),
message: (string) ($anomalyNode->Message ?? $operation.' : EDNOTIF error')
);
}
}

View File

@@ -4,9 +4,23 @@ declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Api;
use DateTimeInterface;
use Malio\EdnotifBundle\Bovin\Dto\AnimalFileDto;
use Malio\EdnotifBundle\Bovin\Dto\InventoryDto;
use Malio\EdnotifBundle\Bovin\Dto\PresumedExitsDto;
use Malio\EdnotifBundle\Bovin\Dto\ReturnedDossiersDto;
interface BovinApiInterface
{
public function getAnimalFile(string $nationalNumber, string $countryCode = 'FR'): AnimalFileDto;
public function getInventory(
DateTimeInterface $startDate,
?DateTimeInterface $endDate = null,
bool $includeEarTagStock = false,
): InventoryDto;
public function getReturnedDossiers(DateTimeInterface $startDate): ReturnedDossiersDto;
public function getPresumedExits(): PresumedExitsDto;
}

View 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,
) {}
}

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Dto;
final readonly class EarTagSeriesDto
{
public function __construct(
public object $rawNode,
) {}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Dto;
use DateTimeImmutable;
use Malio\EdnotifBundle\Shared\Dto\StandardResponseDto;
final readonly class InventoryDto
{
/**
* @param list<AnimalSummaryDto> $animals
* @param list<EarTagSeriesDto> $earTagSeries
*/
public function __construct(
public StandardResponseDto $standardResponse,
public int $nbBovins,
public ?DateTimeImmutable $startDate,
public ?DateTimeImmutable $endDate,
public ?bool $includesEarTagStock,
public array $animals,
public array $earTagSeries,
public ?object $rawSoapResponse,
) {}
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Dto;
use DateTimeImmutable;
final readonly class PresumedExitDto
{
public function __construct(
public ?BovinRef $bovin,
public ?DateTimeImmutable $exitDate,
) {}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Dto;
use Malio\EdnotifBundle\Shared\Dto\StandardResponseDto;
final readonly class PresumedExitsDto
{
/**
* @param list<PresumedExitDto> $presumedExits
*/
public function __construct(
public StandardResponseDto $standardResponse,
public int $nbBovins,
public array $presumedExits,
public ?object $rawSoapResponse,
) {}
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Dto;
use DateTimeImmutable;
use Malio\EdnotifBundle\Shared\Dto\StandardResponseDto;
final readonly class ReturnedDossiersDto
{
/**
* @param list<AnimalSummaryDto> $animals
*/
public function __construct(
public StandardResponseDto $standardResponse,
public int $nbBovins,
public ?DateTimeImmutable $startDate,
public array $animals,
public ?object $rawSoapResponse,
) {}
}

View File

@@ -4,24 +4,20 @@ declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Mapper;
use DateTimeImmutable;
use Malio\EdnotifBundle\Bovin\Dto\AnimalFileDto;
use Malio\EdnotifBundle\Bovin\Dto\BovinIdentificationDto;
use Malio\EdnotifBundle\Bovin\Dto\BovinRef;
use Malio\EdnotifBundle\Bovin\Dto\DateValueDto;
use Malio\EdnotifBundle\Bovin\Dto\ExploitationRef;
use Malio\EdnotifBundle\Bovin\Dto\MovementDto;
use Malio\EdnotifBundle\Bovin\Dto\ParentInfoDto;
use Malio\EdnotifBundle\Bovin\Dto\PresencePeriodDto;
use Malio\EdnotifBundle\Shared\Dto\AnomalyDto;
use Malio\EdnotifBundle\Shared\Dto\StandardResponseDto;
use Throwable;
use Malio\EdnotifBundle\Shared\Mapper\StandardResponseMapper;
final class AnimalFileMapper
{
use BovinNodeMappingTrait;
public function __construct(
private readonly StandardResponseMapper $standardResponseMapper,
) {}
public function map(object $soapResponse): AnimalFileDto
{
$standardResponse = $this->mapStandardResponse($soapResponse->ReponseStandard ?? null);
$standardResponse = $this->standardResponseMapper->map($soapResponse->ReponseStandard ?? null);
$specificResponseNode = $soapResponse->ReponseSpecifique ?? null;
$bovinNode = is_object($specificResponseNode) ? ($specificResponseNode->Bovin ?? null) : null;
@@ -51,185 +47,4 @@ final class AnimalFileMapper
rawSoapResponse: $soapResponse
);
}
private function mapStandardResponse(mixed $standardResponseNode): StandardResponseDto
{
$result = (bool) ($standardResponseNode->Resultat ?? false);
$anomalyNode = $standardResponseNode->Anomalie ?? null;
$anomaly = null;
if (is_object($anomalyNode)) {
$anomaly = new AnomalyDto(
code: $this->toNullableString($anomalyNode->Code ?? null),
severity: $this->toNullableInt($anomalyNode->Severite ?? null),
message: $this->toNullableString($anomalyNode->Message ?? null),
);
}
return new StandardResponseDto($result, $anomaly);
}
private function mapIdentification(object $identificationNode): BovinIdentificationDto
{
$bovinRef = $this->mapBovinRef($identificationNode->Bovin ?? null);
$birthDate = null;
$birthDateNode = $identificationNode->DateNaissance ?? null;
if (is_object($birthDateNode)) {
$birthDate = new DateValueDto(
date: $this->toNullableDate($birthDateNode->Date ?? null),
completenessFlag: $this->toNullableString($birthDateNode->TemoinCompletude ?? null),
);
}
$motherCarrier = $this->mapParentInfo($identificationNode->MerePorteuse ?? null);
$fatherIpg = $this->mapParentInfo($identificationNode->PereIPG ?? null);
$birthExploitation = $this->mapExploitationRef($identificationNode->ExploitationNaissance ?? null);
return new BovinIdentificationDto(
bovin: $bovinRef,
sex: $this->toNullableString($identificationNode->Sexe ?? null),
breedType: $this->toNullableString($identificationNode->TypeRacial ?? null),
birthDate: $birthDate,
workNumber: $this->toNullableString($identificationNode->NumeroTravail ?? null),
isFilie: $this->toNullableBool($identificationNode->StatutFilie ?? null),
motherCarrier: $motherCarrier,
fatherIpg: $fatherIpg,
birthExploitation: $birthExploitation,
);
}
private function mapPresencePeriod(object $presencePeriodNode): PresencePeriodDto
{
$entryNode = $presencePeriodNode->Entree ?? null;
$exitNode = $presencePeriodNode->Sortie ?? null;
$entryMovement = is_object($entryNode) ? $this->mapMovement($entryNode, 'entry') : null;
$exitMovement = is_object($exitNode) ? $this->mapMovement($exitNode, 'exit') : null;
return new PresencePeriodDto(
entry: $entryMovement,
exit: $exitMovement,
);
}
private function mapMovement(object $movementNode, string $direction): MovementDto
{
$dateValue = null;
$causeValue = null;
if ('entry' === $direction) {
// SOAP: DateEntree / CauseEntree
$dateValue = $movementNode->DateEntree ?? ($movementNode->Date ?? ($movementNode->DateMouvement ?? null));
$causeValue = $movementNode->CauseEntree ?? null;
} else {
// SOAP (souvent): DateSortie / CauseSortie
$dateValue = $movementNode->DateSortie ?? ($movementNode->Date ?? ($movementNode->DateMouvement ?? null));
$causeValue = $movementNode->CauseSortie ?? null;
}
$exploitationRef = $this->mapExploitationRef($movementNode->Exploitation ?? null);
return new MovementDto(
date: $this->toNullableDate($dateValue),
cause: $this->toNullableString($causeValue),
exploitation: $exploitationRef,
);
}
private function mapParentInfo(mixed $parentNode): ?ParentInfoDto
{
if (!is_object($parentNode)) {
return null;
}
$bovinRef = $this->mapBovinRef($parentNode->Bovin ?? null);
return new ParentInfoDto(
bovin: $bovinRef,
breedType: $this->toNullableString($parentNode->TypeRacial ?? null),
);
}
private function mapBovinRef(mixed $bovinNode): ?BovinRef
{
if (!is_object($bovinNode)) {
return null;
}
return new BovinRef(
countryCode: $this->toNullableString($bovinNode->CodePays ?? null),
nationalNumber: $this->toNullableString($bovinNode->NumeroNational ?? null),
);
}
private function mapExploitationRef(mixed $exploitationNode): ?ExploitationRef
{
if (!is_object($exploitationNode)) {
return null;
}
return new ExploitationRef(
countryCode: $this->toNullableString($exploitationNode->CodePays ?? null),
exploitationNumber: $this->toNullableString($exploitationNode->NumeroExploitation ?? null),
);
}
/** @return list<mixed> */
private function normalizeToList(mixed $value): array
{
if (null === $value) {
return [];
}
return is_array($value) ? $value : [$value];
}
private function toNullableString(mixed $value): ?string
{
if (null === $value) {
return null;
}
$stringValue = trim((string) $value);
return '' === $stringValue ? null : $stringValue;
}
private function toNullableInt(mixed $value): ?int
{
if (null === $value) {
return null;
}
if (is_int($value)) {
return $value;
}
if (is_numeric($value)) {
return (int) $value;
}
return null;
}
private function toNullableBool(mixed $value): ?bool
{
if (null === $value) {
return null;
}
return (bool) $value;
}
private function toNullableDate(mixed $value): ?DateTimeImmutable
{
if (!is_string($value) || '' === trim($value)) {
return null;
}
try {
return new DateTimeImmutable($value);
} catch (Throwable) {
return null;
}
}
}

View 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,
);
}
}

View File

@@ -0,0 +1,168 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Mapper;
use DateTimeImmutable;
use Malio\EdnotifBundle\Bovin\Dto\BovinIdentificationDto;
use Malio\EdnotifBundle\Bovin\Dto\BovinRef;
use Malio\EdnotifBundle\Bovin\Dto\DateValueDto;
use Malio\EdnotifBundle\Bovin\Dto\ExploitationRef;
use Malio\EdnotifBundle\Bovin\Dto\MovementDto;
use Malio\EdnotifBundle\Bovin\Dto\ParentInfoDto;
use Malio\EdnotifBundle\Bovin\Dto\PresencePeriodDto;
use Throwable;
/**
* Helpers partagés par les mappers travaillant sur des noeuds `Bovin` issus
* d'EDNOTIF, que la source soit une réponse SOAP directe (stdClass via ext-soap)
* ou un message XML zippé (stdClass via ZipMessageDecoder).
*/
trait BovinNodeMappingTrait
{
protected function mapIdentification(object $identificationNode): BovinIdentificationDto
{
$birthDate = null;
$birthDateNode = $identificationNode->DateNaissance ?? null;
if (is_object($birthDateNode)) {
$birthDate = new DateValueDto(
date: $this->toNullableDate($birthDateNode->Date ?? null),
completenessFlag: $this->toNullableString($birthDateNode->TemoinCompletude ?? null),
);
}
return new BovinIdentificationDto(
bovin: $this->mapBovinRef($identificationNode->Bovin ?? null),
sex: $this->toNullableString($identificationNode->Sexe ?? null),
breedType: $this->toNullableString($identificationNode->TypeRacial ?? null),
birthDate: $birthDate,
workNumber: $this->toNullableString($identificationNode->NumeroTravail ?? null),
isFilie: $this->toNullableBool($identificationNode->StatutFilie ?? null),
motherCarrier: $this->mapParentInfo($identificationNode->MerePorteuse ?? null),
fatherIpg: $this->mapParentInfo($identificationNode->PereIPG ?? null),
birthExploitation: $this->mapExploitationRef($identificationNode->ExploitationNaissance ?? null),
);
}
protected function mapPresencePeriod(object $presencePeriodNode): PresencePeriodDto
{
$entryNode = $presencePeriodNode->Entree ?? null;
$exitNode = $presencePeriodNode->Sortie ?? null;
return new PresencePeriodDto(
entry: is_object($entryNode) ? $this->mapMovement($entryNode, 'entry') : null,
exit: is_object($exitNode) ? $this->mapMovement($exitNode, 'exit') : null,
);
}
protected function mapMovement(object $movementNode, string $direction): MovementDto
{
if ('entry' === $direction) {
$dateValue = $movementNode->DateEntree ?? ($movementNode->Date ?? ($movementNode->DateMouvement ?? null));
$causeValue = $movementNode->CauseEntree ?? null;
} else {
$dateValue = $movementNode->DateSortie ?? ($movementNode->Date ?? ($movementNode->DateMouvement ?? null));
$causeValue = $movementNode->CauseSortie ?? null;
}
return new MovementDto(
date: $this->toNullableDate($dateValue),
cause: $this->toNullableString($causeValue),
exploitation: $this->mapExploitationRef($movementNode->Exploitation ?? null),
);
}
protected function mapParentInfo(mixed $parentNode): ?ParentInfoDto
{
if (!is_object($parentNode)) {
return null;
}
return new ParentInfoDto(
bovin: $this->mapBovinRef($parentNode->Bovin ?? null),
breedType: $this->toNullableString($parentNode->TypeRacial ?? null),
);
}
protected function mapBovinRef(mixed $bovinNode): ?BovinRef
{
if (!is_object($bovinNode)) {
return null;
}
return new BovinRef(
countryCode: $this->toNullableString($bovinNode->CodePays ?? null),
nationalNumber: $this->toNullableString($bovinNode->NumeroNational ?? null),
);
}
protected function mapExploitationRef(mixed $exploitationNode): ?ExploitationRef
{
if (!is_object($exploitationNode)) {
return null;
}
return new ExploitationRef(
countryCode: $this->toNullableString($exploitationNode->CodePays ?? null),
exploitationNumber: $this->toNullableString($exploitationNode->NumeroExploitation ?? null),
);
}
/** @return list<mixed> */
protected function normalizeToList(mixed $value): array
{
if (null === $value) {
return [];
}
return is_array($value) ? array_values($value) : [$value];
}
protected function toNullableString(mixed $value): ?string
{
if (null === $value) {
return null;
}
$stringValue = trim((string) $value);
return '' === $stringValue ? null : $stringValue;
}
protected function toNullableInt(mixed $value): ?int
{
if (null === $value) {
return null;
}
if (is_int($value)) {
return $value;
}
if (is_numeric($value)) {
return (int) $value;
}
return null;
}
protected function toNullableBool(mixed $value): ?bool
{
if (null === $value) {
return null;
}
return (bool) $value;
}
protected function toNullableDate(mixed $value): ?DateTimeImmutable
{
if (!is_string($value) || '' === trim($value)) {
return null;
}
try {
return new DateTimeImmutable($value);
} catch (Throwable) {
return null;
}
}
}

View File

@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Mapper;
use Malio\EdnotifBundle\Bovin\Dto\EarTagSeriesDto;
use Malio\EdnotifBundle\Bovin\Dto\InventoryDto;
use Malio\EdnotifBundle\Shared\Mapper\StandardResponseMapper;
final class InventoryMapper
{
use BovinNodeMappingTrait;
public function __construct(
private readonly AnimalSummaryMapper $animalSummaryMapper,
private readonly StandardResponseMapper $standardResponseMapper,
) {}
public function map(object $soapResponse, ?object $unzippedMessage): InventoryDto
{
$standardResponse = $this->standardResponseMapper->map($soapResponse->ReponseStandard ?? null);
$nbBovins = $this->toNullableInt($soapResponse->ReponseSpecifique->NbBovins ?? null) ?? 0;
$startDate = null;
$endDate = null;
$includesEarTagStock = null;
$animals = [];
$earTagSeries = [];
if (is_object($unzippedMessage)) {
$infoNode = $unzippedMessage->InformationsMessage ?? null;
if (is_object($infoNode)) {
$startDate = $this->toNullableDate($infoNode->DateDebut ?? null);
$endDate = $this->toNullableDate($infoNode->DateFin ?? null);
$includesEarTagStock = $this->toNullableBool($infoNode->StockBoucles ?? null);
}
$bovinsNode = $unzippedMessage->Bovins->Bovin ?? null;
foreach ($this->normalizeToList($bovinsNode) as $bovinNode) {
if (!is_object($bovinNode)) {
continue;
}
$animals[] = $this->animalSummaryMapper->map($bovinNode);
}
$seriesNode = $unzippedMessage->Boucles->SerieBoucles ?? null;
foreach ($this->normalizeToList($seriesNode) as $serieNode) {
if (!is_object($serieNode)) {
continue;
}
$earTagSeries[] = new EarTagSeriesDto(rawNode: $serieNode);
}
}
return new InventoryDto(
standardResponse: $standardResponse,
nbBovins: $nbBovins,
startDate: $startDate,
endDate: $endDate,
includesEarTagStock: $includesEarTagStock,
animals: $animals,
earTagSeries: $earTagSeries,
rawSoapResponse: $soapResponse,
);
}
}

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Mapper;
use Malio\EdnotifBundle\Bovin\Dto\PresumedExitDto;
use Malio\EdnotifBundle\Bovin\Dto\PresumedExitsDto;
use Malio\EdnotifBundle\Shared\Mapper\StandardResponseMapper;
final class PresumedExitsMapper
{
use BovinNodeMappingTrait;
public function __construct(
private readonly StandardResponseMapper $standardResponseMapper,
) {}
public function map(object $soapResponse, ?object $unzippedMessage): PresumedExitsDto
{
$standardResponse = $this->standardResponseMapper->map($soapResponse->ReponseStandard ?? null);
$nbBovins = $this->toNullableInt($soapResponse->ReponseSpecifique->NbBovins ?? null) ?? 0;
$presumedExits = [];
if (is_object($unzippedMessage)) {
$exitsNode = $unzippedMessage->SortiesPresumees->SortiePresumee ?? null;
foreach ($this->normalizeToList($exitsNode) as $exitNode) {
if (!is_object($exitNode)) {
continue;
}
$presumedExits[] = new PresumedExitDto(
bovin: $this->mapBovinRef($exitNode->Bovin ?? null),
exitDate: $this->toNullableDate($exitNode->DateSortie ?? null),
);
}
}
return new PresumedExitsDto(
standardResponse: $standardResponse,
nbBovins: $nbBovins,
presumedExits: $presumedExits,
rawSoapResponse: $soapResponse,
);
}
}

View File

@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Mapper;
use Malio\EdnotifBundle\Bovin\Dto\ReturnedDossiersDto;
use Malio\EdnotifBundle\Shared\Mapper\StandardResponseMapper;
final class ReturnedDossiersMapper
{
use BovinNodeMappingTrait;
public function __construct(
private readonly AnimalSummaryMapper $animalSummaryMapper,
private readonly StandardResponseMapper $standardResponseMapper,
) {}
public function map(object $soapResponse, ?object $unzippedMessage): ReturnedDossiersDto
{
$standardResponse = $this->standardResponseMapper->map($soapResponse->ReponseStandard ?? null);
$nbBovins = $this->toNullableInt($soapResponse->ReponseSpecifique->NbBovins ?? null) ?? 0;
$startDate = null;
$animals = [];
if (is_object($unzippedMessage)) {
$infoNode = $unzippedMessage->InformationsMessage ?? null;
if (is_object($infoNode)) {
$startDate = $this->toNullableDate($infoNode->DateDebut ?? null);
}
$bovinsNode = $unzippedMessage->Bovins->Bovin ?? null;
foreach ($this->normalizeToList($bovinsNode) as $bovinNode) {
if (!is_object($bovinNode)) {
continue;
}
$animals[] = $this->animalSummaryMapper->map($bovinNode);
}
}
return new ReturnedDossiersDto(
standardResponse: $standardResponse,
nbBovins: $nbBovins,
startDate: $startDate,
animals: $animals,
rawSoapResponse: $soapResponse,
);
}
}

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Shared\Mapper;
use Malio\EdnotifBundle\Shared\Dto\AnomalyDto;
use Malio\EdnotifBundle\Shared\Dto\StandardResponseDto;
final class StandardResponseMapper
{
public function map(mixed $standardResponseNode): StandardResponseDto
{
$result = (bool) ($standardResponseNode->Resultat ?? false);
$anomalyNode = is_object($standardResponseNode) ? ($standardResponseNode->Anomalie ?? null) : null;
$anomaly = null;
if (is_object($anomalyNode)) {
$anomaly = new AnomalyDto(
code: $this->toNullableString($anomalyNode->Code ?? null),
severity: $this->toNullableInt($anomalyNode->Severite ?? null),
message: $this->toNullableString($anomalyNode->Message ?? null),
);
}
return new StandardResponseDto($result, $anomaly);
}
private function toNullableString(mixed $value): ?string
{
if (null === $value) {
return null;
}
$stringValue = trim((string) $value);
return '' === $stringValue ? null : $stringValue;
}
private function toNullableInt(mixed $value): ?int
{
if (null === $value) {
return null;
}
if (is_int($value)) {
return $value;
}
if (is_numeric($value)) {
return (int) $value;
}
return null;
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Shared\Soap;
use RuntimeException;
use ZipArchive;
final class ZipMessageDecoder
{
/**
* Décode le binaire `MessageZip` retourné par les opérations EDNOTIF de type
* `Get*` (Inventaire / RetourDossiers / SortiesPresumees) : le contenu est déjà
* décodé par ext-soap depuis base64, il reste à dézipper et parser le XML.
*/
public function decode(string $zipBinary): object
{
if ('' === $zipBinary) {
throw new RuntimeException('ZipMessageDecoder: binaire ZIP vide.');
}
$tempFile = tempnam(sys_get_temp_dir(), 'ednotif_zip_');
if (false === $tempFile) {
throw new RuntimeException('ZipMessageDecoder: impossible de créer un fichier temporaire.');
}
try {
if (false === file_put_contents($tempFile, $zipBinary)) {
throw new RuntimeException('ZipMessageDecoder: impossible d\'écrire le fichier temporaire.');
}
$xml = $this->readFirstEntry($tempFile);
} finally {
@unlink($tempFile);
}
$previous = libxml_use_internal_errors(true);
try {
$simpleXml = simplexml_load_string($xml);
} finally {
libxml_clear_errors();
libxml_use_internal_errors($previous);
}
if (false === $simpleXml) {
throw new RuntimeException('ZipMessageDecoder: XML invalide dans l\'archive ZIP.');
}
$json = json_encode($simpleXml);
if (false === $json) {
throw new RuntimeException('ZipMessageDecoder: échec de l\'encodage JSON intermédiaire.');
}
$decoded = json_decode($json, false);
if (!is_object($decoded)) {
throw new RuntimeException('ZipMessageDecoder: décodage JSON non objet.');
}
return $decoded;
}
private function readFirstEntry(string $filePath): string
{
$zip = new ZipArchive();
$openResult = $zip->open($filePath, ZipArchive::RDONLY);
if (true !== $openResult) {
throw new RuntimeException(sprintf('ZipMessageDecoder: ouverture ZIP impossible (code %s).', (string) $openResult));
}
try {
if (0 === $zip->numFiles) {
throw new RuntimeException('ZipMessageDecoder: archive ZIP vide.');
}
$xml = $zip->getFromIndex(0);
if (false === $xml) {
throw new RuntimeException('ZipMessageDecoder: lecture de l\'entrée ZIP impossible.');
}
} finally {
$zip->close();
}
return $xml;
}
}

0
tests/Unit/.gitkeep Normal file
View File

View 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;
}
}

View File

@@ -0,0 +1,100 @@
<?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);
}
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;
}
}

View File

@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Tests\Unit\Bovin\Mapper;
use DateTimeImmutable;
use Malio\EdnotifBundle\Bovin\Dto\PresumedExitsDto;
use Malio\EdnotifBundle\Bovin\Mapper\PresumedExitsMapper;
use Malio\EdnotifBundle\Shared\Mapper\StandardResponseMapper;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use stdClass;
/**
* @internal
*/
#[CoversClass(PresumedExitsMapper::class)]
final class PresumedExitsMapperTest extends TestCase
{
public function testMapWithExits(): void
{
$soapResponse = new stdClass();
$soapResponse->ReponseStandard = new stdClass();
$soapResponse->ReponseStandard->Resultat = true;
$soapResponse->ReponseSpecifique = new stdClass();
$soapResponse->ReponseSpecifique->NbBovins = 2;
$message = new stdClass();
$message->SortiesPresumees = new stdClass();
$message->SortiesPresumees->SortiePresumee = [
$this->makeExit('FR111', '2026-02-15'),
$this->makeExit('FR222', null),
];
$dto = new PresumedExitsMapper(new StandardResponseMapper())->map($soapResponse, $message);
self::assertInstanceOf(PresumedExitsDto::class, $dto);
self::assertSame(2, $dto->nbBovins);
self::assertCount(2, $dto->presumedExits);
self::assertSame('FR111', $dto->presumedExits[0]->bovin?->nationalNumber);
self::assertEquals(new DateTimeImmutable('2026-02-15'), $dto->presumedExits[0]->exitDate);
self::assertNull($dto->presumedExits[1]->exitDate);
}
public function testMapWithoutMessageReturnsEmpty(): void
{
$soapResponse = new stdClass();
$soapResponse->ReponseStandard = new stdClass();
$soapResponse->ReponseStandard->Resultat = true;
$soapResponse->ReponseSpecifique = new stdClass();
$soapResponse->ReponseSpecifique->NbBovins = 0;
$dto = new PresumedExitsMapper(new StandardResponseMapper())->map($soapResponse, null);
self::assertSame(0, $dto->nbBovins);
self::assertSame([], $dto->presumedExits);
}
private function makeExit(string $nationalNumber, ?string $exitDate): object
{
$node = new stdClass();
$node->Bovin = new stdClass();
$node->Bovin->CodePays = 'FR';
$node->Bovin->NumeroNational = $nationalNumber;
if (null !== $exitDate) {
$node->DateSortie = $exitDate;
}
return $node;
}
}

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Tests\Unit\Bovin\Mapper;
use DateTimeImmutable;
use Malio\EdnotifBundle\Bovin\Dto\ReturnedDossiersDto;
use Malio\EdnotifBundle\Bovin\Mapper\AnimalSummaryMapper;
use Malio\EdnotifBundle\Bovin\Mapper\ReturnedDossiersMapper;
use Malio\EdnotifBundle\Shared\Mapper\StandardResponseMapper;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use stdClass;
/**
* @internal
*/
#[CoversClass(ReturnedDossiersMapper::class)]
final class ReturnedDossiersMapperTest extends TestCase
{
public function testMapReturnsAnimalsAndStartDate(): void
{
$mapper = new ReturnedDossiersMapper(new AnimalSummaryMapper(), new StandardResponseMapper());
$soapResponse = new stdClass();
$soapResponse->ReponseStandard = new stdClass();
$soapResponse->ReponseStandard->Resultat = true;
$soapResponse->ReponseSpecifique = new stdClass();
$soapResponse->ReponseSpecifique->NbBovins = 1;
$message = new stdClass();
$message->InformationsMessage = new stdClass();
$message->InformationsMessage->DateDebut = '2026-03-01';
$message->Bovins = new stdClass();
$message->Bovins->Bovin = new stdClass();
$message->Bovins->Bovin->IdentiteBovin = new stdClass();
$message->Bovins->Bovin->IdentiteBovin->Bovin = new stdClass();
$message->Bovins->Bovin->IdentiteBovin->Bovin->NumeroNational = 'FR789';
$message->Bovins->Bovin->PeriodesPresences = new stdClass();
$message->Bovins->Bovin->PeriodesPresences->PeriodePresence = new stdClass();
$dto = $mapper->map($soapResponse, $message);
self::assertInstanceOf(ReturnedDossiersDto::class, $dto);
self::assertEquals(new DateTimeImmutable('2026-03-01'), $dto->startDate);
self::assertSame(1, $dto->nbBovins);
self::assertCount(1, $dto->animals);
self::assertSame('FR789', $dto->animals[0]->identification?->bovin?->nationalNumber);
}
public function testMapWithoutMessageReturnsEmptyAnimals(): void
{
$mapper = new ReturnedDossiersMapper(new AnimalSummaryMapper(), new StandardResponseMapper());
$soapResponse = new stdClass();
$soapResponse->ReponseStandard = new stdClass();
$soapResponse->ReponseStandard->Resultat = true;
$soapResponse->ReponseSpecifique = new stdClass();
$soapResponse->ReponseSpecifique->NbBovins = 0;
$dto = $mapper->map($soapResponse, null);
self::assertSame(0, $dto->nbBovins);
self::assertSame([], $dto->animals);
}
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Tests\Unit\Shared\Soap;
use Malio\EdnotifBundle\Shared\Soap\ZipMessageDecoder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use ZipArchive;
/**
* @internal
*/
#[CoversClass(ZipMessageDecoder::class)]
final class ZipMessageDecoderTest extends TestCase
{
public function testDecodeReturnsObjectFromZippedXml(): void
{
$xml = '<?xml version="1.0" encoding="UTF-8"?>'
.'<MessageIpBNotifGetInventaire>'
.'<InformationsMessage><DateDebut>2026-01-01</DateDebut></InformationsMessage>'
.'<Bovins><Bovin><IdentiteBovin><Sexe>F</Sexe></IdentiteBovin></Bovin></Bovins>'
.'</MessageIpBNotifGetInventaire>';
$zipBinary = $this->makeZipBinary('message.xml', $xml);
$decoded = new ZipMessageDecoder()->decode($zipBinary);
self::assertIsObject($decoded);
self::assertSame('2026-01-01', (string) $decoded->InformationsMessage->DateDebut);
self::assertSame('F', (string) $decoded->Bovins->Bovin->IdentiteBovin->Sexe);
}
public function testDecodeProducesArrayForMultiChildNodes(): void
{
$xml = '<?xml version="1.0" encoding="UTF-8"?>'
.'<MessageIpBNotifGetInventaire>'
.'<Bovins>'
.'<Bovin><IdentiteBovin><Sexe>F</Sexe></IdentiteBovin></Bovin>'
.'<Bovin><IdentiteBovin><Sexe>M</Sexe></IdentiteBovin></Bovin>'
.'</Bovins>'
.'</MessageIpBNotifGetInventaire>';
$decoded = new ZipMessageDecoder()->decode($this->makeZipBinary('message.xml', $xml));
self::assertIsArray($decoded->Bovins->Bovin);
self::assertCount(2, $decoded->Bovins->Bovin);
self::assertSame('F', (string) $decoded->Bovins->Bovin[0]->IdentiteBovin->Sexe);
self::assertSame('M', (string) $decoded->Bovins->Bovin[1]->IdentiteBovin->Sexe);
}
public function testDecodeThrowsOnEmptyBinary(): void
{
$this->expectException(RuntimeException::class);
new ZipMessageDecoder()->decode('');
}
public function testDecodeThrowsOnInvalidZip(): void
{
$this->expectException(RuntimeException::class);
new ZipMessageDecoder()->decode('not a zip');
}
private function makeZipBinary(string $innerFile, string $content): string
{
$tempFile = tempnam(sys_get_temp_dir(), 'test_zip_');
self::assertIsString($tempFile);
$zip = new ZipArchive();
self::assertTrue(true === $zip->open($tempFile, ZipArchive::CREATE | ZipArchive::OVERWRITE));
self::assertTrue($zip->addFromString($innerFile, $content));
self::assertTrue($zip->close());
$binary = file_get_contents($tempFile);
@unlink($tempFile);
self::assertIsString($binary);
return $binary;
}
}

5
tests/bootstrap.php Normal file
View File

@@ -0,0 +1,5 @@
<?php
declare(strict_types=1);
require __DIR__.'/../vendor/autoload.php';