2018-11-17 02:20:54 +00:00
|
|
|
<?php
|
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-12-12 17:42:40 +00:00
|
|
|
namespace JKingWeb\Arsse\TestCase\Db\PostgreSQLPDO;
|
2018-11-17 02:20:54 +00:00
|
|
|
|
2018-11-22 18:30:13 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
2018-12-12 16:15:07 +00:00
|
|
|
use JKingWeb\Arsse\Db\PostgreSQL\PDODriver as Driver;
|
2018-11-17 02:20:54 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-10 17:39:09 +00:00
|
|
|
* @group slow
|
2018-12-12 16:15:07 +00:00
|
|
|
* @covers \JKingWeb\Arsse\Db\PostgreSQL\PDODriver<extended> */
|
2018-11-22 18:30:13 +00:00
|
|
|
class TestCreation extends \JKingWeb\Arsse\Test\AbstractTest {
|
2018-12-14 00:47:51 +00:00
|
|
|
public function setUp() {
|
|
|
|
if (!Driver::requirementsMet()) {
|
|
|
|
$this->markTestSkipped("PDO-PostgreSQL extension not loaded");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-17 02:20:54 +00:00
|
|
|
/** @dataProvider provideConnectionStrings */
|
|
|
|
public function testGenerateConnectionString(bool $pdo, string $user, string $pass, string $db, string $host, int $port, string $service, string $exp) {
|
2018-11-23 00:55:54 +00:00
|
|
|
self::setConf();
|
2018-11-22 18:30:13 +00:00
|
|
|
$timeout = (string) ceil(Arsse::$conf->dbTimeoutConnect ?? 0);
|
|
|
|
$postfix = "application_name='arsse' client_encoding='UTF8' connect_timeout='$timeout'";
|
2018-11-17 02:20:54 +00:00
|
|
|
$act = Driver::makeConnectionString($pdo, $user, $pass, $db, $host, $port, $service);
|
|
|
|
if ($act==$postfix) {
|
|
|
|
$this->assertSame($exp, "");
|
|
|
|
} else {
|
2018-12-05 22:28:11 +00:00
|
|
|
$test = substr($act, 0, strlen($act) - (strlen($postfix) + 1));
|
2018-11-17 02:20:54 +00:00
|
|
|
$check = substr($act, strlen($test) + 1);
|
|
|
|
$this->assertSame($postfix, $check);
|
|
|
|
$this->assertSame($exp, $test);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideConnectionStrings() {
|
|
|
|
return [
|
|
|
|
[false, "arsse", "secret", "arsse", "", 5432, "", "dbname='arsse' password='secret' user='arsse'"],
|
|
|
|
[false, "arsse", "p word", "arsse", "", 5432, "", "dbname='arsse' password='p word' user='arsse'"],
|
|
|
|
[false, "arsse", "p'word", "arsse", "", 5432, "", "dbname='arsse' password='p\\'word' user='arsse'"],
|
|
|
|
[false, "arsse user", "secret", "arsse db", "", 5432, "", "dbname='arsse db' password='secret' user='arsse user'"],
|
|
|
|
[false, "arsse", "secret", "", "", 5432, "", "password='secret' user='arsse'"],
|
|
|
|
[false, "arsse", "secret", "arsse", "localhost", 5432, "", "dbname='arsse' host='localhost' password='secret' user='arsse'"],
|
|
|
|
[false, "arsse", "secret", "arsse", "", 9999, "", "dbname='arsse' password='secret' port='9999' user='arsse'"],
|
|
|
|
[false, "arsse", "secret", "arsse", "localhost", 9999, "", "dbname='arsse' host='localhost' password='secret' port='9999' user='arsse'"],
|
|
|
|
[false, "arsse", "secret", "arsse", "/socket", 9999, "", "dbname='arsse' host='/socket' password='secret' user='arsse'"],
|
|
|
|
[false, "T'Pau of Vulcan", "", "", "", 5432, "", "user='T\\'Pau of Vulcan'"],
|
|
|
|
[false, "T'Pau of Vulcan", "superman", "datumbase", "somehost", 2112, "arsse", "service='arsse'"],
|
|
|
|
[true, "arsse", "secret", "arsse", "", 5432, "", "dbname='arsse'"],
|
|
|
|
[true, "arsse", "p word", "arsse", "", 5432, "", "dbname='arsse'"],
|
|
|
|
[true, "arsse", "p'word", "arsse", "", 5432, "", "dbname='arsse'"],
|
|
|
|
[true, "arsse user", "secret", "arsse db", "", 5432, "", "dbname='arsse db'"],
|
|
|
|
[true, "arsse", "secret", "", "", 5432, "", ""],
|
|
|
|
[true, "arsse", "secret", "arsse", "localhost", 5432, "", "dbname='arsse' host='localhost'"],
|
|
|
|
[true, "arsse", "secret", "arsse", "", 9999, "", "dbname='arsse' port='9999'"],
|
|
|
|
[true, "arsse", "secret", "arsse", "localhost", 9999, "", "dbname='arsse' host='localhost' port='9999'"],
|
|
|
|
[true, "arsse", "secret", "arsse", "/socket", 9999, "", "dbname='arsse' host='/socket'"],
|
|
|
|
[true, "T'Pau of Vulcan", "", "", "", 5432, "", ""],
|
|
|
|
[true, "T'Pau of Vulcan", "superman", "datumbase", "somehost", 2112, "arsse", "service='arsse'"],
|
|
|
|
];
|
|
|
|
}
|
2018-12-12 16:15:07 +00:00
|
|
|
|
|
|
|
public function testFailToConnect() {
|
|
|
|
// PDO dies not distinguish between different connection failure modes
|
|
|
|
self::setConf([
|
|
|
|
'dbPostgreSQLPass' => (string) rand(),
|
|
|
|
]);
|
|
|
|
$this->assertException("connectionFailure", "Db");
|
|
|
|
new Driver;
|
|
|
|
}
|
2018-11-17 02:20:54 +00:00
|
|
|
}
|