From f85c48b4a9d047a5a84580f0e54d6a9005241218 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Thu, 9 Mar 2023 13:05:43 +0000 Subject: [PATCH] Finish implementing SqliteGateway --- src/Gateways/SqliteGateway.php | 36 ++++++++++----- tests/Gateways/SqliteGatewayTest.php | 67 +++++++++++++++++++++++++--- 2 files changed, 87 insertions(+), 16 deletions(-) diff --git a/src/Gateways/SqliteGateway.php b/src/Gateways/SqliteGateway.php index 96a8838..d9b1682 100644 --- a/src/Gateways/SqliteGateway.php +++ b/src/Gateways/SqliteGateway.php @@ -33,14 +33,11 @@ class SqliteGateway extends WebmentionGatewayInterface { public function get(int $id): ?Webmention { - $sql = "SELECT * FROM webmentions;"; - $statement = $this->connection->query($sql); - - if ($statement == false) { - return null; - } - + $sql = "SELECT * FROM webmentions WHERE id=:id LIMIT 1;"; + $statement = $this->connection->prepare($sql); + $statement->execute(["id" => $id]); $row = $statement->fetch(PDO::FETCH_ASSOC); + $statement->closeCursor(); if ($row) { return new Webmention( @@ -57,7 +54,24 @@ class SqliteGateway extends WebmentionGatewayInterface { public function getByPost(string $post): array { - return []; + $sql = "SELECT * FROM webmentions WHERE target=:post"; + $statement = $this->connection->prepare($sql); + $statement->execute(["post" => $post]); + + $mentions = []; + + while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { + $mentions[] = new Webmention( + $row["id"], + $row["target"], + $row["source"], + $row["content"], + $row["author"] + ); + } + $statement->closeCursor(); + + return $mentions; } public function save(Webmention $webmention): ?int @@ -78,8 +92,10 @@ class SqliteGateway extends WebmentionGatewayInterface { public function delete(Webmention $webmention): void { - throw new Exception("Method " . SqliteGateway::class . "::delete not implemented"); - + $sql = "DELETE FROM webmentions WHERE id=:id;"; + $statement = $this->connection->prepare($sql); + $statement->execute(["id" => $webmention->id]); + $statement->closeCursor(); } } ?> \ No newline at end of file diff --git a/tests/Gateways/SqliteGatewayTest.php b/tests/Gateways/SqliteGatewayTest.php index 181685f..3bc416f 100644 --- a/tests/Gateways/SqliteGatewayTest.php +++ b/tests/Gateways/SqliteGatewayTest.php @@ -10,9 +10,15 @@ use PHPUnit\Framework\TestCase; class SqliteGatewayTest extends TestCase { + private SqliteGateway $gateway; + + protected function setUp(): void + { + $this->gateway = new SqliteGateway(":memory:"); + } + public function testCanInsertAWebmention() { - $gateway = new SqliteGateway(":memory:"); $webmention = new Webmention( null, @@ -22,13 +28,13 @@ class SqliteGatewayTest extends TestCase "Some Author Name" ); - $webmention->id = $gateway->save($webmention); + $webmention->id = $this->gateway->save($webmention); $this->assertNotNull($webmention->id); } public function testCanRetrieveAWebmention() { - $gateway = new SqliteGateway(":memory:"); + $this->gateway = new SqliteGateway(":memory:"); $webmention = new Webmention( null, @@ -38,10 +44,59 @@ class SqliteGatewayTest extends TestCase "Some Author Name" ); - $webmention->id = $gateway->save($webmention); + $webmention->id = $this->gateway->save($webmention); - $retrieved = $gateway->get($webmention->id); + $retrieved = $this->gateway->get($webmention->id); - $this->assertEquals($webmention, $retrieved, ((string) $webmention). " " . ((string) $retrieved)); + $this->assertEquals($webmention, $retrieved); + } + + public function testCanDeleteAWebmention() + { + $this->gateway = new SqliteGateway(":memory:"); + + $webmention = new Webmention( + null, + "https://lewisdale.dev/post/a-post", + "https://a-source.url", + "No content", + "Some Author Name" + ); + + $webmention->id = $this->gateway->save($webmention); + $this->gateway->delete($webmention); + + $retrieved = $this->gateway->get($webmention->id); + + $this->assertNull($retrieved); + } + + public function testCanGetByPost() + { + $this->gateway = new SqliteGateway(":memory:"); + + foreach(range(0, 4) as $_) { + $this->gateway->save(new Webmention( + null, + "https://lewisdale.dev/post/a-new-post", + "https://a-source.url", + "No content", + "Some Author Name" + )); + } + + foreach(range(0, 4) as $_) { + $this->gateway->save(new Webmention( + null, + "https://lewisdale.dev/post/a-different-post", + "https://a-source.url", + "No content", + "Some Author Name" + )); + } + + $mentions = $this->gateway->getByPost("https://lewisdale.dev/post/a-new-post"); + + $this->assertCount(5, $mentions); } }