feat(logistique) : pesée pont bascule stub + allocateur DSD + endpoint (ERP-184)
- WeighbridgeReaderInterface (contrat) + RandomWeighbridgeReader (stub,
poids aléatoire ∈ [10000,50000] kg, RG-5.06) + WeighbridgeUnavailableException
- DsdAllocator : compteur DSD par site (weighbridge_dsd_counter) incrémenté
sous verrou ligne SELECT ... FOR UPDATE (RG-5.04, § 2.7)
- endpoint POST /api/weighbridge_readings : ressource virtuelle
WeighbridgeReadingResource + WeighbridgeReadingProcessor (pas de controller)
- AUTO -> {weight, dsd, mode} ; MANUAL -> {weight, dsd, manualNumber, mode}
- WeighbridgeUnavailableException -> HTTP 503 explicite (RG-5.06)
- site courant via CurrentSiteProviderInterface (contrat Sites)
- is_granted('logistique.weighing_tickets.manage')
- dsd renvoyé prévisionnel : attribution autoritaire refaite à la création
du ticket (ERP-185)
- tests : WeighbridgeReaderStubTest, DsdAllocatorTest, processor (503/400),
WeighbridgeReadingApiTest (RBAC + AUTO/MANUAL + 422)
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Module\Logistique\Api;
|
||||
|
||||
use ApiPlatform\Symfony\Bundle\Test\Client;
|
||||
use App\Module\Core\Domain\Entity\Role;
|
||||
use App\Module\Core\Domain\Entity\User;
|
||||
use App\Module\Sites\Domain\Entity\Site;
|
||||
use App\Tests\Module\Core\Api\AbstractApiTestCase;
|
||||
|
||||
/**
|
||||
* Endpoint `POST /api/weighbridge_readings` (§ 4.2) — tests fonctionnels.
|
||||
*
|
||||
* Couvre le wiring securite/routage (que les tests unitaires ne voient pas) :
|
||||
* - happy path AUTO / MANUAL avec site courant et permission `manage` ;
|
||||
* - 403 sans la permission `manage` (RBAC § 5.2) ;
|
||||
* - 422 si le mode est absent / invalide (validation de la ressource).
|
||||
*
|
||||
* Nettoyage manuel (pas de DAMA) : users/roles `test*` + compteurs DSD.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class WeighbridgeReadingApiTest extends AbstractApiTestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$em = $this->getEm();
|
||||
$em->getConnection()->executeStatement('DELETE FROM weighbridge_dsd_counter');
|
||||
$em->createQuery('DELETE FROM '.User::class.' u WHERE u.username LIKE :p')
|
||||
->setParameter('p', 'testuser_%')->execute();
|
||||
$em->createQuery('DELETE FROM '.Role::class.' r WHERE r.code LIKE :p')
|
||||
->setParameter('p', 'test_%')->execute();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testAutoWeighingReturnsWeightInBoundsAndDsd(): void
|
||||
{
|
||||
$client = $this->manageClientWithCurrentSite();
|
||||
|
||||
$response = $client->request('POST', '/api/weighbridge_readings', [
|
||||
'headers' => ['Content-Type' => 'application/ld+json'],
|
||||
'json' => ['mode' => 'AUTO'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(200);
|
||||
$data = $response->toArray();
|
||||
|
||||
self::assertSame('AUTO', $data['mode']);
|
||||
self::assertIsInt($data['weight']);
|
||||
self::assertGreaterThanOrEqual(10000, $data['weight']);
|
||||
self::assertLessThanOrEqual(50000, $data['weight']);
|
||||
self::assertIsInt($data['dsd']);
|
||||
self::assertGreaterThanOrEqual(1, $data['dsd']);
|
||||
// manualNumber est null en mode bascule (cle potentiellement omise si
|
||||
// skip_null_values est actif — tolerant aux deux cas).
|
||||
self::assertNull($data['manualNumber'] ?? null);
|
||||
}
|
||||
|
||||
public function testManualWeighingKeepsWeightAndAllocatesDsd(): void
|
||||
{
|
||||
$client = $this->manageClientWithCurrentSite();
|
||||
|
||||
$response = $client->request('POST', '/api/weighbridge_readings', [
|
||||
'headers' => ['Content-Type' => 'application/ld+json'],
|
||||
'json' => ['mode' => 'MANUAL', 'weight' => 23187, 'manualNumber' => 'PAP-555'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(200);
|
||||
$data = $response->toArray();
|
||||
|
||||
self::assertSame('MANUAL', $data['mode']);
|
||||
self::assertSame(23187, $data['weight']);
|
||||
self::assertSame('PAP-555', $data['manualNumber']);
|
||||
self::assertGreaterThanOrEqual(1, $data['dsd']);
|
||||
}
|
||||
|
||||
public function testManagePermissionIsRequired(): void
|
||||
{
|
||||
// Un user portant uniquement `view` ne peut pas declencher de pesee.
|
||||
$credentials = $this->createUserWithPermission('logistique.weighing_tickets.view');
|
||||
$client = $this->authenticatedClient($credentials['username'], $credentials['password']);
|
||||
|
||||
$client->request('POST', '/api/weighbridge_readings', [
|
||||
'headers' => ['Content-Type' => 'application/ld+json'],
|
||||
'json' => ['mode' => 'AUTO'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
public function testInvalidModeIsRejected(): void
|
||||
{
|
||||
$client = $this->manageClientWithCurrentSite();
|
||||
|
||||
$client->request('POST', '/api/weighbridge_readings', [
|
||||
'headers' => ['Content-Type' => 'application/ld+json'],
|
||||
'json' => ['mode' => 'INVALID'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
}
|
||||
|
||||
public function testManualWeighingRequiresWeight(): void
|
||||
{
|
||||
$client = $this->manageClientWithCurrentSite();
|
||||
|
||||
$client->request('POST', '/api/weighbridge_readings', [
|
||||
'headers' => ['Content-Type' => 'application/ld+json'],
|
||||
'json' => ['mode' => 'MANUAL'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cree un user non-admin portant `logistique.weighing_tickets.manage`, lui
|
||||
* positionne un site courant (l'endpoint est cloisonne par site, § 2.3) et
|
||||
* renvoie un client authentifie.
|
||||
*/
|
||||
private function manageClientWithCurrentSite(): Client
|
||||
{
|
||||
$credentials = $this->createUserWithPermission('logistique.weighing_tickets.manage');
|
||||
|
||||
$em = $this->getEm();
|
||||
$user = $em->getRepository(User::class)->findOneBy(['username' => $credentials['username']]);
|
||||
self::assertInstanceOf(User::class, $user);
|
||||
|
||||
$site = $em->getRepository(Site::class)->findAll()[0];
|
||||
$user->setCurrentSite($site);
|
||||
$em->flush();
|
||||
|
||||
return $this->authenticatedClient($credentials['username'], $credentials['password']);
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Module\Logistique\Infrastructure\ApiPlatform\State\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use App\Module\Logistique\Application\Service\DsdAllocatorInterface;
|
||||
use App\Module\Logistique\Domain\Contract\WeighbridgeReaderInterface;
|
||||
use App\Module\Logistique\Domain\Exception\WeighbridgeUnavailableException;
|
||||
use App\Module\Logistique\Domain\Weighbridge\WeighbridgeReading;
|
||||
use App\Module\Logistique\Infrastructure\ApiPlatform\Resource\WeighbridgeReadingResource;
|
||||
use App\Module\Logistique\Infrastructure\ApiPlatform\State\Processor\WeighbridgeReadingProcessor;
|
||||
use App\Module\Sites\Application\Service\CurrentSiteProviderInterface;
|
||||
use App\Module\Sites\Domain\Entity\Site;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
|
||||
|
||||
/**
|
||||
* Processor de l'action `POST /api/weighbridge_readings` (§ 4.2).
|
||||
*
|
||||
* Couvre les 4 chemins sans BDD ni HTTP (stubs purs) : AUTO (lecture pont),
|
||||
* MANUAL (allocation DSD seule), indisponibilite → 503 (RG-5.06) et absence de
|
||||
* site courant → 400.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class WeighbridgeReadingProcessorTest extends TestCase
|
||||
{
|
||||
private function site(): Site
|
||||
{
|
||||
// getId() reste null (non persiste) — sans incidence : reader et allocator
|
||||
// sont stubbes dans ces tests unitaires.
|
||||
return new Site('Châtellerault', 'Rue du Pont', null, '86000', 'Châtellerault', '#112233');
|
||||
}
|
||||
|
||||
public function testAutoModeFillsWeightAndDsdFromReader(): void
|
||||
{
|
||||
$siteProvider = $this->createStub(CurrentSiteProviderInterface::class);
|
||||
$siteProvider->method('get')->willReturn($this->site());
|
||||
|
||||
$reader = $this->createStub(WeighbridgeReaderInterface::class);
|
||||
$reader->method('read')->willReturn(new WeighbridgeReading(23000, 42));
|
||||
|
||||
$processor = new WeighbridgeReadingProcessor(
|
||||
$siteProvider,
|
||||
$reader,
|
||||
$this->createStub(DsdAllocatorInterface::class),
|
||||
);
|
||||
|
||||
$resource = new WeighbridgeReadingResource();
|
||||
$resource->mode = 'AUTO';
|
||||
|
||||
$result = $processor->process($resource, new Post());
|
||||
|
||||
self::assertSame(23000, $result->weight);
|
||||
self::assertSame(42, $result->dsd);
|
||||
self::assertNull($result->manualNumber);
|
||||
self::assertSame('AUTO', $result->mode);
|
||||
}
|
||||
|
||||
public function testManualModeKeepsWeightAndAllocatesDsd(): void
|
||||
{
|
||||
$siteProvider = $this->createStub(CurrentSiteProviderInterface::class);
|
||||
$siteProvider->method('get')->willReturn($this->site());
|
||||
|
||||
$allocator = $this->createStub(DsdAllocatorInterface::class);
|
||||
$allocator->method('next')->willReturn(43);
|
||||
|
||||
$processor = new WeighbridgeReadingProcessor(
|
||||
$siteProvider,
|
||||
$this->createStub(WeighbridgeReaderInterface::class),
|
||||
$allocator,
|
||||
);
|
||||
|
||||
$resource = new WeighbridgeReadingResource();
|
||||
$resource->mode = 'MANUAL';
|
||||
$resource->weight = 23187;
|
||||
$resource->manualNumber = 'PAP-555';
|
||||
|
||||
$result = $processor->process($resource, new Post());
|
||||
|
||||
self::assertSame(23187, $result->weight, 'Le poids saisi est conserve en manuel.');
|
||||
self::assertSame(43, $result->dsd);
|
||||
self::assertSame('PAP-555', $result->manualNumber);
|
||||
self::assertSame('MANUAL', $result->mode);
|
||||
}
|
||||
|
||||
public function testWeighbridgeUnavailableIsMappedTo503(): void
|
||||
{
|
||||
$siteProvider = $this->createStub(CurrentSiteProviderInterface::class);
|
||||
$siteProvider->method('get')->willReturn($this->site());
|
||||
|
||||
$reader = $this->createStub(WeighbridgeReaderInterface::class);
|
||||
$reader->method('read')->willThrowException(new WeighbridgeUnavailableException());
|
||||
|
||||
$processor = new WeighbridgeReadingProcessor(
|
||||
$siteProvider,
|
||||
$reader,
|
||||
$this->createStub(DsdAllocatorInterface::class),
|
||||
);
|
||||
|
||||
$resource = new WeighbridgeReadingResource();
|
||||
$resource->mode = 'AUTO';
|
||||
|
||||
try {
|
||||
$processor->process($resource, new Post());
|
||||
self::fail('Une ServiceUnavailableHttpException (503) etait attendue.');
|
||||
} catch (ServiceUnavailableHttpException $e) {
|
||||
self::assertSame(503, $e->getStatusCode());
|
||||
self::assertStringContainsString('pesée manuelle', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testMissingCurrentSiteIsRejected(): void
|
||||
{
|
||||
$siteProvider = $this->createStub(CurrentSiteProviderInterface::class);
|
||||
$siteProvider->method('get')->willReturn(null);
|
||||
|
||||
$processor = new WeighbridgeReadingProcessor(
|
||||
$siteProvider,
|
||||
$this->createStub(WeighbridgeReaderInterface::class),
|
||||
$this->createStub(DsdAllocatorInterface::class),
|
||||
);
|
||||
|
||||
$resource = new WeighbridgeReadingResource();
|
||||
$resource->mode = 'AUTO';
|
||||
|
||||
$this->expectException(BadRequestHttpException::class);
|
||||
$processor->process($resource, new Post());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Module\Logistique\Infrastructure\Service;
|
||||
|
||||
use App\Module\Logistique\Infrastructure\Service\DsdAllocator;
|
||||
use App\Module\Sites\Domain\Entity\Site;
|
||||
use Doctrine\DBAL\ArrayParameterType;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* Allocateur DSD (RG-5.04 / § 2.7) — test d'integration sur la table
|
||||
* `weighbridge_dsd_counter` (DBAL brut, verrou FOR UPDATE).
|
||||
*
|
||||
* Verifie l'increment sequentiel et l'isolation PAR SITE (un pont par site).
|
||||
* Les compteurs des sites touches sont remis a zero en debut de test et purges
|
||||
* en tearDown (pas de DAMA en local — nettoyage manuel obligatoire).
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class DsdAllocatorTest extends KernelTestCase
|
||||
{
|
||||
private Connection $connection;
|
||||
private DsdAllocator $allocator;
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
/** @var list<int> */
|
||||
private array $touchedSiteIds = [];
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
$container = self::getContainer();
|
||||
$this->em = $container->get('doctrine')->getManager();
|
||||
$this->connection = $this->em->getConnection();
|
||||
$this->allocator = $container->get(DsdAllocator::class);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ([] !== $this->touchedSiteIds) {
|
||||
$this->connection->executeStatement(
|
||||
'DELETE FROM weighbridge_dsd_counter WHERE site_id IN (?)',
|
||||
[$this->touchedSiteIds],
|
||||
[ArrayParameterType::INTEGER],
|
||||
);
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testNextIncrementsSequentiallyAndIsIsolatedPerSite(): void
|
||||
{
|
||||
$sites = $this->em->getRepository(Site::class)->findAll();
|
||||
self::assertGreaterThanOrEqual(2, \count($sites), 'Au moins 2 sites doivent etre seedes (fixtures).');
|
||||
|
||||
$siteA = $sites[0];
|
||||
$siteB = $sites[1];
|
||||
$this->resetCounter($siteA);
|
||||
$this->resetCounter($siteB);
|
||||
|
||||
// AUTO/MANUAL partagent le meme increment : la sequence demarre a 1.
|
||||
self::assertSame(1, $this->allocator->next($siteA));
|
||||
self::assertSame(2, $this->allocator->next($siteA));
|
||||
self::assertSame(3, $this->allocator->next($siteA));
|
||||
|
||||
// Isolation par site : le compteur de B est independant de celui de A.
|
||||
self::assertSame(1, $this->allocator->next($siteB));
|
||||
self::assertSame(2, $this->allocator->next($siteB));
|
||||
|
||||
// La sequence de A reprend la ou elle en etait (4), non perturbee par B.
|
||||
self::assertSame(4, $this->allocator->next($siteA));
|
||||
}
|
||||
|
||||
public function testNextStartsAtOneWhenNoCounterRowExists(): void
|
||||
{
|
||||
$site = $this->em->getRepository(Site::class)->findAll()[0];
|
||||
$this->resetCounter($site);
|
||||
|
||||
// Aucune ligne compteur pour ce site : le premier appel la cree (last=0)
|
||||
// et renvoie 1 (dernier + 1).
|
||||
self::assertSame(1, $this->allocator->next($site));
|
||||
}
|
||||
|
||||
/**
|
||||
* Supprime la ligne compteur du site pour repartir d'un etat connu, et
|
||||
* enregistre l'id pour la purge de tearDown.
|
||||
*/
|
||||
private function resetCounter(Site $site): void
|
||||
{
|
||||
$siteId = $site->getId();
|
||||
self::assertNotNull($siteId);
|
||||
|
||||
$this->connection->executeStatement(
|
||||
'DELETE FROM weighbridge_dsd_counter WHERE site_id = :site',
|
||||
['site' => $siteId],
|
||||
);
|
||||
|
||||
if (!\in_array($siteId, $this->touchedSiteIds, true)) {
|
||||
$this->touchedSiteIds[] = $siteId;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Module\Logistique\Infrastructure\Weighbridge;
|
||||
|
||||
use App\Module\Logistique\Application\Service\DsdAllocatorInterface;
|
||||
use App\Module\Logistique\Infrastructure\Weighbridge\RandomWeighbridgeReader;
|
||||
use App\Shared\Domain\Contract\SiteInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Stub du pont bascule (RG-5.06 / § 2.6).
|
||||
*
|
||||
* Verifie le contrat du stub livre au M5 : poids aleatoire borne a
|
||||
* [10000, 50000] kg et DSD delegue a l'allocateur (le chemin d'erreur 503
|
||||
* est couvert cote Processor — WeighbridgeReadingProcessorTest).
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class WeighbridgeReaderStubTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* RG-5.06 : sur un grand nombre de lectures, le poids reste toujours dans
|
||||
* l'intervalle borne [10000, 50000] (random_int inclusif aux deux bornes).
|
||||
*/
|
||||
public function testReadReturnsWeightWithinBounds(): void
|
||||
{
|
||||
$allocator = $this->createStub(DsdAllocatorInterface::class);
|
||||
$allocator->method('next')->willReturn(1);
|
||||
|
||||
$reader = new RandomWeighbridgeReader($allocator);
|
||||
$site = $this->createStub(SiteInterface::class);
|
||||
|
||||
for ($i = 0; $i < 500; ++$i) {
|
||||
$reading = $reader->read($site);
|
||||
self::assertGreaterThanOrEqual(10000, $reading->weight);
|
||||
self::assertLessThanOrEqual(50000, $reading->weight);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* RG-5.04 : le DSD renvoye par la lecture est celui fourni par l'allocateur
|
||||
* de site (le reader ne calcule pas le DSD lui-meme).
|
||||
*/
|
||||
public function testReadDelegatesDsdToAllocator(): void
|
||||
{
|
||||
$allocator = $this->createStub(DsdAllocatorInterface::class);
|
||||
$allocator->method('next')->willReturn(42);
|
||||
|
||||
$reader = new RandomWeighbridgeReader($allocator);
|
||||
$reading = $reader->read($this->createStub(SiteInterface::class));
|
||||
|
||||
self::assertSame(42, $reading->dsd);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user