mirror of
https://github.com/danog/sql.git
synced 2024-11-26 12:04:44 +01:00
Add SqlConfig test
This commit is contained in:
parent
664945b16c
commit
6ac65a30e8
@ -16,7 +16,6 @@
|
||||
},
|
||||
"require-dev": {
|
||||
"amphp/php-cs-fixer-config": "^2",
|
||||
"amphp/phpunit-util": "^3",
|
||||
"phpunit/phpunit": "^9",
|
||||
"psalm/phar": "^5.4"
|
||||
},
|
||||
|
@ -26,9 +26,9 @@ abstract class SqlConfig
|
||||
* Parses a connection string into an array of keys and values given.
|
||||
*
|
||||
* @param string $connectionString Connection string, e.g., "hostname=localhost username=sql password=default"
|
||||
* @param string[] $keymap Map of alternative key names to canonical key names.
|
||||
* @param array<non-empty-string, non-empty-string> $keymap Map of alternative key names to canonical key names.
|
||||
*
|
||||
* @return string[]
|
||||
* @return array<non-empty-string, string>
|
||||
*/
|
||||
protected static function parseConnectionString(string $connectionString, array $keymap = self::KEY_MAP): array
|
||||
{
|
||||
@ -42,18 +42,17 @@ abstract class SqlConfig
|
||||
|
||||
foreach ($params as $param) {
|
||||
/** @psalm-suppress PossiblyInvalidArgument */
|
||||
[$key, $value] = \array_map("trim", \explode("=", $param, 2) + [1 => ""]);
|
||||
|
||||
if (isset($keymap[$key])) {
|
||||
$key = $keymap[$key];
|
||||
[$key, $value] = \array_map(\trim(...), \explode("=", $param, 2) + [1 => ""]);
|
||||
if ($key === '') {
|
||||
throw new \ValueError("Empty key name in connection string");
|
||||
}
|
||||
|
||||
$values[$key] = $value;
|
||||
$values[$keymap[$key] ?? $key] = $value;
|
||||
}
|
||||
|
||||
if (\preg_match('/^(.+):(\d{1,5})$/', $values["host"] ?? "", $matches)) {
|
||||
$values["host"] = $matches[1];
|
||||
$values["port"] = $matches[2];
|
||||
if (\preg_match('/^(?<host>.+):(?<port>\d{1,5})$/', $values["host"] ?? "", $matches)) {
|
||||
$values["host"] = $matches["host"];
|
||||
$values["port"] = $matches["port"];
|
||||
}
|
||||
|
||||
return $values;
|
||||
|
@ -2,10 +2,10 @@
|
||||
|
||||
namespace Amp\Sql\Test;
|
||||
|
||||
use Amp\PHPUnit\AsyncTestCase;
|
||||
use Amp\Sql\QueryError;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class QueryErrorTest extends AsyncTestCase
|
||||
class QueryErrorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
@ -14,7 +14,7 @@ class QueryErrorTest extends AsyncTestCase
|
||||
{
|
||||
$error = new QueryError('error', 'SELECT * FROM foo');
|
||||
|
||||
$this->assertSame('SELECT * FROM foo', $error->getQuery());
|
||||
$this->assertStringStartsWith("Amp\Sql\QueryError: error\nCurrent query was SELECT * FROM foo", (string) $error);
|
||||
self::assertSame('SELECT * FROM foo', $error->getQuery());
|
||||
self::assertStringStartsWith("Amp\Sql\QueryError: error\nCurrent query was SELECT * FROM foo", (string) $error);
|
||||
}
|
||||
}
|
||||
|
67
test/SqlConfigTest.php
Normal file
67
test/SqlConfigTest.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Amp\Sql\Test;
|
||||
|
||||
use Amp\Sql\SqlConfig;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SqlConfigTest extends TestCase
|
||||
{
|
||||
private function createConfigFromString(string $connectionString): SqlConfig
|
||||
{
|
||||
return new class($connectionString) extends SqlConfig {
|
||||
public function __construct(string $connectionString)
|
||||
{
|
||||
$parts = self::parseConnectionString($connectionString);
|
||||
|
||||
parent::__construct(
|
||||
host: $parts["host"] ?? '',
|
||||
port: (int) ($parts["port"] ?? 0),
|
||||
user: $parts["user"] ?? "",
|
||||
password: $parts["password"] ?? "",
|
||||
database: $parts["db"] ?? "",
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public function testPortInHost(): void
|
||||
{
|
||||
$config = $this->createConfigFromString("host=localhost:5432 user=user database=test");
|
||||
|
||||
self::assertSame("localhost", $config->getHost());
|
||||
self::assertSame(5432, $config->getPort());
|
||||
self::assertSame("user", $config->getUser());
|
||||
self::assertSame("", $config->getPassword());
|
||||
self::assertSame("test", $config->getDatabase());
|
||||
}
|
||||
|
||||
public function testBasicSyntax(): void
|
||||
{
|
||||
$config = $this->createConfigFromString("host=localhost port=5432 user=user pass=test db=test");
|
||||
|
||||
self::assertSame("localhost", $config->getHost());
|
||||
self::assertSame(5432, $config->getPort());
|
||||
self::assertSame("user", $config->getUser());
|
||||
self::assertSame("test", $config->getPassword());
|
||||
self::assertSame("test", $config->getDatabase());
|
||||
}
|
||||
|
||||
public function testAlternativeSyntax(): void
|
||||
{
|
||||
$config = $this->createConfigFromString("host=localhost;port=3306;user=user;password=test;db=test");
|
||||
|
||||
self::assertSame("localhost", $config->getHost());
|
||||
self::assertSame(3306, $config->getPort());
|
||||
self::assertSame("user", $config->getUser());
|
||||
self::assertSame("test", $config->getPassword());
|
||||
self::assertSame("test", $config->getDatabase());
|
||||
}
|
||||
|
||||
public function testInvalidString(): void
|
||||
{
|
||||
$this->expectException(\ValueError::class);
|
||||
$this->expectExceptionMessage("Empty key name in connection string");
|
||||
$this->createConfigFromString("invalid =connection string");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user