Work on parsing authors and content from mentions
This commit is contained in:
1 parent
a5e0902366
commit
a429befc94
6 files changed
+282
-77
No files matched your search
+189
-8
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user