Somehow... a complete (functional) suite of tests for Endpoints
This commit is contained in:
parent
a429befc94
commit
9cdd917297
@ -109,19 +109,20 @@ class Endpoint
|
|||||||
|
|
||||||
private function parseAuthor(Crawler $document): Author
|
private function parseAuthor(Crawler $document): Author
|
||||||
{
|
{
|
||||||
|
if (!$document->count()) {
|
||||||
|
return new Author();
|
||||||
|
}
|
||||||
|
|
||||||
$card = $document->filter('.h-card');
|
$card = $document->filter('.h-card');
|
||||||
|
|
||||||
if (!$card->count()) {
|
if (!$card->count()) {
|
||||||
$card = $document->closest('.h-card');
|
return $this->parseAuthor($document->ancestors());
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($card && $card->count()) {
|
$name = $card->filter('.p-name')?->text($card->text(""));
|
||||||
$name = $card->filter('.p-name')?->text("");
|
$url = $card->filter('.u-url')->count() ? $card->filter('.u-url')->attr('href') : ($card->attr('href') ?? "");
|
||||||
$url = $card->filter('.u-url')->count() ? $card->filter('.u-url')->attr('href') : "";
|
$photo = $card->filter('.u-photo')->count() ? $card->filter('.u-photo')->attr('src') : "";
|
||||||
$photo = $card->filter('.u-photo')->count() ? $card->filter('.u-photo')->attr('src') : "";
|
|
||||||
|
|
||||||
return new Author(null, $name, $url, $photo);
|
return new Author(null, $name, $url, $photo);
|
||||||
}
|
|
||||||
return new Author();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -459,4 +459,150 @@ class EndpointTest extends TestCase
|
|||||||
$endpoint = new Endpoint($mockClient, $mockGateway);
|
$endpoint = new Endpoint($mockClient, $mockGateway);
|
||||||
$endpoint->receiveWebmention($source, $target);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user