diff options
author | Sean Molenaar <sean@seanmolenaar.eu> | 2021-01-20 21:45:05 +0100 |
---|---|---|
committer | Sean Molenaar <SMillerDev@users.noreply.github.com> | 2021-01-20 22:52:09 +0100 |
commit | 8fa1537cbfa7bc37412c029887dc9ea455aade42 (patch) | |
tree | ee1a38664f78028ceb36e7649406e635e9ef0d44 /tests/Unit | |
parent | 54e3b2e57a9d4b2c312647fbd6247fe1bea41c7d (diff) |
Fetcher: Update client and add test
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
Diffstat (limited to 'tests/Unit')
-rw-r--r-- | tests/Unit/Fetcher/FeedIoClientTest.php | 92 |
1 files changed, 92 insertions, 0 deletions
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 |