|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Tests\Doctrine\Middleware; |
| 6 | + |
| 7 | +use App\Doctrine\Middleware\ConnectionErrorDriver; |
| 8 | +use Doctrine\DBAL\Driver; |
| 9 | +use Doctrine\DBAL\Driver\Connection as DriverConnection; |
| 10 | +use Doctrine\DBAL\Driver\Exception as DriverException; |
| 11 | +use Monolog\Handler\TestHandler; |
| 12 | +use Monolog\Level; |
| 13 | +use Monolog\Logger; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Psr\Log\LoggerInterface; |
| 16 | + |
| 17 | +class ConnectionErrorDriverTest extends TestCase |
| 18 | +{ |
| 19 | + public function testConnectionPressureCodeIsLoggedAsCriticalWithoutSecrets(): void |
| 20 | + { |
| 21 | + $handler = new TestHandler(); |
| 22 | + $driver = $this->driverThrowing(2003, 'HY000'); // CR_CONN_HOST_ERROR |
| 23 | + |
| 24 | + $this->expectException(DriverException::class); |
| 25 | + |
| 26 | + try { |
| 27 | + (new ConnectionErrorDriver($driver, new Logger('database', [$handler]))) |
| 28 | + ->connect(['host' => 'db.internal', 'password' => 's3cr3t', 'user' => 'app']); |
| 29 | + } finally { |
| 30 | + $records = $handler->getRecords(); |
| 31 | + $this->assertCount(1, $records); |
| 32 | + $record = $records[0]; |
| 33 | + |
| 34 | + $this->assertSame(Level::Critical, $record->level); |
| 35 | + $this->assertSame('database', $record->channel); |
| 36 | + $this->assertSame('Database connection failed', $record->message); |
| 37 | + $this->assertSame('db.connection_error', $record->context['event']); |
| 38 | + $this->assertSame('2003', $record->context['db.response.status_code']); |
| 39 | + $this->assertSame('connection_refused', $record->context['error.type']); |
| 40 | + $this->assertSame('HY000', $record->context['db.sqlstate']); |
| 41 | + $this->assertSame('db.internal', $record->context['server.address']); |
| 42 | + // The fixture passes no port, so server.port is omitted (OTel omit-when-absent). |
| 43 | + $this->assertArrayNotHasKey('server.port', $record->context); |
| 44 | + |
| 45 | + // The middleware logs the host only — never the password or DSN. |
| 46 | + $serialised = json_encode($record->toArray(), JSON_THROW_ON_ERROR | JSON_PARTIAL_OUTPUT_ON_ERROR); |
| 47 | + $this->assertStringNotContainsString('s3cr3t', $serialised); |
| 48 | + $this->assertArrayNotHasKey('password', $record->context); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public function testCredentialErrorIsLoggedAsError(): void |
| 53 | + { |
| 54 | + $handler = new TestHandler(); |
| 55 | + $driver = $this->driverThrowing(1045, '28000'); // ER_ACCESS_DENIED_ERROR |
| 56 | + |
| 57 | + try { |
| 58 | + (new ConnectionErrorDriver($driver, new Logger('database', [$handler]))) |
| 59 | + ->connect(['host' => 'db.internal']); |
| 60 | + $this->fail('Expected the driver exception to be rethrown.'); |
| 61 | + } catch (DriverException) { |
| 62 | + $record = $handler->getRecords()[0]; |
| 63 | + $this->assertSame(Level::Error, $record->level); |
| 64 | + $this->assertSame('1045', $record->context['db.response.status_code']); |
| 65 | + $this->assertSame('access_denied', $record->context['error.type']); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public function testSuccessfulConnectionIsNotLoggedAndPassesThrough(): void |
| 70 | + { |
| 71 | + $handler = new TestHandler(); |
| 72 | + $connection = $this->createMock(DriverConnection::class); |
| 73 | + $driver = $this->createMock(Driver::class); |
| 74 | + $driver->method('connect')->willReturn($connection); |
| 75 | + |
| 76 | + $result = (new ConnectionErrorDriver($driver, new Logger('database', [$handler]))) |
| 77 | + ->connect(['host' => 'db.internal']); |
| 78 | + |
| 79 | + // The wrapped connection passes through untouched and nothing is logged. |
| 80 | + $this->assertSame($connection, $result); |
| 81 | + $this->assertSame([], $handler->getRecords()); |
| 82 | + } |
| 83 | + |
| 84 | + public function testOriginalExceptionInstanceIsRethrown(): void |
| 85 | + { |
| 86 | + $handler = new TestHandler(); |
| 87 | + $exception = $this->driverException(2003, 'HY000'); |
| 88 | + $driver = $this->driverThrowingException($exception); |
| 89 | + |
| 90 | + try { |
| 91 | + (new ConnectionErrorDriver($driver, new Logger('database', [$handler]))) |
| 92 | + ->connect(['host' => 'db.internal']); |
| 93 | + $this->fail('Expected the driver exception to be rethrown.'); |
| 94 | + } catch (DriverException $caught) { |
| 95 | + // The exact original instance must propagate — not a wrapper that |
| 96 | + // would drop the driver code / previous cause DBAL relies on. |
| 97 | + $this->assertSame($exception, $caught); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public function testThrowingLoggerDoesNotMaskTheConnectionError(): void |
| 102 | + { |
| 103 | + $exception = $this->driverException(2003, 'HY000'); |
| 104 | + $driver = $this->driverThrowingException($exception); |
| 105 | + |
| 106 | + // A logger whose write fails — e.g. an unwritable LOG_PATH during the |
| 107 | + // very outage this middleware reports on. |
| 108 | + $logger = $this->createMock(LoggerInterface::class); |
| 109 | + $logger->method('log')->willThrowException(new \RuntimeException('log sink unavailable')); |
| 110 | + |
| 111 | + try { |
| 112 | + (new ConnectionErrorDriver($driver, $logger))->connect(['host' => 'db.internal']); |
| 113 | + $this->fail('Expected the driver exception to be rethrown.'); |
| 114 | + } catch (\Throwable $caught) { |
| 115 | + // The real DB connection error wins; the logging failure is swallowed. |
| 116 | + $this->assertSame($exception, $caught); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private function driverThrowing(int $code, string $sqlState): Driver |
| 121 | + { |
| 122 | + return $this->driverThrowingException($this->driverException($code, $sqlState)); |
| 123 | + } |
| 124 | + |
| 125 | + private function driverException(int $code, string $sqlState): DriverException |
| 126 | + { |
| 127 | + return new class($code, $sqlState) extends \RuntimeException implements DriverException { |
| 128 | + public function __construct( |
| 129 | + int $code, |
| 130 | + private readonly string $sqlState, |
| 131 | + ) { |
| 132 | + parent::__construct('driver failure', $code); |
| 133 | + } |
| 134 | + |
| 135 | + public function getSQLState(): ?string |
| 136 | + { |
| 137 | + return $this->sqlState; |
| 138 | + } |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + private function driverThrowingException(DriverException $exception): Driver |
| 143 | + { |
| 144 | + $driver = $this->createMock(Driver::class); |
| 145 | + $driver->method('connect')->willThrowException($exception); |
| 146 | + |
| 147 | + return $driver; |
| 148 | + } |
| 149 | +} |
0 commit comments