Refactor EndpointTests
This commit is contained in:
parent
9cdd917297
commit
3cafc5238e
@ -14,6 +14,17 @@ use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
|
||||
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)
|
||||
{
|
||||
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(["my.url.com", false])]
|
||||
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");
|
||||
}
|
||||
|
||||
@ -42,15 +63,13 @@ class EndpointTest extends TestCase
|
||||
public function testThrowsInvalidUrlExceptionIfTheUrlIsInvalid(string $source, string $target)
|
||||
{
|
||||
$this->expectException(InvalidUrlException::class);
|
||||
$endpoint = new Endpoint($this->createMock(HttpClientInterface::class), $this->createMock(WebmentionGatewayInterface::class));
|
||||
$endpoint->receiveWebmention($source, $target);
|
||||
$this->callEndpoint($source, $target);
|
||||
}
|
||||
|
||||
public function testThrowsInvalidTargetExceptionIfTheTargetUrlIsNotOnCurrentSite()
|
||||
{
|
||||
$this->expectException(InvalidTargetException::class);
|
||||
$endpoint = new Endpoint($this->createMock(HttpClientInterface::class), $this->createMock(WebmentionGatewayInterface::class));
|
||||
$endpoint->receiveWebmention("https://a-valid-source.url", "https://not-my-site.com");
|
||||
$this->callEndpoint("https://a-valid-source.url", "https://not-my-site.com");
|
||||
}
|
||||
|
||||
#[TestWith([404])]
|
||||
@ -65,21 +84,17 @@ class EndpointTest extends TestCase
|
||||
$source = "https://my-valid-source-url.com";
|
||||
$target = "https://lewisdale.dev/post/a-post-page";
|
||||
|
||||
$mockClient = $this->createMock(HttpClientInterface::class);
|
||||
$mockResponse = $this->createMock(ResponseInterface::class);
|
||||
|
||||
$mockClient->expects($this->once())
|
||||
$this->mockClient->expects($this->once())
|
||||
->method('request')
|
||||
->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));
|
||||
|
||||
$this->expectException(SourceNotFoundException::class);
|
||||
|
||||
$endpoint = new Endpoint($mockClient, $this->createMock(WebmentionGatewayInterface::class));
|
||||
$endpoint->receiveWebmention($source, $target);
|
||||
$this->callEndpoint($source, $target);
|
||||
}
|
||||
|
||||
public function testItShouldThrowATargetNotMentionedErrorIfTheSourceDoesNotMentionTheTarget()
|
||||
@ -99,24 +114,31 @@ class EndpointTest extends TestCase
|
||||
</html>
|
||||
XML;
|
||||
|
||||
$mockClient = $this->createMock(HttpClientInterface::class);
|
||||
$mockResponse = $this->createMock(ResponseInterface::class);
|
||||
|
||||
$mockClient->expects($this->once())
|
||||
->method('request')
|
||||
->with($this->identicalTo('GET'), $this->identicalTo($source))
|
||||
->will($this->returnValue($mockResponse));
|
||||
|
||||
$mockResponse->method('getStatusCode')
|
||||
$this->mockResponse->method('getStatusCode')
|
||||
->will($this->returnValue(200));
|
||||
|
||||
$mockResponse->method('getContent')
|
||||
$this->mockResponse->method('getContent')
|
||||
->willReturn($content);
|
||||
|
||||
$this->expectException(TargetNotMentionedException::class);
|
||||
|
||||
$endpoint = new Endpoint($mockClient, $this->createMock(WebmentionGatewayInterface::class));
|
||||
$endpoint->receiveWebmention($source, $target);
|
||||
$this->callEndpoint($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()
|
||||
@ -138,28 +160,7 @@ class EndpointTest extends TestCase
|
||||
</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('type', MentionType::Like));
|
||||
|
||||
$endpoint = new Endpoint($mockClient, $mockGateway);
|
||||
|
||||
$endpoint->receiveWebmention($source, $target);
|
||||
$this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Like));
|
||||
}
|
||||
|
||||
public function testItShouldParseAWebmentionAsAMentionIfItHasNoMicroformat()
|
||||
@ -181,27 +182,7 @@ class EndpointTest extends TestCase
|
||||
</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('type', MentionType::Mention));
|
||||
|
||||
$endpoint = new Endpoint($mockClient, $mockGateway);
|
||||
$endpoint->receiveWebmention($source, $target);
|
||||
$this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Mention));
|
||||
}
|
||||
|
||||
public function testItShouldParseAWebmentionAsAReplyIfItHasTheCorrectMicroFormat()
|
||||
@ -223,27 +204,7 @@ class EndpointTest extends TestCase
|
||||
</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('type', MentionType::Reply));
|
||||
|
||||
$endpoint = new Endpoint($mockClient, $mockGateway);
|
||||
$endpoint->receiveWebmention($source, $target);
|
||||
$this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Reply));
|
||||
}
|
||||
|
||||
public function testItShouldParseAWebmentionAsARepost()
|
||||
@ -265,27 +226,7 @@ class EndpointTest extends TestCase
|
||||
</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('type', MentionType::Repost));
|
||||
|
||||
$endpoint = new Endpoint($mockClient, $mockGateway);
|
||||
$endpoint->receiveWebmention($source, $target);
|
||||
$this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Repost));
|
||||
}
|
||||
|
||||
public function testItShouldParseARepostsContent()
|
||||
@ -307,27 +248,8 @@ class EndpointTest extends TestCase
|
||||
</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);
|
||||
$this->verifyContent($source, $target, $content, $this->objectContains('content', "Reposted this post"));
|
||||
}
|
||||
|
||||
public function testItShouldParseALikeContent()
|
||||
@ -347,27 +269,7 @@ class EndpointTest extends TestCase
|
||||
</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);
|
||||
$this->verifyContent($source, $target, $content, $this->objectContains('content', "Liked this post"));
|
||||
}
|
||||
|
||||
public function testItShouldParseAReplyContent()
|
||||
@ -387,27 +289,7 @@ class EndpointTest extends TestCase
|
||||
</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);
|
||||
$this->verifyContent($source, $target, $content, $this->objectContains('content', "@post: That's a great idea!"));
|
||||
}
|
||||
|
||||
public function testItShouldParseAnAuthorCardWithANameUrlAndPhoto()
|
||||
@ -431,33 +313,9 @@ class EndpointTest extends TestCase
|
||||
</html>
|
||||
HTML;
|
||||
|
||||
$mockClient = $this->createMock(HttpClientInterface::class);
|
||||
$mockResponse = $this->createMock(ResponseInterface::class);
|
||||
$mockGateway = $this->createMock(WebmentionGatewayInterface::class);
|
||||
$expected = new \Lewisdale\Webmentions\Models\Author(null, "Anne Author", "https://my-blog.com", "https://dummyimage.com/100x100/fff/aaa");
|
||||
|
||||
$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);
|
||||
$this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
|
||||
}
|
||||
|
||||
public function testItShouldParseAnAuthorCardWithAMinimalHCard()
|
||||
@ -480,33 +338,13 @@ class EndpointTest extends TestCase
|
||||
</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);
|
||||
$this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
|
||||
}
|
||||
|
||||
public function testItShouldParseAnAuthorCardWithAVeryMinimalHCard()
|
||||
@ -527,33 +365,14 @@ class EndpointTest extends TestCase
|
||||
</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));
|
||||
$this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
|
||||
|
||||
$endpoint = new Endpoint($mockClient, $mockGateway);
|
||||
$endpoint->receiveWebmention($source, $target);
|
||||
}
|
||||
|
||||
public function testItShouldParseAnAuthorCardOutsideTheEntrry()
|
||||
@ -576,33 +395,12 @@ class EndpointTest extends TestCase
|
||||
</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);
|
||||
$this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user