Add SqlConfig test

This commit is contained in:
Aaron Piotrowski 2022-12-31 13:38:42 -06:00
parent 664945b16c
commit 6ac65a30e8
No known key found for this signature in database
GPG Key ID: 5B456E6AABA44A63
4 changed files with 80 additions and 15 deletions

View File

@ -16,7 +16,6 @@
}, },
"require-dev": { "require-dev": {
"amphp/php-cs-fixer-config": "^2", "amphp/php-cs-fixer-config": "^2",
"amphp/phpunit-util": "^3",
"phpunit/phpunit": "^9", "phpunit/phpunit": "^9",
"psalm/phar": "^5.4" "psalm/phar": "^5.4"
}, },

View File

@ -26,9 +26,9 @@ abstract class SqlConfig
* Parses a connection string into an array of keys and values given. * 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 $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 protected static function parseConnectionString(string $connectionString, array $keymap = self::KEY_MAP): array
{ {
@ -42,18 +42,17 @@ abstract class SqlConfig
foreach ($params as $param) { foreach ($params as $param) {
/** @psalm-suppress PossiblyInvalidArgument */ /** @psalm-suppress PossiblyInvalidArgument */
[$key, $value] = \array_map("trim", \explode("=", $param, 2) + [1 => ""]); [$key, $value] = \array_map(\trim(...), \explode("=", $param, 2) + [1 => ""]);
if ($key === '') {
if (isset($keymap[$key])) { throw new \ValueError("Empty key name in connection string");
$key = $keymap[$key];
} }
$values[$key] = $value; $values[$keymap[$key] ?? $key] = $value;
} }
if (\preg_match('/^(.+):(\d{1,5})$/', $values["host"] ?? "", $matches)) { if (\preg_match('/^(?<host>.+):(?<port>\d{1,5})$/', $values["host"] ?? "", $matches)) {
$values["host"] = $matches[1]; $values["host"] = $matches["host"];
$values["port"] = $matches[2]; $values["port"] = $matches["port"];
} }
return $values; return $values;

View File

@ -2,10 +2,10 @@
namespace Amp\Sql\Test; namespace Amp\Sql\Test;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Sql\QueryError; use Amp\Sql\QueryError;
use PHPUnit\Framework\TestCase;
class QueryErrorTest extends AsyncTestCase class QueryErrorTest extends TestCase
{ {
/** /**
* @test * @test
@ -14,7 +14,7 @@ class QueryErrorTest extends AsyncTestCase
{ {
$error = new QueryError('error', 'SELECT * FROM foo'); $error = new QueryError('error', 'SELECT * FROM foo');
$this->assertSame('SELECT * FROM foo', $error->getQuery()); self::assertSame('SELECT * FROM foo', $error->getQuery());
$this->assertStringStartsWith("Amp\Sql\QueryError: error\nCurrent query was SELECT * FROM foo", (string) $error); self::assertStringStartsWith("Amp\Sql\QueryError: error\nCurrent query was SELECT * FROM foo", (string) $error);
} }
} }

67
test/SqlConfigTest.php Normal file
View 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");
}
}