Skip to content

Commit 6ac65a3

Browse files
committed
Add SqlConfig test
1 parent 664945b commit 6ac65a3

4 files changed

Lines changed: 80 additions & 15 deletions

File tree

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
},
1717
"require-dev": {
1818
"amphp/php-cs-fixer-config": "^2",
19-
"amphp/phpunit-util": "^3",
2019
"phpunit/phpunit": "^9",
2120
"psalm/phar": "^5.4"
2221
},

src/SqlConfig.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ abstract class SqlConfig
2626
* Parses a connection string into an array of keys and values given.
2727
*
2828
* @param string $connectionString Connection string, e.g., "hostname=localhost username=sql password=default"
29-
* @param string[] $keymap Map of alternative key names to canonical key names.
29+
* @param array<non-empty-string, non-empty-string> $keymap Map of alternative key names to canonical key names.
3030
*
31-
* @return string[]
31+
* @return array<non-empty-string, string>
3232
*/
3333
protected static function parseConnectionString(string $connectionString, array $keymap = self::KEY_MAP): array
3434
{
@@ -42,18 +42,17 @@ protected static function parseConnectionString(string $connectionString, array
4242

4343
foreach ($params as $param) {
4444
/** @psalm-suppress PossiblyInvalidArgument */
45-
[$key, $value] = \array_map("trim", \explode("=", $param, 2) + [1 => ""]);
46-
47-
if (isset($keymap[$key])) {
48-
$key = $keymap[$key];
45+
[$key, $value] = \array_map(\trim(...), \explode("=", $param, 2) + [1 => ""]);
46+
if ($key === '') {
47+
throw new \ValueError("Empty key name in connection string");
4948
}
5049

51-
$values[$key] = $value;
50+
$values[$keymap[$key] ?? $key] = $value;
5251
}
5352

54-
if (\preg_match('/^(.+):(\d{1,5})$/', $values["host"] ?? "", $matches)) {
55-
$values["host"] = $matches[1];
56-
$values["port"] = $matches[2];
53+
if (\preg_match('/^(?<host>.+):(?<port>\d{1,5})$/', $values["host"] ?? "", $matches)) {
54+
$values["host"] = $matches["host"];
55+
$values["port"] = $matches["port"];
5756
}
5857

5958
return $values;

test/QueryErrorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Amp\Sql\Test;
44

5-
use Amp\PHPUnit\AsyncTestCase;
65
use Amp\Sql\QueryError;
6+
use PHPUnit\Framework\TestCase;
77

8-
class QueryErrorTest extends AsyncTestCase
8+
class QueryErrorTest extends TestCase
99
{
1010
/**
1111
* @test
@@ -14,7 +14,7 @@ public function testItPassesQueryAlong()
1414
{
1515
$error = new QueryError('error', 'SELECT * FROM foo');
1616

17-
$this->assertSame('SELECT * FROM foo', $error->getQuery());
18-
$this->assertStringStartsWith("Amp\Sql\QueryError: error\nCurrent query was SELECT * FROM foo", (string) $error);
17+
self::assertSame('SELECT * FROM foo', $error->getQuery());
18+
self::assertStringStartsWith("Amp\Sql\QueryError: error\nCurrent query was SELECT * FROM foo", (string) $error);
1919
}
2020
}

test/SqlConfigTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Amp\Sql\Test;
4+
5+
use Amp\Sql\SqlConfig;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class SqlConfigTest extends TestCase
9+
{
10+
private function createConfigFromString(string $connectionString): SqlConfig
11+
{
12+
return new class($connectionString) extends SqlConfig {
13+
public function __construct(string $connectionString)
14+
{
15+
$parts = self::parseConnectionString($connectionString);
16+
17+
parent::__construct(
18+
host: $parts["host"] ?? '',
19+
port: (int) ($parts["port"] ?? 0),
20+
user: $parts["user"] ?? "",
21+
password: $parts["password"] ?? "",
22+
database: $parts["db"] ?? "",
23+
);
24+
}
25+
};
26+
}
27+
28+
public function testPortInHost(): void
29+
{
30+
$config = $this->createConfigFromString("host=localhost:5432 user=user database=test");
31+
32+
self::assertSame("localhost", $config->getHost());
33+
self::assertSame(5432, $config->getPort());
34+
self::assertSame("user", $config->getUser());
35+
self::assertSame("", $config->getPassword());
36+
self::assertSame("test", $config->getDatabase());
37+
}
38+
39+
public function testBasicSyntax(): void
40+
{
41+
$config = $this->createConfigFromString("host=localhost port=5432 user=user pass=test db=test");
42+
43+
self::assertSame("localhost", $config->getHost());
44+
self::assertSame(5432, $config->getPort());
45+
self::assertSame("user", $config->getUser());
46+
self::assertSame("test", $config->getPassword());
47+
self::assertSame("test", $config->getDatabase());
48+
}
49+
50+
public function testAlternativeSyntax(): void
51+
{
52+
$config = $this->createConfigFromString("host=localhost;port=3306;user=user;password=test;db=test");
53+
54+
self::assertSame("localhost", $config->getHost());
55+
self::assertSame(3306, $config->getPort());
56+
self::assertSame("user", $config->getUser());
57+
self::assertSame("test", $config->getPassword());
58+
self::assertSame("test", $config->getDatabase());
59+
}
60+
61+
public function testInvalidString(): void
62+
{
63+
$this->expectException(\ValueError::class);
64+
$this->expectExceptionMessage("Empty key name in connection string");
65+
$this->createConfigFromString("invalid =connection string");
66+
}
67+
}

0 commit comments

Comments
 (0)