Passed
Pull Request — master (#91)
by Robbie
02:50
created

ClientTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Tests\Service;
4
5
use GuzzleHttp\Psr7\Response;
6
use Psr\Http\Message\StreamInterface;
7
use RuntimeException;
8
use SilverStripe\CKANRegistry\Model\Resource;
9
use SilverStripe\CKANRegistry\Service\Client;
10
use SilverStripe\Dev\SapphireTest;
11
12
class ClientTest extends SapphireTest
13
{
14
    /**
15
     * @var \GuzzleHttp\Client
16
     */
17
    protected $guzzleClient;
18
19
    /**
20
     * @var Response
21
     */
22
    protected $response;
23
24
    /**
25
     * @var StreamInterface
26
     */
27
    protected $mockBody;
28
29
    /**
30
     * @var Resource
31
     */
32
    protected $resource;
33
34
    protected function setUp()
35
    {
36
        parent::setUp();
37
38
        $this->guzzleClient = $this->createMock(\GuzzleHttp\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...
39
        $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...
40
        $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...
41
        $this->resource = new Resource();
42
    }
43
44
    /**
45
     * @expectedException RuntimeException
46
     * @expectedExceptionMessage CKAN API is not available. Error code 123
47
     */
48
    public function testExceptionThrownOnInvalidHttpStatusCode()
49
    {
50
        $this->guzzleClient->expects($this->once())->method('send')->willReturn($this->response);
0 ignored issues
show
Bug introduced by
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

50
        $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...
51
        $this->response->expects($this->once())->method('getStatusCode')->willReturn(123);
0 ignored issues
show
Bug introduced by
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

51
        $this->response->/** @scrutinizer ignore-call */ 
52
                         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...
52
53
        $client = new Client();
54
        $client->setGuzzleClient($this->guzzleClient);
55
        $client->getData($this->resource);
56
    }
57
58
    /**
59
     * @expectedException RuntimeException
60
     * @expectedExceptionMessage CKAN API returns an invalid response: Content-Type is not JSON
61
     */
62
    public function testExceptionThrownOnNonJsonResponse()
63
    {
64
        $this->guzzleClient->expects($this->once())->method('send')->willReturn($this->response);
65
        $this->response->expects($this->once())->method('getStatusCode')->willReturn(200);
66
        $this->response->expects($this->once())->method('getHeader')->with('Content-Type')->willReturn(['junk']);
67
68
        $client = new Client();
69
        $client->setGuzzleClient($this->guzzleClient);
70
        $client->getData($this->resource);
71
    }
72
73
    /**
74
     * @expectedException RuntimeException
75
     * @expectedExceptionMessage CKAN API returns an invalid response: Responded as invalid
76
     */
77
    public function testExceptionThrownOnUnsuccessfulResponse()
78
    {
79
        $this->guzzleClient->expects($this->once())->method('send')->willReturn($this->response);
80
        $this->response->expects($this->once())->method('getStatusCode')->willReturn(200);
81
        $this->response->expects($this->once())->method('getHeader')->willReturn(['application/json']);
82
        $this->response->expects($this->once())->method('getBody')->willReturn($this->mockBody);
83
        $this->mockBody->expects($this->once())->method('getContents')->willReturn('{
0 ignored issues
show
Bug introduced by
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

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