summaryrefslogtreecommitdiffstats
path: root/tests/Unit/Fetcher/FeedIoClientTest.php
blob: 4020bc51f04c061e477bee725beb19e53e813ff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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'));
    }
}