Refactor EndpointTests

This commit is contained in:
Lewis Dale 2023-03-15 13:49:53 +00:00
parent 9cdd917297
commit 3cafc5238e

View File

@ -14,6 +14,17 @@ use Symfony\Contracts\HttpClient\ResponseInterface;
class EndpointTest extends TestCase class EndpointTest extends TestCase
{ {
private HttpClientInterface $mockClient;
private ResponseInterface $mockResponse;
private WebmentionGatewayInterface $mockGateway;
protected function setUp(): void
{
$this->mockClient = $this->createMock(HttpClientInterface::class);
$this->mockResponse = $this->createMock(ResponseInterface::class);
$this->mockGateway = $this->createMock(WebmentionGatewayInterface::class);
}
private function objectContains(string $key, mixed $expected) private function objectContains(string $key, mixed $expected)
{ {
return $this->callback(function (object $obj) use ($expected, $key) { return $this->callback(function (object $obj) use ($expected, $key) {
@ -28,11 +39,21 @@ class EndpointTest extends TestCase
}); });
} }
private function callEndpoint(string $source, string $target)
{
$this->mockClient->method('request')
->with($this->identicalTo('GET'), $this->identicalTo($source))
->will($this->returnValue($this->mockResponse));
$endpoint = new Endpoint($this->mockClient, $this->mockGateway);
$endpoint->receiveWebmention($source, $target);
}
#[TestWith(["https://my.url.com", true])] #[TestWith(["https://my.url.com", true])]
#[TestWith(["my.url.com", false])] #[TestWith(["my.url.com", false])]
public function testValidatesUrls(string $url, bool $expected) public function testValidatesUrls(string $url, bool $expected)
{ {
$endpoint = new Endpoint($this->createMock(HttpClientInterface::class), $this->createMock(WebmentionGatewayInterface::class)); $endpoint = new Endpoint($this->mockClient, $this->mockGateway);
$this->assertEquals($expected, $endpoint->validateUrl($url), "Expected $url"); $this->assertEquals($expected, $endpoint->validateUrl($url), "Expected $url");
} }
@ -42,15 +63,13 @@ class EndpointTest extends TestCase
public function testThrowsInvalidUrlExceptionIfTheUrlIsInvalid(string $source, string $target) public function testThrowsInvalidUrlExceptionIfTheUrlIsInvalid(string $source, string $target)
{ {
$this->expectException(InvalidUrlException::class); $this->expectException(InvalidUrlException::class);
$endpoint = new Endpoint($this->createMock(HttpClientInterface::class), $this->createMock(WebmentionGatewayInterface::class)); $this->callEndpoint($source, $target);
$endpoint->receiveWebmention($source, $target);
} }
public function testThrowsInvalidTargetExceptionIfTheTargetUrlIsNotOnCurrentSite() public function testThrowsInvalidTargetExceptionIfTheTargetUrlIsNotOnCurrentSite()
{ {
$this->expectException(InvalidTargetException::class); $this->expectException(InvalidTargetException::class);
$endpoint = new Endpoint($this->createMock(HttpClientInterface::class), $this->createMock(WebmentionGatewayInterface::class)); $this->callEndpoint("https://a-valid-source.url", "https://not-my-site.com");
$endpoint->receiveWebmention("https://a-valid-source.url", "https://not-my-site.com");
} }
#[TestWith([404])] #[TestWith([404])]
@ -65,21 +84,17 @@ class EndpointTest extends TestCase
$source = "https://my-valid-source-url.com"; $source = "https://my-valid-source-url.com";
$target = "https://lewisdale.dev/post/a-post-page"; $target = "https://lewisdale.dev/post/a-post-page";
$mockClient = $this->createMock(HttpClientInterface::class); $this->mockClient->expects($this->once())
$mockResponse = $this->createMock(ResponseInterface::class);
$mockClient->expects($this->once())
->method('request') ->method('request')
->with($this->identicalTo('GET'), $this->identicalTo($source)) ->with($this->identicalTo('GET'), $this->identicalTo($source))
->will($this->returnValue($mockResponse)); ->will($this->returnValue($this->mockResponse));
$mockResponse->method('getStatusCode') $this->mockResponse->method('getStatusCode')
->will($this->returnValue($statusCode)); ->will($this->returnValue($statusCode));
$this->expectException(SourceNotFoundException::class); $this->expectException(SourceNotFoundException::class);
$endpoint = new Endpoint($mockClient, $this->createMock(WebmentionGatewayInterface::class)); $this->callEndpoint($source, $target);
$endpoint->receiveWebmention($source, $target);
} }
public function testItShouldThrowATargetNotMentionedErrorIfTheSourceDoesNotMentionTheTarget() public function testItShouldThrowATargetNotMentionedErrorIfTheSourceDoesNotMentionTheTarget()
@ -99,24 +114,31 @@ class EndpointTest extends TestCase
</html> </html>
XML; XML;
$mockClient = $this->createMock(HttpClientInterface::class);
$mockResponse = $this->createMock(ResponseInterface::class);
$mockClient->expects($this->once()) $this->mockResponse->method('getStatusCode')
->method('request')
->with($this->identicalTo('GET'), $this->identicalTo($source))
->will($this->returnValue($mockResponse));
$mockResponse->method('getStatusCode')
->will($this->returnValue(200)); ->will($this->returnValue(200));
$mockResponse->method('getContent') $this->mockResponse->method('getContent')
->willReturn($content); ->willReturn($content);
$this->expectException(TargetNotMentionedException::class); $this->expectException(TargetNotMentionedException::class);
$endpoint = new Endpoint($mockClient, $this->createMock(WebmentionGatewayInterface::class)); $this->callEndpoint($source, $target);
$endpoint->receiveWebmention($source, $target); }
private function verifyContent(string $source, string $target, string $content, \PHPUnit\Framework\Constraint\Callback $verify)
{
$this->mockResponse->method('getStatusCode')
->will($this->returnValue(200));
$this->mockResponse->method('getContent')
->willReturn($content);
$this->mockGateway->expects($this->once())
->method('save')
->with($verify);
$this->callEndpoint($source, $target);
} }
public function testItShouldParseAWebmentionAsALikeIfItHasTheCorrectMicroFormat() public function testItShouldParseAWebmentionAsALikeIfItHasTheCorrectMicroFormat()
@ -138,28 +160,7 @@ class EndpointTest extends TestCase
</html> </html>
XML; XML;
$mockClient = $this->createMock(HttpClientInterface::class); $this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Like));
$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('type', MentionType::Like));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
} }
public function testItShouldParseAWebmentionAsAMentionIfItHasNoMicroformat() public function testItShouldParseAWebmentionAsAMentionIfItHasNoMicroformat()
@ -181,27 +182,7 @@ class EndpointTest extends TestCase
</html> </html>
XML; XML;
$mockClient = $this->createMock(HttpClientInterface::class); $this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Mention));
$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('type', MentionType::Mention));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
} }
public function testItShouldParseAWebmentionAsAReplyIfItHasTheCorrectMicroFormat() public function testItShouldParseAWebmentionAsAReplyIfItHasTheCorrectMicroFormat()
@ -223,27 +204,7 @@ class EndpointTest extends TestCase
</html> </html>
XML; XML;
$mockClient = $this->createMock(HttpClientInterface::class); $this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Reply));
$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('type', MentionType::Reply));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
} }
public function testItShouldParseAWebmentionAsARepost() public function testItShouldParseAWebmentionAsARepost()
@ -265,27 +226,7 @@ class EndpointTest extends TestCase
</html> </html>
XML; XML;
$mockClient = $this->createMock(HttpClientInterface::class); $this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Repost));
$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('type', MentionType::Repost));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
} }
public function testItShouldParseARepostsContent() public function testItShouldParseARepostsContent()
@ -307,27 +248,8 @@ class EndpointTest extends TestCase
</html> </html>
XML; XML;
$mockClient = $this->createMock(HttpClientInterface::class);
$mockResponse = $this->createMock(ResponseInterface::class);
$mockGateway = $this->createMock(WebmentionGatewayInterface::class);
$mockClient->expects($this->once()) $this->verifyContent($source, $target, $content, $this->objectContains('content', "Reposted this post"));
->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() public function testItShouldParseALikeContent()
@ -347,27 +269,7 @@ class EndpointTest extends TestCase
</html> </html>
HTML; HTML;
$mockClient = $this->createMock(HttpClientInterface::class); $this->verifyContent($source, $target, $content, $this->objectContains('content', "Liked this post"));
$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() public function testItShouldParseAReplyContent()
@ -387,27 +289,7 @@ class EndpointTest extends TestCase
</html> </html>
HTML; HTML;
$mockClient = $this->createMock(HttpClientInterface::class); $this->verifyContent($source, $target, $content, $this->objectContains('content', "@post: That's a great idea!"));
$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() public function testItShouldParseAnAuthorCardWithANameUrlAndPhoto()
@ -431,33 +313,9 @@ class EndpointTest extends TestCase
</html> </html>
HTML; HTML;
$mockClient = $this->createMock(HttpClientInterface::class); $expected = new \Lewisdale\Webmentions\Models\Author(null, "Anne Author", "https://my-blog.com", "https://dummyimage.com/100x100/fff/aaa");
$mockResponse = $this->createMock(ResponseInterface::class);
$mockGateway = $this->createMock(WebmentionGatewayInterface::class);
$mockClient->expects($this->once()) $this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
->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);
} }
public function testItShouldParseAnAuthorCardWithAMinimalHCard() public function testItShouldParseAnAuthorCardWithAMinimalHCard()
@ -480,33 +338,13 @@ class EndpointTest extends TestCase
</html> </html>
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( $expected = new \Lewisdale\Webmentions\Models\Author(
null, null,
"microformats.org", "microformats.org",
"https://microformats.org/", "https://microformats.org/",
"" ""
); );
$mockGateway->expects($this->once()) $this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
->method('save')
->with($this->objectContains('author', $expected));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
} }
public function testItShouldParseAnAuthorCardWithAVeryMinimalHCard() public function testItShouldParseAnAuthorCardWithAVeryMinimalHCard()
@ -527,33 +365,14 @@ class EndpointTest extends TestCase
</html> </html>
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( $expected = new \Lewisdale\Webmentions\Models\Author(
null, null,
"microformats.org", "microformats.org",
"https://microformats.org/", "https://microformats.org/",
"" ""
); );
$mockGateway->expects($this->once()) $this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
->method('save')
->with($this->objectContains('author', $expected));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
} }
public function testItShouldParseAnAuthorCardOutsideTheEntrry() public function testItShouldParseAnAuthorCardOutsideTheEntrry()
@ -576,33 +395,12 @@ class EndpointTest extends TestCase
</body> </body>
</html> </html>
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( $expected = new \Lewisdale\Webmentions\Models\Author(
null, null,
"Anne Author", "Anne Author",
"https://my-blog.com", "https://my-blog.com",
"https://dummyimage.com/100x100/fff/aaa" "https://dummyimage.com/100x100/fff/aaa"
); );
$mockGateway->expects($this->once()) $this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
->method('save')
->with($this->objectContains('author', $expected));
$endpoint = new Endpoint($mockClient, $mockGateway);
$endpoint->receiveWebmention($source, $target);
} }
} }