diff --git a/composer.json b/composer.json index 2c0c81e..d9a826c 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,6 @@ }, "require-dev": { "amphp/php-cs-fixer-config": "^2", - "amphp/phpunit-util": "^3", "phpunit/phpunit": "^9", "psalm/phar": "^5.4" }, diff --git a/src/SqlConfig.php b/src/SqlConfig.php index 0312d10..d6b77ba 100644 --- a/src/SqlConfig.php +++ b/src/SqlConfig.php @@ -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 $keymap Map of alternative key names to canonical key names. * - * @return string[] + * @return array */ 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('/^(?.+):(?\d{1,5})$/', $values["host"] ?? "", $matches)) { + $values["host"] = $matches["host"]; + $values["port"] = $matches["port"]; } return $values; diff --git a/test/QueryErrorTest.php b/test/QueryErrorTest.php index 8721310..bcd61cf 100644 --- a/test/QueryErrorTest.php +++ b/test/QueryErrorTest.php @@ -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); } } diff --git a/test/SqlConfigTest.php b/test/SqlConfigTest.php new file mode 100644 index 0000000..5e80860 --- /dev/null +++ b/test/SqlConfigTest.php @@ -0,0 +1,67 @@ +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"); + } +}