|
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); |
|
|
|
|
|
|
40
|
|
|
$this->response = $this->createMock(Response::class); |
|
|
|
|
|
|
41
|
|
|
$this->mockBody = $this->createMock(StreamInterface::class); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
52
|
|
|
$this->response->expects($this->once())->method('getStatusCode')->willReturn(123); |
|
|
|
|
|
|
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('{ |
|
|
|
|
|
|
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
|
|
|
|
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..