From 8fa1537cbfa7bc37412c029887dc9ea455aade42 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Wed, 20 Jan 2021 21:45:05 +0100 Subject: Fetcher: Update client and add test Signed-off-by: Sean Molenaar --- AUTHORS.md | 2 +- CHANGELOG.md | 1 + lib/Fetcher/Client/FeedIoClient.php | 21 +++++--- tests/Unit/Fetcher/FeedIoClientTest.php | 92 +++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 tests/Unit/Fetcher/FeedIoClientTest.php diff --git a/AUTHORS.md b/AUTHORS.md index 5ef2f8aaa..6e4a6689f 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -7,8 +7,8 @@ * [Sean Molenaar](mailto:sean@seanmolenaar.eu) * [Morris Jobke](mailto:hey@morrisjobke.de) * [Sean Molenaar](mailto:SMillerDev@users.noreply.github.com) -* [Jan-Christoph Borchardt](mailto:hey@jancborchardt.net) * [anoy](mailto:anoymouserver+github@mailbox.org) +* [Jan-Christoph Borchardt](mailto:hey@jancborchardt.net) * [Daniel Schaal](mailto:daniel@schaal.email) * [Davide Saurino](mailto:davide.saurino@alcacoop.it) * [raghunayyar](mailto:me@iraghu.com) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0001ceaca..3c1754843 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is almost based on [Keep a Changelog](https://keepachangelog.com/en/1 ### Fixed - Fetch feed after creation (#1058) +- Update FeedIO Response call and add tests ## [15.2.0-beta2] - 2021-01-17 diff --git a/lib/Fetcher/Client/FeedIoClient.php b/lib/Fetcher/Client/FeedIoClient.php index 33a3db2ad..3a1427e84 100644 --- a/lib/Fetcher/Client/FeedIoClient.php +++ b/lib/Fetcher/Client/FeedIoClient.php @@ -10,12 +10,14 @@ namespace OCA\News\Fetcher\Client; +use DateTime; use FeedIo\Adapter\ClientInterface; use FeedIo\Adapter\ResponseInterface; use FeedIo\Adapter\Guzzle\Response; use FeedIo\Adapter\NotFoundException; use FeedIo\Adapter\ServerErrorException; use GuzzleHttp\Exception\BadResponseException; +use GuzzleHttp\Exception\GuzzleException; /** * Guzzle dependent HTTP client @@ -23,7 +25,7 @@ use GuzzleHttp\Exception\BadResponseException; class FeedIoClient implements ClientInterface { /** - * @var \GuzzleHttp\ClientInterface + * @param \GuzzleHttp\ClientInterface $guzzleClient */ protected $guzzleClient; @@ -37,12 +39,13 @@ class FeedIoClient implements ClientInterface /** * @param string $url - * @param \DateTime $modifiedSince - * @throws \FeedIo\Adapter\NotFoundException - * @throws \FeedIo\Adapter\ServerErrorException - * @return \FeedIo\Adapter\ResponseInterface + * @param DateTime $modifiedSince + * + * @return ResponseInterface + * @throws ServerErrorException|GuzzleException + * @throws NotFoundException */ - public function getResponse(string $url, \DateTime $modifiedSince) : ResponseInterface + public function getResponse(string $url, DateTime $modifiedSince) : ResponseInterface { $modifiedSince->setTimezone(new \DateTimeZone('GMT')); try { @@ -52,7 +55,11 @@ class FeedIoClient implements ClientInterface ] ]; - return new Response($this->guzzleClient->request('get', $url, $options)); + $start = microtime(true); + $psrResponse = $this->guzzleClient->request('get', $url, $options); + $duration = intval(round(microtime(true) - $start, 3) * 1000); + + return new Response($psrResponse, $duration); } catch (BadResponseException $e) { switch ((int) $e->getResponse()->getStatusCode()) { case 404: diff --git a/tests/Unit/Fetcher/FeedIoClientTest.php b/tests/Unit/Fetcher/FeedIoClientTest.php new file mode 100644 index 000000000..4020bc51f --- /dev/null +++ b/tests/Unit/Fetcher/FeedIoClientTest.php @@ -0,0 +1,92 @@ +guzzleClient = $this->getMockBuilder(ClientInterface::class) + ->getMock(); + + $this->class = new FeedIoClient($this->guzzleClient); + } + + public function testGetResponseSuccess(): void + { + $response = $this->getMockBuilder(ResponseInterface::class) + ->getMock(); + + $this->guzzleClient->expects($this->once()) + ->method('request') + ->with('get', 'url', ['headers' => ['If-Modified-Since' => 'Thu, 01 Jan 1970 00:00:00 GMT']]) + ->will($this->returnValue($response)); + + $result = $this->class->getResponse('url', new DateTime('@0')); + $this->assertInstanceOf(Response::class, $result); + } + + public function testGetResponse404(): void + { + $this->expectException(NotFoundException::class); + $this->expectExceptionMessage('error'); + + $request = $this->getMockBuilder(RequestInterface::class) + ->getMock(); + $response = $this->getMockBuilder(ResponseInterface::class) + ->getMock(); + + $response->expects($this->exactly(2)) + ->method('getStatusCode') + ->willReturn(404); + + $this->guzzleClient->expects($this->once()) + ->method('request') + ->with('get', 'url', ['headers' => ['If-Modified-Since' => 'Thu, 01 Jan 1970 00:00:00 GMT']]) + ->will($this->throwException(new BadResponseException('error', $request, $response))); + + $this->class->getResponse('url', new DateTime('@0')); + } + + public function testGetResponseThrows(): void + { + $this->expectException(ServerErrorException::class); + $this->expectExceptionMessage('error'); + + $request = $this->getMockBuilder(RequestInterface::class) + ->getMock(); + $response = $this->getMockBuilder(ResponseInterface::class) + ->getMock(); + + $this->guzzleClient->expects($this->once()) + ->method('request') + ->with('get', 'url', ['headers' => ['If-Modified-Since' => 'Thu, 01 Jan 1970 00:00:00 GMT']]) + ->will($this->throwException(new BadResponseException('error', $request, $response))); + + $this->class->getResponse('url', new DateTime('@0')); + } +} \ No newline at end of file -- cgit v1.2.3