Files
Ferme/tests/Service/PontBasculeServiceTest.php
AUTIN Tristan 4a77449a41 [#203] Réceptions — Parcours de pesée multi-étapes (première partie) (!3)
| Numéro du ticket | Titre du ticket |
|------------------|-----------------|
|        #203          |      Réceptions — Parcours de pesée multi-étapes         |

## Description de la PR
[#203] Réceptions — Parcours de pesée multi-étapes

## Modification du .env

## Check list

- [x] Pas de régression
- [x] TU/TI/TF rédigée
- [x] TU/TI/TF OK
- [x] CHANGELOG modifié

Reviewed-on: #3
Reviewed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr>
Co-authored-by: AUTIN Tristan <tristan@yuno.malio.fr>
Co-committed-by: AUTIN Tristan <tristan@yuno.malio.fr>
2026-01-14 07:17:34 +00:00

86 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\Exception\PontBasculeException;
use App\Service\PontBasculePayloadDecoder;
use App\Service\PontBasculeService;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
/**
* @internal
*/
final class PontBasculeServiceTest extends TestCase
{
public function testFetchBypassUsesDecoder(): void
{
$decoder = new PontBasculePayloadDecoder();
$httpClient = $this->createMock(HttpClientInterface::class);
$httpClient->expects(self::never())->method('request');
$service = new PontBasculeService($httpClient, $decoder, 'http://example.test', true);
$result = $service->fetch();
self::assertSame(121, $result->getDsd());
self::assertSame(1420.0, $result->getWeight());
self::assertInstanceOf(DateTimeImmutable::class, $result->getWeighedAt());
}
public function testFetchUsesHttpClientWhenNotBypass(): void
{
$payload = json_encode([
'response_ascii' => "\u{0001}\u{0002}040200\u{0002}03000123.kg \u{0002}9900042",
], JSON_THROW_ON_ERROR);
$response = $this->createMock(ResponseInterface::class);
$response->expects(self::once())->method('getContent')->with(false)->willReturn($payload);
$httpClient = $this->createMock(HttpClientInterface::class);
$httpClient
->expects(self::once())
->method('request')
->with('POST', 'http://example.test')
->willReturn($response)
;
$decoder = new PontBasculePayloadDecoder();
$service = new PontBasculeService($httpClient, $decoder, 'http://example.test', false);
$result = $service->fetch();
self::assertSame(42, $result->getDsd());
self::assertSame(123.0, $result->getWeight());
self::assertInstanceOf(DateTimeImmutable::class, $result->getWeighedAt());
}
public function testFetchThrowsOnTransportFailure(): void
{
$exception = $this->createStub(TransportExceptionInterface::class);
$httpClient = $this->createMock(HttpClientInterface::class);
$httpClient
->expects(self::once())
->method('request')
->willThrowException($exception)
;
$decoder = new PontBasculePayloadDecoder();
$service = new PontBasculeService($httpClient, $decoder, 'http://example.test', false);
$this->expectException(PontBasculeException::class);
$this->expectExceptionMessage('Erreur lors de la communication avec le pont bascule:');
$service->fetch();
}
}