|
| 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\Exception as DriverException; |
| 10 | +use Monolog\Handler\TestHandler; |
| 11 | +use Monolog\Level; |
| 12 | +use Monolog\Logger; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class ConnectionErrorDriverTest extends TestCase |
| 16 | +{ |
| 17 | + public function testConnectionPressureCodeIsLoggedAsCriticalWithoutSecrets(): void |
| 18 | + { |
| 19 | + $handler = new TestHandler(); |
| 20 | + $driver = $this->driverThrowing(2003, 'HY000'); // CR_CONN_HOST_ERROR |
| 21 | + |
| 22 | + $this->expectException(DriverException::class); |
| 23 | + |
| 24 | + try { |
| 25 | + (new ConnectionErrorDriver($driver, new Logger('database', [$handler]))) |
| 26 | + ->connect(['host' => 'db.internal', 'password' => 's3cr3t', 'user' => 'app']); |
| 27 | + } finally { |
| 28 | + $records = $handler->getRecords(); |
| 29 | + $this->assertCount(1, $records); |
| 30 | + $record = $records[0]; |
| 31 | + |
| 32 | + $this->assertSame(Level::Critical, $record->level); |
| 33 | + $this->assertSame('database', $record->channel); |
| 34 | + $this->assertSame('Database connection failed', $record->message); |
| 35 | + $this->assertSame('db.connection_error', $record->context['event']); |
| 36 | + $this->assertSame(2003, $record->context['db.error_code']); |
| 37 | + $this->assertSame('HY000', $record->context['db.sqlstate']); |
| 38 | + $this->assertSame('db.internal', $record->context['db.host']); |
| 39 | + |
| 40 | + // The middleware logs the host only — never the password or DSN. |
| 41 | + $serialised = json_encode($record->toArray(), JSON_THROW_ON_ERROR | JSON_PARTIAL_OUTPUT_ON_ERROR); |
| 42 | + $this->assertStringNotContainsString('s3cr3t', $serialised); |
| 43 | + $this->assertArrayNotHasKey('password', $record->context); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public function testCredentialErrorIsLoggedAsError(): void |
| 48 | + { |
| 49 | + $handler = new TestHandler(); |
| 50 | + $driver = $this->driverThrowing(1045, '28000'); // ER_ACCESS_DENIED_ERROR |
| 51 | + |
| 52 | + try { |
| 53 | + (new ConnectionErrorDriver($driver, new Logger('database', [$handler]))) |
| 54 | + ->connect(['host' => 'db.internal']); |
| 55 | + $this->fail('Expected the driver exception to be rethrown.'); |
| 56 | + } catch (DriverException) { |
| 57 | + $record = $handler->getRecords()[0]; |
| 58 | + $this->assertSame(Level::Error, $record->level); |
| 59 | + $this->assertSame(1045, $record->context['db.error_code']); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private function driverThrowing(int $code, string $sqlState): Driver |
| 64 | + { |
| 65 | + $exception = new class($code, $sqlState) extends \RuntimeException implements DriverException { |
| 66 | + public function __construct( |
| 67 | + int $code, |
| 68 | + private readonly string $sqlState, |
| 69 | + ) { |
| 70 | + parent::__construct('driver failure', $code); |
| 71 | + } |
| 72 | + |
| 73 | + public function getSQLState(): ?string |
| 74 | + { |
| 75 | + return $this->sqlState; |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + $driver = $this->createMock(Driver::class); |
| 80 | + $driver->method('connect')->willThrowException($exception); |
| 81 | + |
| 82 | + return $driver; |
| 83 | + } |
| 84 | +} |
0 commit comments