test(logistique) : tests PHPUnit RG-5.01→5.10 + capture contrat JSON (ERP-187) (#137)
Auto Tag Develop / tag (push) Successful in 8s
Auto Tag Develop / tag (push) Successful in 8s
## ERP-187 (1.7) — Tests PHPUnit RG-5.01→5.10 + capture contrat JSON
Couvre les règles de gestion du M5 (tickets de pesée) par des tests PHPUnit et capture la **réponse JSON réelle** (DoD § 4.0.bis) collée dans `spec-back.md` avant les écrans front.
### Tests unitaires (Processor / Normalizer / Callback — sans BDD ni HTTP)
- **NetWeightTest** (RG-5.05) : net = plein − vide, `null` si une pesée manque, recalcul au PATCH.
- **CounterpartyValidationTest** (RG-5.03) : présence du champ requis par branche (propertyPath `client`/`supplier`/`otherLabel`) + exclusivité (null-ification hors-branche).
- **ImmatriculationNormalizationTest** (RG-5.01/5.10) : masque `XX-000-XX`, « Tout format », mapping 422 sur `immatriculation`.
### Tests fonctionnels (API réelle)
- **WeighingTicketNumberingTest** (RG-5.02/5.09) : format `{siteCode}-TP-{NNNN}`, séquence par site, isolation inter-sites, immuabilité numéro/site au PATCH.
- **WeighingTicketSerializationContractTest** (DoD § 4.0.bis) : 4 pièges verts (client embarqué, `plateFreeFormat` présent, `number` formaté, `netWeight` = full − empty) + dump JSON via `WEIGHING_TICKET_DOD_DUMP`.
- **WeighingTicketRBACMatrixTest** (§ 5.2) : admin/bureau/usine OK, compta/commerciale 403, anonyme 401.
> DSD / stub pont bascule / endpoint pesée déjà couverts (ERP-184/185).
### DoD
- `spec-back.md § 4.0.bis` : **JSON réel** (liste + détail) collé, 4 pièges marqués ✅ — feu vert front.
### Vérifications
- `make test` complet **vert** : 848 tests, 6302 assertions (0 échec ; deprecations/notices PHPUnit seuls).
- `make php-cs-fixer-allow-risky` : 0 correction.
Empilée sur ERP-186 (stack M5).
---------
Co-authored-by: Matthieu <contact@malio.fr>
Reviewed-on: #137
This commit was merged in pull request #137.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Module\Logistique\Api;
|
||||
|
||||
/**
|
||||
* Numerotation des tickets de pesee (RG-5.02 / § 2.5) — tests fonctionnels sur
|
||||
* l'API reelle (compteur DBAL `weighing_ticket_counter`, verrou FOR UPDATE).
|
||||
*
|
||||
* Couvre : format {siteCode}-TP-{NNNN}, sequence incrementale et unique PAR site,
|
||||
* independance des sequences entre sites, immuabilite du numero et du site au PATCH
|
||||
* (RG-5.09 : aucun groupe d'ecriture sur ces champs).
|
||||
*
|
||||
* La serialisation concurrente (FOR UPDATE) est exercee a l'identique par le
|
||||
* DsdAllocator (cf. DsdAllocatorTest) ; un vrai parallelisme n'est pas reproductible
|
||||
* en PHPUnit mono-processus — on valide ici la sequence deterministe.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class WeighingTicketNumberingTest extends AbstractWeighingTicketApiTestCase
|
||||
{
|
||||
public function testNumberFormatAndSequentialPerSite(): void
|
||||
{
|
||||
$site = $this->siteByCode('86');
|
||||
$http = $this->authManageOnSite($site);
|
||||
$client = $this->seedTestClient('Num');
|
||||
|
||||
$first = $this->postTicket($http, $this->validClientTicketPayload($client));
|
||||
self::assertResponseStatusCodeSame(201);
|
||||
$second = $this->postTicket($http, $this->validClientTicketPayload($client));
|
||||
self::assertResponseStatusCodeSame(201);
|
||||
|
||||
$n1 = (string) $first->toArray()['number'];
|
||||
$n2 = (string) $second->toArray()['number'];
|
||||
|
||||
self::assertMatchesRegularExpression('/^86-TP-\d{4}$/', $n1);
|
||||
self::assertMatchesRegularExpression('/^86-TP-\d{4}$/', $n2);
|
||||
self::assertNotSame($n1, $n2, 'Deux tickets du meme site portent des numeros distincts (unicite).');
|
||||
|
||||
// Sequence : le second numero = premier + 1 (compteur par site).
|
||||
self::assertSame($this->suffix($n1) + 1, $this->suffix($n2));
|
||||
}
|
||||
|
||||
public function testNumberingIsIsolatedPerSite(): void
|
||||
{
|
||||
$client = $this->seedTestClient('IsoSite');
|
||||
|
||||
$http86 = $this->authManageOnSite($this->siteByCode('86'));
|
||||
$http17 = $this->authManageOnSite($this->siteByCode('17'));
|
||||
|
||||
$n86 = (string) $this->postTicket($http86, $this->validClientTicketPayload($client))->toArray()['number'];
|
||||
$n17 = (string) $this->postTicket($http17, $this->validClientTicketPayload($client))->toArray()['number'];
|
||||
|
||||
// Chaque site encode son propre code dans le numero ; sequences disjointes.
|
||||
self::assertStringStartsWith('86-TP-', $n86);
|
||||
self::assertStringStartsWith('17-TP-', $n17);
|
||||
}
|
||||
|
||||
public function testNumberAndSiteAreImmutableOnPatch(): void
|
||||
{
|
||||
$site = $this->siteByCode('86');
|
||||
$http = $this->authManageOnSite($site);
|
||||
$client = $this->seedTestClient('Immutable');
|
||||
|
||||
$created = $this->postTicket($http, $this->validClientTicketPayload($client))->toArray();
|
||||
$id = (int) $created['id'];
|
||||
$number = (string) $created['number'];
|
||||
|
||||
// Tentative de re-ecriture du numero et du site (aucun groupe d'ecriture) +
|
||||
// changement legitime de la pesee a plein -> net recalcule.
|
||||
$patched = $http->request('PATCH', '/api/weighing_tickets/'.$id, [
|
||||
'headers' => ['Content-Type' => self::MERGE],
|
||||
'json' => [
|
||||
'number' => 'HACK-TP-9999',
|
||||
'site' => '/api/sites/'.$this->siteByCode('17')->getId(),
|
||||
'fullWeight' => 20000,
|
||||
],
|
||||
])->toArray();
|
||||
|
||||
self::assertSame($number, $patched['number'], 'Le numero est immuable (RG-5.02 / RG-5.09).');
|
||||
self::assertSame('86', $patched['site']['code'], 'Le site est immuable (RG-5.09).');
|
||||
// Net recalcule : 20000 - 7150 = 12850 (RG-5.05).
|
||||
self::assertSame(12850, $patched['netWeight']);
|
||||
}
|
||||
|
||||
/** Suffixe numerique {NNNN} d'un numero {siteCode}-TP-{NNNN}. */
|
||||
private function suffix(string $number): int
|
||||
{
|
||||
return (int) substr($number, strrpos($number, '-') + 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user