[#FER-19] Ajouter le healthCheck du pont bascule (!58)
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled

| Numéro du ticket | Titre du ticket |
|------------------|-----------------|
|                  |                 |

## Description de la PR

## 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: #58
Co-authored-by: tristan <tristan@yuno.malio.fr>
Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #58.
This commit is contained in:
2026-05-21 12:38:32 +00:00
committed by Autin
parent 16ebede557
commit fd6aed8ae0
13 changed files with 1229 additions and 7 deletions

View File

@@ -47,7 +47,7 @@ final class PontBasculeServiceTest extends TestCase
$httpClient
->expects(self::once())
->method('request')
->with('POST', 'http://example.test')
->with('POST', 'http://example.test/send/dsd')
->willReturn($response)
;
@@ -82,4 +82,103 @@ final class PontBasculeServiceTest extends TestCase
$service->fetch();
}
public function testCheckHealthBypassIsHealthyWithoutHttpCall(): void
{
$httpClient = $this->createMock(HttpClientInterface::class);
$httpClient->expects(self::never())->method('request');
$service = new PontBasculeService($httpClient, new PontBasculePayloadDecoder(), 'http://example.test', true);
$health = $service->checkHealth();
self::assertTrue($health->isHealthy());
}
public function testCheckHealthHealthyPayload(): void
{
$service = $this->serviceForHealthBody(json_encode([
'ok' => true,
'busy' => false,
'port_connected' => true,
'port_error' => null,
'hostname' => 'liot-rasp-ferme-01',
], JSON_THROW_ON_ERROR));
$health = $service->checkHealth();
self::assertTrue($health->isHealthy());
self::assertSame('liot-rasp-ferme-01', $health->getHostname());
}
public function testCheckHealthUnhealthyWhenPortError(): void
{
$service = $this->serviceForHealthBody(json_encode([
'ok' => true,
'busy' => false,
'port_connected' => true,
'port_error' => 'device disconnected',
], JSON_THROW_ON_ERROR));
self::assertFalse($service->checkHealth()->isHealthy());
}
public function testCheckHealthUnhealthyWhenPortNotConnected(): void
{
$service = $this->serviceForHealthBody(json_encode([
'ok' => true,
'busy' => false,
'port_connected' => false,
'port_error' => null,
], JSON_THROW_ON_ERROR));
self::assertFalse($service->checkHealth()->isHealthy());
}
public function testCheckHealthUnhealthyWhenBusy(): void
{
$service = $this->serviceForHealthBody(json_encode([
'ok' => true,
'busy' => true,
'port_connected' => true,
'port_error' => null,
], JSON_THROW_ON_ERROR));
self::assertFalse($service->checkHealth()->isHealthy());
}
public function testCheckHealthUnhealthyOnTransportFailure(): void
{
$httpClient = $this->createMock(HttpClientInterface::class);
$httpClient
->expects(self::once())
->method('request')
->willThrowException($this->createStub(TransportExceptionInterface::class))
;
$service = new PontBasculeService($httpClient, new PontBasculePayloadDecoder(), 'http://example.test', false);
self::assertFalse($service->checkHealth()->isHealthy());
}
public function testCheckHealthUnhealthyOnInvalidJson(): void
{
self::assertFalse($this->serviceForHealthBody('not-json')->checkHealth()->isHealthy());
}
private function serviceForHealthBody(string $body): PontBasculeService
{
$response = $this->createStub(ResponseInterface::class);
$response->method('getContent')->with(false)->willReturn($body);
$httpClient = $this->createMock(HttpClientInterface::class);
$httpClient
->expects(self::once())
->method('request')
->with('GET', 'http://example.test/health')
->willReturn($response)
;
return new PontBasculeService($httpClient, new PontBasculePayloadDecoder(), 'http://example.test', false);
}
}

View File

@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);
namespace App\Tests\State;
use ApiPlatform\Metadata\Get;
use App\ApiResource\PontBasculeHealthCheck;
use App\Service\PontBasculePayloadDecoder;
use App\Service\PontBasculeService;
use App\State\PontBasculeHealthProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
/**
* @internal
*/
final class PontBasculeHealthProviderTest extends TestCase
{
public function testProvideMapsHealthyPayloadToResource(): void
{
$provider = $this->providerForHealthBody(json_encode([
'ok' => true,
'busy' => false,
'port_connected' => true,
'port_error' => null,
'hostname' => 'liot-rasp-ferme-01',
], JSON_THROW_ON_ERROR));
$result = $provider->provide(new Get());
self::assertInstanceOf(PontBasculeHealthCheck::class, $result);
self::assertTrue($result->healthy);
self::assertTrue($result->ok);
self::assertFalse($result->busy);
self::assertTrue($result->portConnected);
self::assertNull($result->portError);
self::assertSame('liot-rasp-ferme-01', $result->hostname);
}
public function testProvideMapsUnhealthyPayloadWithPortError(): void
{
$provider = $this->providerForHealthBody(json_encode([
'ok' => true,
'busy' => true,
'port_connected' => false,
'port_error' => 'device disconnected',
'hostname' => 'liot-rasp-ferme-01',
], JSON_THROW_ON_ERROR));
$result = $provider->provide(new Get());
self::assertFalse($result->healthy);
self::assertTrue($result->ok);
self::assertTrue($result->busy);
self::assertFalse($result->portConnected);
self::assertSame('device disconnected', $result->portError);
}
private function providerForHealthBody(string $body): PontBasculeHealthProvider
{
$response = $this->createStub(ResponseInterface::class);
$response->method('getContent')->willReturn($body);
$httpClient = $this->createMock(HttpClientInterface::class);
$httpClient
->expects(self::once())
->method('request')
->with('GET', 'http://example.test/health')
->willReturn($response)
;
$service = new PontBasculeService($httpClient, new PontBasculePayloadDecoder(), 'http://example.test', false);
return new PontBasculeHealthProvider($service);
}
}