webmentions/tests/Gateways/SqliteGatewayTest.php

153 lines
4.0 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);
use Lewisdale\Webmentions\Gateways\SqliteGateway;
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",
"No content",
"Some Author Name"
);
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",
"No content",
"Some Author Name"
);
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",
"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()
{
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"
));
}
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",
"No content",
"Some Author Name"
));
$this->gateway->save(new Webmention(
null,
"https://lewisdale.dev/post/a-new-post",
"https://a-different-source.url",
"No content",
"Some Author Name"
));
$this->gateway->save(new Webmention(
null,
"https://lewisdale.dev/post/a-new-post",
"https://a-source.url",
"Some content",
"Some Author Name"
));
$this->gateway->find([
"id; DROP TABLE webmentions;" => 'whatever'
]);
$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-09 09:48:08 +00:00
}