From cdff6cc2ec04d0fd5cb7ba75f913ca49ccf51286 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Tue, 14 Jun 2022 17:10:09 +0200 Subject: [PATCH 1/4] feat: add DSN --- .phpunit.result.cache | 1 + src/Appwrite/DSN/DSN.php | 134 +++++++++++++++++++++++++++++++++++++ tests/unit/DSN/DSNTest.php | 90 +++++++++++++++++++++++++ 3 files changed, 225 insertions(+) create mode 100644 .phpunit.result.cache create mode 100644 src/Appwrite/DSN/DSN.php create mode 100644 tests/unit/DSN/DSNTest.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000000..c4f25de1db --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":[],"times":{"Appwrite\\Tests\\DSNTest::testSuccess":0.061,"Appwrite\\Tests\\DSNTest::testFail":0.009}} \ No newline at end of file diff --git a/src/Appwrite/DSN/DSN.php b/src/Appwrite/DSN/DSN.php new file mode 100644 index 0000000000..da6cb1ae7d --- /dev/null +++ b/src/Appwrite/DSN/DSN.php @@ -0,0 +1,134 @@ +scheme = $parts['scheme'] ?? null; + $this->user = $parts['user'] ?? null; + $this->password = $parts['pass'] ?? null; + $this->host = $parts['host'] ?? null; + $this->port = $parts['port'] ?? null; + $this->database = $parts['path'] ?? null; + $this->query = $parts['query'] ?? null; + } + + /** + * Return the scheme. + * + * @return string + */ + public function getScheme(): string + { + return $this->scheme; + } + + /** + * Return the user. + * + * @return ?string + */ + public function getUser(): ?string + { + return $this->user; + } + + /** + * Return the password. + * + * @return ?string + */ + public function getPassword(): ?string + { + return $this->password; + } + + /** + * Return the host + * + * @return string + */ + public function getHost(): string + { + return $this->host; + } + + /** + * Return the port + * + * @return ?string + */ + public function getPort(): ?string + { + return $this->port; + } + + /** + * Return the database + * + * @return ?string + */ + public function getDatabase(): ?string + { + return ltrim($this->database, '/'); + } + + /** + * Return the query string + * + * @return ?string + */ + public function getQuery(): ?string + { + return $this->query; + } +} \ No newline at end of file diff --git a/tests/unit/DSN/DSNTest.php b/tests/unit/DSN/DSNTest.php new file mode 100644 index 0000000000..cadb4ab559 --- /dev/null +++ b/tests/unit/DSN/DSNTest.php @@ -0,0 +1,90 @@ +assertEquals("mariadb", $dsn->getScheme()); + $this->assertEquals("user", $dsn->getUser()); + $this->assertEquals("password", $dsn->getPassword()); + $this->assertEquals("localhost", $dsn->getHost()); + $this->assertEquals("3306", $dsn->getPort()); + $this->assertEquals("database", $dsn->getDatabase()); + $this->assertEquals("charset=utf8&timezone=UTC", $dsn->getQuery()); + + $dsn = new DSN("mariadb://user@localhost:3306/database?charset=utf8&timezone=UTC"); + $this->assertEquals("mariadb", $dsn->getScheme()); + $this->assertEquals("user", $dsn->getUser()); + $this->assertNull($dsn->getPassword()); + $this->assertEquals("localhost", $dsn->getHost()); + $this->assertEquals("3306", $dsn->getPort()); + $this->assertEquals("database", $dsn->getDatabase()); + $this->assertEquals("charset=utf8&timezone=UTC", $dsn->getQuery()); + + $dsn = new DSN("mariadb://user@localhost/database?charset=utf8&timezone=UTC"); + $this->assertEquals("mariadb", $dsn->getScheme()); + $this->assertEquals("user", $dsn->getUser()); + $this->assertNull($dsn->getPassword()); + $this->assertEquals("localhost", $dsn->getHost()); + $this->assertNull($dsn->getPort()); + $this->assertEquals("database", $dsn->getDatabase()); + $this->assertEquals("charset=utf8&timezone=UTC", $dsn->getQuery()); + + $dsn = new DSN("mariadb://user@localhost?charset=utf8&timezone=UTC"); + $this->assertEquals("mariadb", $dsn->getScheme()); + $this->assertEquals("user", $dsn->getUser()); + $this->assertNull($dsn->getPassword()); + $this->assertEquals("localhost", $dsn->getHost()); + $this->assertNull($dsn->getPort()); + $this->assertEmpty($dsn->getDatabase()); + $this->assertEquals("charset=utf8&timezone=UTC", $dsn->getQuery()); + + $dsn = new DSN("mariadb://user@localhost"); + $this->assertEquals("mariadb", $dsn->getScheme()); + $this->assertEquals("user", $dsn->getUser()); + $this->assertNull($dsn->getPassword()); + $this->assertEquals("localhost", $dsn->getHost()); + $this->assertNull($dsn->getPort()); + $this->assertEmpty($dsn->getDatabase()); + $this->assertNull($dsn->getQuery()); + + $dsn = new DSN("mariadb://user:@localhost"); + $this->assertEquals("mariadb", $dsn->getScheme()); + $this->assertEquals("user", $dsn->getUser()); + $this->assertEmpty($dsn->getPassword()); + $this->assertEquals("localhost", $dsn->getHost()); + $this->assertNull($dsn->getPort()); + $this->assertEmpty($dsn->getDatabase()); + $this->assertNull($dsn->getQuery()); + + $dsn = new DSN("mariadb://localhost"); + $this->assertEquals("mariadb", $dsn->getScheme()); + $this->assertNull($dsn->getUser()); + $this->assertNull($dsn->getPassword()); + $this->assertEquals("localhost", $dsn->getHost()); + $this->assertNull($dsn->getPort()); + $this->assertEmpty($dsn->getDatabase()); + $this->assertNull($dsn->getQuery()); + } + + public function testFail(): void + { + $this->expectException(\InvalidArgumentException::class); + $dsn = new DSN("mariadb://"); + } +} \ No newline at end of file From 13d428c30b5971660e4e594450891e6dbf0b102a Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Tue, 14 Jun 2022 17:14:43 +0200 Subject: [PATCH 2/4] feat: add DSN --- .phpunit.result.cache | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache deleted file mode 100644 index c4f25de1db..0000000000 --- a/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -{"version":1,"defects":[],"times":{"Appwrite\\Tests\\DSNTest::testSuccess":0.061,"Appwrite\\Tests\\DSNTest::testFail":0.009}} \ No newline at end of file From 0556990b66c1d89ede266902ea3f8e6bbe56e2d1 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Tue, 14 Jun 2022 17:18:15 +0200 Subject: [PATCH 3/4] fix: linter issues --- src/Appwrite/DSN/DSN.php | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Appwrite/DSN/DSN.php b/src/Appwrite/DSN/DSN.php index da6cb1ae7d..a4cc68eb3a 100644 --- a/src/Appwrite/DSN/DSN.php +++ b/src/Appwrite/DSN/DSN.php @@ -2,7 +2,8 @@ namespace Appwrite\DSN; -class DSN { +class DSN +{ /** * @var string */ @@ -21,7 +22,7 @@ class DSN { /** * @var string */ - protected string $host; + protected string $host; /** * @var ?string @@ -42,7 +43,7 @@ class DSN { * Construct * * Construct a new DSN object - * + * * @param string $dsn */ public function __construct(string $dsn) @@ -64,17 +65,17 @@ class DSN { /** * Return the scheme. - * + * * @return string */ - public function getScheme(): string + public function getScheme(): string { return $this->scheme; } /** * Return the user. - * + * * @return ?string */ public function getUser(): ?string @@ -84,7 +85,7 @@ class DSN { /** * Return the password. - * + * * @return ?string */ public function getPassword(): ?string @@ -94,17 +95,17 @@ class DSN { /** * Return the host - * + * * @return string */ - public function getHost(): string + public function getHost(): string { return $this->host; } /** * Return the port - * + * * @return ?string */ public function getPort(): ?string @@ -114,21 +115,21 @@ class DSN { /** * Return the database - * + * * @return ?string */ - public function getDatabase(): ?string + public function getDatabase(): ?string { return ltrim($this->database, '/'); } /** * Return the query string - * + * * @return ?string */ - public function getQuery(): ?string + public function getQuery(): ?string { return $this->query; } -} \ No newline at end of file +} \ No newline at end of file From b56fb8f6f46d395a98d2e4f0e9b09ec02973bc42 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Tue, 14 Jun 2022 20:00:02 +0200 Subject: [PATCH 4/4] fix: linter issues --- src/Appwrite/DSN/DSN.php | 8 ++++---- tests/unit/DSN/DSNTest.php | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/DSN/DSN.php b/src/Appwrite/DSN/DSN.php index a4cc68eb3a..5605640989 100644 --- a/src/Appwrite/DSN/DSN.php +++ b/src/Appwrite/DSN/DSN.php @@ -2,7 +2,7 @@ namespace Appwrite\DSN; -class DSN +class DSN { /** * @var string @@ -78,7 +78,7 @@ class DSN * * @return ?string */ - public function getUser(): ?string + public function getUser(): ?string { return $this->user; } @@ -88,7 +88,7 @@ class DSN * * @return ?string */ - public function getPassword(): ?string + public function getPassword(): ?string { return $this->password; } @@ -132,4 +132,4 @@ class DSN { return $this->query; } -} \ No newline at end of file +} diff --git a/tests/unit/DSN/DSNTest.php b/tests/unit/DSN/DSNTest.php index cadb4ab559..be31a640b8 100644 --- a/tests/unit/DSN/DSNTest.php +++ b/tests/unit/DSN/DSNTest.php @@ -7,7 +7,6 @@ use PHPUnit\Framework\TestCase; class DSNTest extends TestCase { - public function setUp(): void { } @@ -87,4 +86,4 @@ class DSNTest extends TestCase $this->expectException(\InvalidArgumentException::class); $dsn = new DSN("mariadb://"); } -} \ No newline at end of file +}