Work on parsing authors and content from mentions

This commit is contained in:
Lewis Dale committed 2023-03-15 09:15:55 +00:00
1 parent a5e0902366
commit a429befc94
6 files changed
+282 -77

No files matched your search

+189 -8
View File
@@ -7,15 +7,25 @@ use Lewisdale\Webmentions\Exceptions\SourceNotFoundException;
use Lewisdale\Webmentions\Exceptions\TargetNotMentionedException;
use Lewisdale\Webmentions\Gateways\WebmentionGatewayInterface;
use Lewisdale\Webmentions\Models\MentionType;
use Lewisdale\Webmentions\Models\Webmention;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class EndpointTest extends TestCase {
private function objectContains(string $key, mixed $value) {
return $this->callback(fn(object $obj) => $obj->$key === $value);
class EndpointTest extends TestCase
{
private function objectContains(string $key, mixed $expected)
{
return $this->callback(function (object $obj) use ($expected, $key) {
$val = $obj->$key;
$type = gettype($val);
return match ($type) {
"object" => $val == $expected,
"array" => count(array_diff($val, $expected)) === 0,
default => $val === $expected
};
});
}
#[TestWith(["https://my.url.com", true])]
@@ -148,7 +158,7 @@ class EndpointTest extends TestCase {
->with($this->objectContains('type', MentionType::Like));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
}
@@ -277,5 +287,176 @@ class EndpointTest extends TestCase {
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
}
}
?>
public function testItShouldParseARepostsContent()
{
$source = "https://my-valid-source-url.com";
$target = "https://lewisdale.dev/post/a-post-page";
$content = <<<XML
<html>
<head>
</head>
<body>
<article="h-entry">
<h1>Some content</h1>
<p>Here's some body content. It <a href="/another/page">contains a url</a>.</p>
<p>I'm writing about <a href="$target" class="u-repost-of">this post</a>.</p>
</article>
</body>
</html>
XML;
$mockClient = $this->createMock(HttpClientInterface::class);
$mockResponse = $this->createMock(ResponseInterface::class);
$mockGateway = $this->createMock(WebmentionGatewayInterface::class);
$mockClient->expects($this->once())
->method('request')
->with($this->identicalTo('GET'), $this->identicalTo($source))
->will($this->returnValue($mockResponse));
$mockResponse->method('getStatusCode')
->will($this->returnValue(200));
$mockResponse->method('getContent')
->willReturn($content);
$mockGateway->expects($this->once())
->method('save')
->with($this->objectContains('content', "Reposted this post"));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
}
public function testItShouldParseALikeContent()
{
$source = "https://my-valid-source-url.com";
$target = "https://lewisdale.dev/post/a-post-page";
$content = <<<HTML
<html>
<head>
</head>
<body>
<span class="h-entry">
<a class="u-like-of" href="$target">A Cool Post</a><a class="u-url" href="/"></a>
</span>
</body>
</html>
HTML;
$mockClient = $this->createMock(HttpClientInterface::class);
$mockResponse = $this->createMock(ResponseInterface::class);
$mockGateway = $this->createMock(WebmentionGatewayInterface::class);
$mockClient->expects($this->once())
->method('request')
->with($this->identicalTo('GET'), $this->identicalTo($source))
->will($this->returnValue($mockResponse));
$mockResponse->method('getStatusCode')
->will($this->returnValue(200));
$mockResponse->method('getContent')
->willReturn($content);
$mockGateway->expects($this->once())
->method('save')
->with($this->objectContains('content', "Liked this post"));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
}
public function testItShouldParseAReplyContent()
{
$source = "https://my-valid-source-url.com";
$target = "https://lewisdale.dev/post/a-post-page";
$content = <<<HTML
<html>
<head>
</head>
<body>
<article class="h-entry">
<a class="u-in-reply-to" rel="in-reply-to" href="$target">@post</a>: That's a great idea!<a class="u-url" href="/"></a>
</article>
</body>
</html>
HTML;
$mockClient = $this->createMock(HttpClientInterface::class);
$mockResponse = $this->createMock(ResponseInterface::class);
$mockGateway = $this->createMock(WebmentionGatewayInterface::class);
$mockClient->expects($this->once())
->method('request')
->with($this->identicalTo('GET'), $this->identicalTo($source))
->will($this->returnValue($mockResponse));
$mockResponse->method('getStatusCode')
->will($this->returnValue(200));
$mockResponse->method('getContent')
->willReturn($content);
$mockGateway->expects($this->once())
->method('save')
->with($this->objectContains('content', "@post: That's a great idea!"));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
}
public function testItShouldParseAnAuthorCardWithANameUrlAndPhoto()
{
$source = "https://my-valid-source-url.com";
$target = "https://lewisdale.dev/post/a-post-page";
$content = <<<HTML
<html>
<head>
</head>
<body>
<article class="h-entry">
<a class="u-in-reply-to" rel="in-reply-to" href="$target">@post</a>: That's a great idea!<a class="u-url" href="/"></a>
<div class="h-card">
<p class="p-name">Anne Author</p> who can be found at <a class="u-url" href="https://my-blog.com">my-blog.com</a>.
<img src="https://dummyimage.com/100x100/fff/aaa" class="u-photo" alt="My profile picture" />
</div>
</article>
</body>
</html>
HTML;
$mockClient = $this->createMock(HttpClientInterface::class);
$mockResponse = $this->createMock(ResponseInterface::class);
$mockGateway = $this->createMock(WebmentionGatewayInterface::class);
$mockClient->expects($this->once())
->method('request')
->with($this->identicalTo('GET'), $this->identicalTo($source))
->will($this->returnValue($mockResponse));
$mockResponse->method('getStatusCode')
->will($this->returnValue(200));
$mockResponse->method('getContent')
->willReturn($content);
$expected = new \Lewisdale\Webmentions\Models\Author(
null,
"Anne Author",
"https://my-blog.com",
"https://dummyimage.com/100x100/fff/aaa"
);
$mockGateway->expects($this->once())
->method('save')
->with($this->objectContains('author', $expected));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
}
}
+15 -14
View File
@@ -1,7 +1,8 @@
<?php
declare(strict_types=1);
error_reporting(E_ALL); ini_set('display_errors',1);
error_reporting(E_ALL);
ini_set('display_errors', 1);
use Lewisdale\Webmentions\Gateways\SqliteGateway;
@@ -27,7 +28,7 @@ class SqliteGatewayTest extends TestCase
"https://a-source.url",
MentionType::Like,
"No content",
"Some Author Name"
new \Lewisdale\Webmentions\Models\Author(),
);
$webmention->id = $this->gateway->save($webmention);
@@ -42,7 +43,7 @@ class SqliteGatewayTest extends TestCase
"https://a-source.url",
MentionType::Like,
"No content",
"Some Author Name"
new \Lewisdale\Webmentions\Models\Author(),
);
$webmention->id = $this->gateway->save($webmention);
@@ -60,7 +61,7 @@ class SqliteGatewayTest extends TestCase
"https://a-source.url",
MentionType::Like,
"No content",
"Some Author Name"
new \Lewisdale\Webmentions\Models\Author()
);
$webmention->id = $this->gateway->save($webmention);
@@ -73,25 +74,25 @@ class SqliteGatewayTest extends TestCase
public function testCanGetByPost()
{
foreach(range(0, 4) as $_) {
foreach (range(0, 4) as $_) {
$this->gateway->save(new Webmention(
null,
"https://lewisdale.dev/post/a-new-post",
"https://a-source.url",
MentionType::Reply,
"No content",
"Some Author Name"
new \Lewisdale\Webmentions\Models\Author()
));
}
foreach(range(0, 4) as $_) {
foreach (range(0, 4) as $_) {
$this->gateway->save(new Webmention(
null,
"https://lewisdale.dev/post/a-different-post",
"https://a-source.url",
MentionType::Like,
"No content",
"Some Author Name"
new \Lewisdale\Webmentions\Models\Author()
));
}
@@ -108,7 +109,7 @@ class SqliteGatewayTest extends TestCase
"https://a-source.url",
MentionType::Reply,
"No content",
"Some Author Name"
new \Lewisdale\Webmentions\Models\Author()
));
$this->gateway->save(new Webmention(
@@ -117,7 +118,7 @@ class SqliteGatewayTest extends TestCase
"https://a-different-source.url",
MentionType::Like,
"No content",
"Some Author Name"
new \Lewisdale\Webmentions\Models\Author()
));
$this->gateway->save(new Webmention(
@@ -126,14 +127,14 @@ class SqliteGatewayTest extends TestCase
"https://a-source.url",
MentionType::Reply,
"Some content",
"Some Author Name"
new \Lewisdale\Webmentions\Models\Author()
));
$this->assertCount(
2,
$this->gateway->find([
"target" => "https://lewisdale.dev/post/a-new-post",
"source" => "https://a-source.url"
"source" => "https://a-source.url",
])
);
@@ -141,7 +142,7 @@ class SqliteGatewayTest extends TestCase
1,
$this->gateway->find([
"target" => "https://lewisdale.dev/post/a-new-post",
"source" => "https://a-different-source.url"
"source" => "https://a-different-source.url",
])
);
@@ -150,7 +151,7 @@ class SqliteGatewayTest extends TestCase
$this->gateway->find([
"target" => "https://lewisdale.dev/post/a-new-post",
"source" => "https://a-source.url",
"content" => "Some content"
"content" => "Some content",
])
);
}