Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
]
},
"config": {
"platform": {
"php": "8.2"
},
"preferred-install": {
"*": "dist"
},
Expand Down
5 changes: 4 additions & 1 deletion src/Controller/HealthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use OpenConext\MonitorBundle\HealthCheck\HealthCheckChain;
use OpenConext\MonitorBundle\Value\HealthReport;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
Expand All @@ -34,7 +35,8 @@
class HealthController extends AbstractController
{
public function __construct(
private readonly HealthCheckChain $healthChecker
private readonly HealthCheckChain $healthChecker,
private readonly LoggerInterface $logger,
) {
}

Expand All @@ -45,6 +47,7 @@ public function __invoke(): JsonResponse
try {
$statusResponse = $this->healthChecker->check();
} catch (Throwable $exception) {
$this->logger->error('An unexpected error occurred during health checking.', ['exception' => $exception]);
$statusResponse = HealthReport::buildStatusDown($exception->getMessage());
}
return $this->json($statusResponse, $statusResponse->getStatusCode());
Expand Down
11 changes: 7 additions & 4 deletions src/HealthCheck/DoctrineConnectionHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Doctrine\DBAL\Exception\ConnectionException;
use Exception;
use OpenConext\MonitorBundle\Value\HealthReport;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/**
Expand All @@ -36,8 +37,8 @@ class DoctrineConnectionHealthCheck implements HealthCheckInterface
public function __construct(
#[Autowire(service: 'doctrine.dbal.default_connection')]
private readonly ?Connection $connection,
)
{
private readonly LoggerInterface $logger,
) {
}

public function check(HealthReportInterface $report): HealthReportInterface
Expand Down Expand Up @@ -66,9 +67,11 @@ public function check(HealthReportInterface $report): HealthReportInterface
$query = "SELECT * FROM %s LIMIT 1";
$this->connection->executeQuery(sprintf($query, $table->getName()));

} catch (ConnectionException) {
} catch (ConnectionException $exception) {
$this->logger->error('Unable to connect to the database.', ['exception' => $exception]);
return HealthReport::buildStatusDown('Unable to connect to the database.');
} catch (Exception) {
} catch (Exception $exception) {
$this->logger->error('Unable to execute a query on the database.', ['exception' => $exception]);
return HealthReport::buildStatusDown('Unable to execute a query on the database.');
}

Expand Down
121 changes: 121 additions & 0 deletions tests/HealthCheck/DoctrineConnectionHealthCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/**
* Copyright 2026 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OpenConext\MonitorBundle\Tests\HealthCheck;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\ConnectionException;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Table;
use Exception;
use Mockery as m;
use OpenConext\MonitorBundle\HealthCheck\DoctrineConnectionHealthCheck;
use OpenConext\MonitorBundle\Value\HealthReport;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class DoctrineConnectionHealthCheckTest extends TestCase
{
protected function tearDown(): void
{
m::close();
parent::tearDown();
}

public function testSkipsCheckWhenConnectionIsNull(): void
{
$logger = m::mock(LoggerInterface::class);
$logger->shouldNotReceive('error');

$check = new DoctrineConnectionHealthCheck(null, $logger);
$report = HealthReport::buildStatusUp();
$result = $check->check($report);

$this->assertFalse($result->isDown());
}

public function testLogsErrorOnConnectionException(): void
{
$exception = m::mock(ConnectionException::class);

$connection = m::mock(Connection::class);
$connection->shouldAllowMockingProtectedMethods();
// Note: Connection::connect() is protected in Doctrine DBAL 4 and cannot be made to throw directly.
// The exception is thrown from createSchemaManager() to exercise the ConnectionException catch block.
$connection->shouldReceive('connect');
$connection->shouldReceive('createSchemaManager')->andThrow($exception);

$logger = m::mock(LoggerInterface::class);
$logger->shouldReceive('error')
->once()
->with('Unable to connect to the database.', ['exception' => $exception]);

$check = new DoctrineConnectionHealthCheck($connection, $logger);
$result = $check->check(HealthReport::buildStatusUp());

$this->assertTrue($result->isDown());
$this->assertEquals('Unable to connect to the database.', $result->jsonSerialize()['message']);
}

public function testLogsErrorOnGenericException(): void
{
$exception = new Exception('Query failed');

$connection = m::mock(Connection::class);
$connection->shouldAllowMockingProtectedMethods();
// Note: Connection::connect() is protected in Doctrine DBAL 4 and cannot be made to throw directly.
// The exception is thrown from createSchemaManager() to exercise the generic Exception catch block.
$connection->shouldReceive('connect');
$connection->shouldReceive('createSchemaManager')->andThrow($exception);

$logger = m::mock(LoggerInterface::class);
$logger->shouldReceive('error')
->once()
->with('Unable to execute a query on the database.', ['exception' => $exception]);

$check = new DoctrineConnectionHealthCheck($connection, $logger);
$result = $check->check(HealthReport::buildStatusUp());

$this->assertTrue($result->isDown());
$this->assertEquals('Unable to execute a query on the database.', $result->jsonSerialize()['message']);
}

public function testReturnsUpReportWhenDatabaseIsHealthy(): void
{
$table = m::mock(Table::class);
$table->shouldReceive('getName')->andReturn('some_table');

$schemaManager = m::mock(AbstractSchemaManager::class);
$schemaManager->shouldReceive('listTables')->andReturn([$table]);

$connection = m::mock(Connection::class);
$connection->shouldAllowMockingProtectedMethods();
$connection->shouldReceive('connect');
$connection->shouldReceive('createSchemaManager')->andReturn($schemaManager);
$connection->shouldReceive('executeQuery');

$logger = m::mock(LoggerInterface::class);
$logger->shouldNotReceive('error');

$report = HealthReport::buildStatusUp();
$check = new DoctrineConnectionHealthCheck($connection, $logger);
$result = $check->check($report);

$this->assertSame($report, $result);
}
}
Loading