Somehow... a complete (functional) suite of tests for Endpoints

This commit is contained in:
Lewis Dale 2023-03-15 13:16:30 +00:00
parent a429befc94
commit 9cdd917297
2 changed files with 155 additions and 8 deletions

View File

@ -109,19 +109,20 @@ class Endpoint
private function parseAuthor(Crawler $document): Author
{
if (!$document->count()) {
return new Author();
}
$card = $document->filter('.h-card');
if (!$card->count()) {
$card = $document->closest('.h-card');
return $this->parseAuthor($document->ancestors());
}
if ($card && $card->count()) {
$name = $card->filter('.p-name')?->text("");
$url = $card->filter('.u-url')->count() ? $card->filter('.u-url')->attr('href') : "";
$photo = $card->filter('.u-photo')->count() ? $card->filter('.u-photo')->attr('src') : "";
$name = $card->filter('.p-name')?->text($card->text(""));
$url = $card->filter('.u-url')->count() ? $card->filter('.u-url')->attr('href') : ($card->attr('href') ?? "");
$photo = $card->filter('.u-photo')->count() ? $card->filter('.u-photo')->attr('src') : "";
return new Author(null, $name, $url, $photo);
}
return new Author();
return new Author(null, $name, $url, $photo);
}
}

View File

@ -459,4 +459,150 @@ class EndpointTest extends TestCase
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
}
public function testItShouldParseAnAuthorCardWithAMinimalHCard()
{
$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>
<span class="h-card">
<a class="p-name p-org u-url" href="https://microformats.org/">microformats.org</a>
</span>
</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,
"microformats.org",
"https://microformats.org/",
""
);
$mockGateway->expects($this->once())
->method('save')
->with($this->objectContains('author', $expected));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
}
public function testItShouldParseAnAuthorCardWithAVeryMinimalHCard()
{
$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>
<a class="h-card" href="https://microformats.org/">microformats.org</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);
$expected = new \Lewisdale\Webmentions\Models\Author(
null,
"microformats.org",
"https://microformats.org/",
""
);
$mockGateway->expects($this->once())
->method('save')
->with($this->objectContains('author', $expected));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
}
public function testItShouldParseAnAuthorCardOutsideTheEntrry()
{
$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>
<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>
</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);
}
}