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