mirror of
https://github.com/php/php-src.git
synced 2026-03-24 00:02:20 +01:00
Optimized pdo_pgsql connection test (#12454)
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
--TEST--
|
||||
GitHub #12424 (Fix GH-12423: [pdo_pgsql] Changed to prioritize DSN authentication information over arguments.)
|
||||
--EXTENSIONS--
|
||||
pdo
|
||||
pdo_pgsql
|
||||
--SKIPIF--
|
||||
<?php
|
||||
if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
|
||||
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
|
||||
require __DIR__ . '/config.inc';
|
||||
if (strpos($config['ENV']['PDOTEST_DSN'], 'password=') === false && !isset($config['ENV']['PDOTEST_PASS'])) {
|
||||
die('skip no password');
|
||||
}
|
||||
PDOTest::skip();
|
||||
?>
|
||||
--FILE--
|
||||
@@ -15,53 +20,55 @@ $dsnWithCredentials = $config['ENV']['PDOTEST_DSN'];
|
||||
$user = $config['ENV']['PDOTEST_USER'] ?? null;
|
||||
$password = $config['ENV']['PDOTEST_PASS'] ?? null;
|
||||
if (!$user) {
|
||||
preg_match('/user=(.*?) /', $dsnWithCredentials, $match);
|
||||
preg_match('/user=([^ ]*)/', $dsnWithCredentials, $match);
|
||||
$user = $match[1] ?? '';
|
||||
}
|
||||
if (!$password) {
|
||||
preg_match('/password=(.*?)$/', $dsnWithCredentials, $match);
|
||||
preg_match('/password=([^ ]*)/', $dsnWithCredentials, $match);
|
||||
$password = $match[1] ?? '';
|
||||
}
|
||||
$dsn = str_replace(" user={$user} password={$password}", '', $dsnWithCredentials);
|
||||
$dsn = str_replace("user={$user}", '', $dsnWithCredentials);
|
||||
$dsn = str_replace("password={$password}", '', $dsn);
|
||||
$dsn = rtrim($dsn);
|
||||
|
||||
echo "dsn without credentials / correct user / correct password\n";
|
||||
echo "dsn without credentials / correct user / correct password".PHP_EOL;
|
||||
try {
|
||||
$db = new PDO($dsn, $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
||||
echo "Connected.\n\n";
|
||||
echo "Connected.".PHP_EOL.PHP_EOL;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
echo "dsn with credentials / no user / no password\n";
|
||||
echo "dsn with credentials / no user / no password".PHP_EOL;
|
||||
try {
|
||||
$db = new PDO("{$dsn} user={$user} password={$password}", null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
||||
echo "Connected.\n\n";
|
||||
echo "Connected.".PHP_EOL.PHP_EOL;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
echo "dsn with correct user / incorrect user / correct password\n";
|
||||
echo "dsn with correct user / incorrect user / correct password".PHP_EOL;
|
||||
try {
|
||||
$db = new PDO("{$dsn} user={$user}", 'hoge', $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
||||
echo "Connected.\n\n";
|
||||
echo "Connected.".PHP_EOL.PHP_EOL;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
echo "dsn with correct password / correct user / incorrect password\n";
|
||||
echo "dsn with correct password / correct user / incorrect password".PHP_EOL;
|
||||
try {
|
||||
$db = new PDO("{$dsn} password={$password}", $user, 'fuga', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
||||
echo "Connected.\n\n";
|
||||
echo "Connected.".PHP_EOL.PHP_EOL;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
echo "dsn with correct credentials / incorrect user / incorrect password\n";
|
||||
echo "dsn with correct credentials / incorrect user / incorrect password".PHP_EOL;
|
||||
try {
|
||||
$db = new PDO("{$dsn} user={$user} password={$password}", 'hoge', 'fuga', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
||||
echo "Connected.\n";
|
||||
echo "Connected.".PHP_EOL;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
|
||||
Reference in New Issue
Block a user