58 lines
1.9 KiB
PHP
58 lines
1.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Tests\Functional;
|
||
|
|
|
||
|
|
use Doctrine\DBAL\Connection;
|
||
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||
|
|
|
||
|
|
final class HealthApiTest extends WebTestCase
|
||
|
|
{
|
||
|
|
public function testItReportsHealthyWithoutAuthentication(): void
|
||
|
|
{
|
||
|
|
$client = static::createClient();
|
||
|
|
|
||
|
|
$client->request('GET', '/api/health');
|
||
|
|
|
||
|
|
$response = $client->getResponse();
|
||
|
|
|
||
|
|
self::assertSame(
|
||
|
|
200,
|
||
|
|
$response->getStatusCode(),
|
||
|
|
'GET /api/health must return 200 and require no authentication.',
|
||
|
|
);
|
||
|
|
|
||
|
|
$body = json_decode((string) $response->getContent(), true);
|
||
|
|
|
||
|
|
self::assertIsArray($body);
|
||
|
|
self::assertSame('ok', $body['status'] ?? null);
|
||
|
|
self::assertSame('ok', $body['db'] ?? null);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testItReportsUnhealthyWhenTheDatabaseIsUnreachable(): void
|
||
|
|
{
|
||
|
|
$client = static::createClient();
|
||
|
|
|
||
|
|
// Swap the DBAL connection for a stub that fails the ping, simulating an
|
||
|
|
// unreachable database without touching the real one. Overriding the
|
||
|
|
// bare Connection::class id is what the controller actually receives:
|
||
|
|
// autowiring keys by FQCN, so the `Connection $connection` parameter
|
||
|
|
// resolves to this same service.
|
||
|
|
$connection = $this->createStub(Connection::class);
|
||
|
|
$connection->method('executeQuery')
|
||
|
|
->willThrowException(new \RuntimeException('connection refused'));
|
||
|
|
static::getContainer()->set(Connection::class, $connection);
|
||
|
|
|
||
|
|
$client->request('GET', '/api/health');
|
||
|
|
|
||
|
|
$response = $client->getResponse();
|
||
|
|
|
||
|
|
self::assertSame(503, $response->getStatusCode());
|
||
|
|
|
||
|
|
$body = json_decode((string) $response->getContent(), true);
|
||
|
|
|
||
|
|
self::assertIsArray($body);
|
||
|
|
self::assertSame('error', $body['status'] ?? null);
|
||
|
|
self::assertSame('unreachable', $body['db'] ?? null);
|
||
|
|
}
|
||
|
|
}
|