summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Molenaar <sean@seanmolenaar.eu>2021-01-20 21:45:05 +0100
committerSean Molenaar <SMillerDev@users.noreply.github.com>2021-01-20 22:52:09 +0100
commit8fa1537cbfa7bc37412c029887dc9ea455aade42 (patch)
treeee1a38664f78028ceb36e7649406e635e9ef0d44
parent54e3b2e57a9d4b2c312647fbd6247fe1bea41c7d (diff)
Fetcher: Update client and add test
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
-rw-r--r--AUTHORS.md2
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/Fetcher/Client/FeedIoClient.php21
-rw-r--r--tests/Unit/Fetcher/FeedIoClientTest.php92
4 files changed, 108 insertions, 8 deletions
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 @@
+<?php
+
+
+namespace OCA\News\Tests\Unit\Fetcher;
+
+
+use DateTime;
+use FeedIo\Adapter\Guzzle\Response;
+use FeedIo\Adapter\NotFoundException;
+use FeedIo\Adapter\ServerErrorException;
+use GuzzleHttp\ClientInterface;
+use GuzzleHttp\Exception\BadResponseException;
+use OCA\News\Fetcher\Client\FeedIoClient;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Psr\Http\Message\RequestInterface;
+use Psr\Http\Message\ResponseInterface;
+
+class FeedIoClientTest extends TestCase
+{
+
+ /**
+ * @var FeedIoClient
+ */
+ protected $class;
+ /**
+ * @var ClientInterface|MockObject
+ */
+ protected $guzzleClient;
+
+ protected function setUp(): void
+ {
+ $this->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