Skip to content

Commit 1f7da72

Browse files
jkuchardg
authored andcommitted
PdoDriver: check for misconfigured PDO connections resource (#294)
1 parent e880631 commit 1f7da72

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/Dibi/Drivers/PdoDriver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public function __construct(array &$config)
6969
}
7070
}
7171

72+
if ($this->connection->getAttribute(PDO::ATTR_ERRMODE) !== PDO::ERRMODE_SILENT) {
73+
throw new Dibi\DriverException('PDO connection in exception or warning error mode is not supported.');
74+
}
75+
7276
$this->driverName = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME);
7377
$this->serverVersion = (string) ($config['version'] ?? @$this->connection->getAttribute(PDO::ATTR_SERVER_VERSION)); // @ - may be not supported
7478
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tester\Assert;
6+
7+
require __DIR__ . '/bootstrap.php';
8+
9+
10+
function buildPdoDriver(?int $errorMode): Dibi\Drivers\PdoDriver
11+
{
12+
$pdo = new PDO('sqlite::memory:');
13+
if ($errorMode !== null) {
14+
$pdo->setAttribute(PDO::ATTR_ERRMODE, $errorMode);
15+
}
16+
$config = ['resource' => $pdo];
17+
return new Dibi\Drivers\PdoDriver($config);
18+
}
19+
20+
21+
// PDO error mode: exception
22+
Assert::exception(function () {
23+
buildPdoDriver(PDO::ERRMODE_EXCEPTION);
24+
}, Dibi\DriverException::class, 'PDO connection in exception or warning error mode is not supported.');
25+
26+
27+
// PDO error mode: warning
28+
Assert::exception(function () {
29+
buildPdoDriver(PDO::ERRMODE_WARNING);
30+
}, Dibi\DriverException::class, 'PDO connection in exception or warning error mode is not supported.');
31+
32+
33+
// PDO error mode: explicitly set silent
34+
test(function () {
35+
buildPdoDriver(PDO::ERRMODE_SILENT);
36+
});
37+
38+
39+
// PDO error mode: implicitly set silent
40+
test(function () {
41+
buildPdoDriver(null);
42+
});

0 commit comments

Comments
 (0)