buckets/tests/Functional/HealthApiTest.php
myrmidex f16e279f58
All checks were successful
CI / backend (push) Successful in 20m34s
CI / frontend (push) Successful in 1m2s
CI / e2e (push) Successful in 21m57s
35 - Add prod image SPA serving, health endpoint, and Forgejo CI
2026-06-30 21:07:45 +02:00

57 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);
}
}