test(fer-19) : couverture du mapping PontBasculeHealthProvider
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
78
tests/State/PontBasculeHealthProviderTest.php
Normal file
78
tests/State/PontBasculeHealthProviderTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user