-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PdoDriver: check for misconfigured PDO connections resource (#294)
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/** | ||
* @dataProvider ../databases.ini != nothing, pdo | ||
*/ | ||
|
||
// Background: | ||
// When PDO connection is passed into Dibi it can be in (re)configured in various ways. | ||
// This affects how connection is then internally handled. | ||
// There should be no visible difference in Dibi behaviour regardless of PDO configuration (except unsupported configurations). | ||
|
||
|
||
declare(strict_types=1); | ||
|
||
use Tester\Assert; | ||
|
||
require __DIR__ . '/bootstrap.php'; | ||
|
||
|
||
function buildPDOConnection(int $errorMode = null): PDO | ||
{ | ||
global $config; | ||
|
||
// used to parse config, establish connection | ||
$connection = new \Dibi\Connection($config); | ||
$dibiDriver = $connection->getDriver(); | ||
\assert($dibiDriver instanceof \Dibi\Drivers\PdoDriver); | ||
|
||
// hack: extract PDO connection from driver (no public interface for that) | ||
$connectionProperty = (new ReflectionClass($dibiDriver)) | ||
->getProperty('connection'); | ||
$connectionProperty->setAccessible(true); | ||
$pdo = $connectionProperty->getValue($dibiDriver); | ||
\assert($pdo instanceof PDO); | ||
|
||
// check that error reporting is in PHPs default value | ||
\assert($pdo->getAttribute(\PDO::ATTR_ERRMODE) === \PDO::ERRMODE_SILENT); | ||
|
||
// override PDO error mode if provided | ||
if ($errorMode !== null) { | ||
$pdo->setAttribute(\PDO::ATTR_ERRMODE, $errorMode); | ||
} | ||
return $pdo; | ||
} | ||
|
||
|
||
/** @throws \Dibi\NotSupportedException */ | ||
function buildPdoDriverWithProvidedConnection(PDO $pdo): \Dibi\Drivers\PdoDriver | ||
{ | ||
$driverConfig = ['resource' => $pdo]; | ||
return new \Dibi\Drivers\PdoDriver($driverConfig); | ||
} | ||
|
||
|
||
// PDO error mode: exception | ||
Assert::exception(function () { | ||
$pdoConnection = buildPDOConnection(\PDO::ERRMODE_EXCEPTION); | ||
buildPdoDriverWithProvidedConnection($pdoConnection); | ||
}, \Dibi\DriverException::class, 'PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); | ||
|
||
|
||
// PDO error mode: warning | ||
Assert::exception(function () { | ||
$pdoConnection = buildPDOConnection(\PDO::ERRMODE_WARNING); | ||
buildPdoDriverWithProvidedConnection($pdoConnection); | ||
}, \Dibi\DriverException::class, 'PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); | ||
|
||
|
||
// PDO error mode: explicitly set silent | ||
test(function () { | ||
$pdoConnection = buildPDOConnection(\PDO::ERRMODE_SILENT); | ||
Assert::type(\Dibi\Drivers\PdoDriver::class, buildPdoDriverWithProvidedConnection($pdoConnection)); | ||
}); | ||
|
||
|
||
// PDO error mode: implicitly set silent | ||
test(function () { | ||
$pdoConnection = buildPDOConnection(null); | ||
Assert::type(\Dibi\Drivers\PdoDriver::class, buildPdoDriverWithProvidedConnection($pdoConnection)); | ||
}); |