Issues (50)

tests/Service/APIClientTest.php (6 issues)

1
<?php
2
3
namespace SilverStripe\CKANRegistry\Tests\Service;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Response;
7
use Psr\Http\Message\StreamInterface;
8
use RuntimeException;
9
use SilverStripe\CKANRegistry\Model\Resource;
10
use SilverStripe\CKANRegistry\Service\APIClient;
11
use SilverStripe\Dev\SapphireTest;
12
13
class APIClientTest extends SapphireTest
14
{
15
    /**
16
     * @var Client
17
     */
18
    protected $guzzleClient;
19
20
    /**
21
     * @var Response
22
     */
23
    protected $response;
24
25
    /**
26
     * @var StreamInterface
27
     */
28
    protected $mockBody;
29
30
    /**
31
     * @var Resource
32
     */
33
    protected $resource;
34
35
    protected function setUp()
36
    {
37
        parent::setUp();
38
39
        $this->guzzleClient = $this->createMock(Client::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(GuzzleHttp\Client::class) of type PHPUnit_Framework_MockObject_MockObject is incompatible with the declared type GuzzleHttp\Client of property $guzzleClient.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
        $this->response = $this->createMock(Response::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Guzzle...p\Psr7\Response::class) of type PHPUnit_Framework_MockObject_MockObject is incompatible with the declared type GuzzleHttp\Psr7\Response of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        $this->mockBody = $this->createMock(StreamInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Ht...StreamInterface::class) of type PHPUnit_Framework_MockObject_MockObject is incompatible with the declared type Psr\Http\Message\StreamInterface of property $mockBody.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->resource = new Resource();
43
    }
44
45
    /**
46
     * @expectedException RuntimeException
47
     * @expectedExceptionMessage CKAN API is not available. Error code 123
48
     */
49
    public function testExceptionThrownOnInvalidHttpStatusCode()
50
    {
51
        $this->guzzleClient->expects($this->once())->method('send')->willReturn($this->response);
0 ignored issues
show
The method method() does not exist on GuzzleHttp\Promise\PromiseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $this->guzzleClient->expects($this->once())->/** @scrutinizer ignore-call */ method('send')->willReturn($this->response);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
        $this->response->expects($this->once())->method('getStatusCode')->willReturn(123);
0 ignored issues
show
The method expects() does not exist on GuzzleHttp\Psr7\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        $this->response->/** @scrutinizer ignore-call */ 
53
                         expects($this->once())->method('getStatusCode')->willReturn(123);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
54
        $client = new APIClient();
55
        $client->setGuzzleClient($this->guzzleClient);
56
        $client->getData($this->resource);
57
    }
58
59
    /**
60
     * @expectedException RuntimeException
61
     * @expectedExceptionMessage CKAN API returns an invalid response: Content-Type is not JSON
62
     */
63
    public function testExceptionThrownOnNonJsonResponse()
64
    {
65
        $this->guzzleClient->expects($this->once())->method('send')->willReturn($this->response);
66
        $this->response->expects($this->once())->method('getStatusCode')->willReturn(200);
67
        $this->response->expects($this->once())->method('getHeader')->with('Content-Type')->willReturn(['junk']);
68
69
        $client = new APIClient();
70
        $client->setGuzzleClient($this->guzzleClient);
71
        $client->getData($this->resource);
72
    }
73
74
    /**
75
     * @expectedException RuntimeException
76
     * @expectedExceptionMessage CKAN API returns an invalid response: Responded as invalid
77
     */
78
    public function testExceptionThrownOnUnsuccessfulResponse()
79
    {
80
        $this->guzzleClient->expects($this->once())->method('send')->willReturn($this->response);
81
        $this->response->expects($this->once())->method('getStatusCode')->willReturn(200);
82
        $this->response->expects($this->once())->method('getHeader')->willReturn(['application/json']);
83
        $this->response->expects($this->once())->method('getBody')->willReturn($this->mockBody);
84
        $this->mockBody->expects($this->once())->method('getContents')->willReturn('{
0 ignored issues
show
The method expects() does not exist on Psr\Http\Message\StreamInterface. It seems like you code against a sub-type of said class. However, the method does not exist in GuzzleHttp\Psr7\BufferStream or GuzzleHttp\Psr7\PumpStream or GuzzleHttp\Psr7\AppendStream or GuzzleHttp\Psr7\Stream or GuzzleHttp\Psr7\FnStream. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        $this->mockBody->/** @scrutinizer ignore-call */ 
85
                         expects($this->once())->method('getContents')->willReturn('{
Loading history...
85
            "success": false
86
        }');
87
88
        $client = new APIClient();
89
        $client->setGuzzleClient($this->guzzleClient);
90
        $client->getData($this->resource);
91
    }
92
93
    public function testReturnsResponseData()
94
    {
95
        $this->guzzleClient->expects($this->once())->method('send')->willReturn($this->response);
96
        $this->response->expects($this->once())->method('getStatusCode')->willReturn(200);
97
        $this->response->expects($this->once())->method('getHeader')->willReturn(['application/json']);
98
        $this->response->expects($this->once())->method('getBody')->willReturn($this->mockBody);
99
        $this->mockBody->expects($this->once())->method('getContents')->willReturn('{
100
            "success": true,
101
            "data": "test"
102
        }');
103
104
        $client = new APIClient();
105
        $client->setGuzzleClient($this->guzzleClient);
106
        $result = $client->getData($this->resource);
107
108
        $this->assertSame('test', $result['data'], 'Raw response body should be returned');
109
    }
110
}
111