webmentions/tests/Gateways/SqliteGatewayTest.php

306 lines
8.9 KiB
PHP
Raw Normal View History

2023-03-09 09:48:08 +00:00
<?php
declare(strict_types=1);
error_reporting(E_ALL);
ini_set('display_errors', 1);
2023-03-09 09:48:08 +00:00
use Lewisdale\Webmentions\Gateways\SqliteGateway;
2023-03-14 08:43:25 +00:00
use Lewisdale\Webmentions\Models\MentionType;
2023-03-09 09:48:08 +00:00
use Lewisdale\Webmentions\Models\Webmention;
use PHPUnit\Framework\TestCase;
class SqliteGatewayTest extends TestCase
{
2023-03-09 13:05:43 +00:00
private SqliteGateway $gateway;
protected function setUp(): void
{
$this->gateway = new SqliteGateway(":memory:");
}
2023-03-09 09:48:08 +00:00
public function testCanInsertAWebmention()
{
$webmention = new Webmention(
null,
"https://lewisdale.dev/post/a-post",
"https://a-source.url",
2023-03-14 08:43:25 +00:00
MentionType::Like,
2023-03-09 09:48:08 +00:00
"No content",
new \Lewisdale\Webmentions\Models\Author(),
2023-03-09 09:48:08 +00:00
);
2023-03-09 13:05:43 +00:00
$webmention->id = $this->gateway->save($webmention);
2023-03-09 09:48:08 +00:00
$this->assertNotNull($webmention->id);
}
public function testCanRetrieveAWebmention()
{
$webmention = new Webmention(
null,
"https://lewisdale.dev/post/a-post",
"https://a-source.url",
2023-03-14 08:43:25 +00:00
MentionType::Like,
2023-03-09 09:48:08 +00:00
"No content",
2023-03-16 09:34:57 +00:00
new \Lewisdale\Webmentions\Models\Author(null, null, null, null),
2023-03-09 09:48:08 +00:00
);
2023-03-09 13:05:43 +00:00
$webmention->id = $this->gateway->save($webmention);
$retrieved = $this->gateway->get($webmention->id);
$this->assertEquals($webmention, $retrieved);
}
public function testCanDeleteAWebmention()
{
$webmention = new Webmention(
null,
"https://lewisdale.dev/post/a-post",
"https://a-source.url",
2023-03-14 08:43:25 +00:00
MentionType::Like,
2023-03-09 13:05:43 +00:00
"No content",
new \Lewisdale\Webmentions\Models\Author()
2023-03-09 13:05:43 +00:00
);
$webmention->id = $this->gateway->save($webmention);
$this->gateway->delete($webmention);
$retrieved = $this->gateway->get($webmention->id);
$this->assertNull($retrieved);
}
public function testCanGetByPost()
{
foreach (range(0, 4) as $_) {
2023-03-09 13:05:43 +00:00
$this->gateway->save(new Webmention(
null,
"https://lewisdale.dev/post/a-new-post",
"https://a-source.url",
2023-03-14 13:54:05 +00:00
MentionType::Reply,
2023-03-09 13:05:43 +00:00
"No content",
new \Lewisdale\Webmentions\Models\Author()
2023-03-09 13:05:43 +00:00
));
}
foreach (range(0, 4) as $_) {
2023-03-09 13:05:43 +00:00
$this->gateway->save(new Webmention(
null,
"https://lewisdale.dev/post/a-different-post",
"https://a-source.url",
2023-03-14 08:43:25 +00:00
MentionType::Like,
2023-03-09 13:05:43 +00:00
"No content",
new \Lewisdale\Webmentions\Models\Author()
2023-03-09 13:05:43 +00:00
));
}
2023-03-09 09:48:08 +00:00
2023-03-09 13:05:43 +00:00
$mentions = $this->gateway->getByPost("https://lewisdale.dev/post/a-new-post");
2023-03-09 09:48:08 +00:00
2023-03-09 13:05:43 +00:00
$this->assertCount(5, $mentions);
2023-03-09 09:48:08 +00:00
}
public function testCanFindByParams()
{
$this->gateway->save(new Webmention(
null,
"https://lewisdale.dev/post/a-new-post",
"https://a-source.url",
2023-03-14 08:43:25 +00:00
MentionType::Reply,
"No content",
new \Lewisdale\Webmentions\Models\Author()
));
$this->gateway->save(new Webmention(
null,
"https://lewisdale.dev/post/a-new-post",
"https://a-different-source.url",
2023-03-14 08:43:25 +00:00
MentionType::Like,
"No content",
new \Lewisdale\Webmentions\Models\Author()
));
$this->gateway->save(new Webmention(
null,
"https://lewisdale.dev/post/a-new-post",
"https://a-source.url",
2023-03-14 13:54:05 +00:00
MentionType::Reply,
"Some content",
new \Lewisdale\Webmentions\Models\Author()
));
$this->assertCount(
2,
$this->gateway->find([
"target" => "https://lewisdale.dev/post/a-new-post",
"source" => "https://a-source.url",
])
);
$this->assertCount(
1,
$this->gateway->find([
"target" => "https://lewisdale.dev/post/a-new-post",
"source" => "https://a-different-source.url",
])
);
$this->assertCount(
1,
$this->gateway->find([
"target" => "https://lewisdale.dev/post/a-new-post",
"source" => "https://a-source.url",
"content" => "Some content",
])
);
}
2023-03-16 09:34:57 +00:00
public function testItSavesAnAuthorWithTheWebmention()
{
$webmention = new Webmention(
null,
"https://a-target-url.com",
"https://a-source-url.com",
MentionType::Like,
"Liked this post",
new \Lewisdale\Webmentions\Models\Author(
null,
"Doreen Mifah",
"https://my-homepage.com",
"https://cdn.imgserver.com/400/400"
)
);
$webmention->id = $this->gateway->save($webmention);
$retrieved = $this->gateway->get($webmention->id)?->author;
$expected = $webmention->author;
$this->assertEquals($expected->name, $retrieved?->name);
$this->assertEquals($expected->url, $retrieved?->url);
$this->assertEquals($expected->photo, $retrieved?->photo);
}
public function testItLooksUpWebmentionsByAuthorName()
{
$this->gateway->save(new Webmention(
null,
"https://a-target-url.com",
"https://a-source-url.com",
MentionType::Like,
"Liked this post",
new \Lewisdale\Webmentions\Models\Author(
null,
"Doreen Mifah",
"https://my-homepage.com",
"https://cdn.imgserver.com/400/400"
)
));
$this->gateway->save(new Webmention(
null,
"https://a-target-url.com",
"https://a-source-url.com",
MentionType::Like,
"Liked this post",
new \Lewisdale\Webmentions\Models\Author(
null,
"Carl Weathers",
"https://carl-weathers.com",
"https://cdn.imgserver.com/400/400"
)
));
$this->gateway->save(new Webmention(
null,
"https://a-target-url.com",
"https://a-source-url.com",
MentionType::Like,
"Liked this post",
new \Lewisdale\Webmentions\Models\Author(
null,
"Barry White",
"https://barry.white.com",
"https://cdn.imgserver.com/400/400"
)
));
$this->gateway->save(new Webmention(
null,
"https://a-target-url.com",
"https://a-source-url.com",
MentionType::Reply,
"This was a cool post!",
new \Lewisdale\Webmentions\Models\Author(
null,
"Carl Weathers",
"https://carl-weathers.com",
"https://cdn.imgserver.com/400/400"
)
));
$retrieved = $this->gateway->find(["author_name" => "Carl Weathers"]);
$this->assertCount(2, $retrieved);
}
public function testCanListAllWebmentions()
{
$this->gateway->save(new Webmention(
null,
"https://a-target-url.com",
"https://a-source-url.com",
MentionType::Like,
"Liked this post",
new \Lewisdale\Webmentions\Models\Author(
null,
"Doreen Mifah",
"https://my-homepage.com",
"https://cdn.imgserver.com/400/400"
)
));
$this->gateway->save(new Webmention(
null,
"https://a-target-url.com",
"https://a-source-url.com",
MentionType::Like,
"Liked this post",
new \Lewisdale\Webmentions\Models\Author(
null,
"Carl Weathers",
"https://carl-weathers.com",
"https://cdn.imgserver.com/400/400"
)
));
$this->gateway->save(new Webmention(
null,
"https://a-target-url.com",
"https://a-source-url.com",
MentionType::Like,
"Liked this post",
new \Lewisdale\Webmentions\Models\Author(
null,
"Barry White",
"https://barry.white.com",
"https://cdn.imgserver.com/400/400"
)
));
$this->gateway->save(new Webmention(
null,
"https://a-target-url.com",
"https://a-source-url.com",
MentionType::Reply,
"This was a cool post!",
new \Lewisdale\Webmentions\Models\Author(
null,
"Carl Weathers",
"https://carl-weathers.com",
"https://cdn.imgserver.com/400/400"
)
));
$this->assertCount(4, $this->gateway->list());
}
2023-03-09 09:48:08 +00:00
}