1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EtherpadLite\Tests; |
4
|
|
|
|
5
|
|
|
use EtherpadLite\Response; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
class ResponseTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @dataProvider jsonResponseProvider |
13
|
|
|
*/ |
14
|
|
|
public function testResponse($rawResponse, $expected) |
15
|
|
|
{ |
16
|
|
|
$response = $this->getMockedResponse($rawResponse); |
17
|
|
|
|
18
|
|
|
$this->assertNotEmpty($response->getResponse()); |
19
|
|
|
$this->assertArrayHasKey('code', $response->getResponse()); |
20
|
|
|
$this->assertArrayHasKey('message', $response->getResponse()); |
21
|
|
|
$this->assertArrayHasKey('data', $response->getResponse()); |
22
|
|
|
$this->assertEquals($expected['code'], $response->getCode()); |
23
|
|
|
$this->assertEquals($expected['message'], $response->getMessage()); |
24
|
|
|
$this->assertEquals($expected['data'], $response->getData()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function jsonResponseProvider() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
[ |
31
|
|
|
'{"code":0,"message":"ok","data":{"groupID": "g.s8oes9dhwrvt0zif"}}', |
32
|
|
|
[ |
33
|
|
|
'code' => 0, |
34
|
|
|
'message' => 'ok', |
35
|
|
|
'data' => [ |
36
|
|
|
'groupID' => 'g.s8oes9dhwrvt0zif', |
37
|
|
|
], |
38
|
|
|
], |
39
|
|
|
], |
40
|
|
|
[ |
41
|
|
|
'{"code":0,"message":"ok","data":{"padIDs":["g.OTeVWJ4KuZyRBlDI$aaa","meinNEUESPad","sjImkoFqDK","t08dXmHh1t"]}}', |
42
|
|
|
[ |
43
|
|
|
'code' => 0, |
44
|
|
|
'message' => 'ok', |
45
|
|
|
'data' => [ |
46
|
|
|
'padIDs' => ['g.OTeVWJ4KuZyRBlDI$aaa', 'meinNEUESPad', 'sjImkoFqDK', 't08dXmHh1t'], |
47
|
|
|
], |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
[ |
51
|
|
|
'{"code":4,"message":"no or wrong API Key","data":null}', |
52
|
|
|
[ |
53
|
|
|
'code' => 4, |
54
|
|
|
'message' => 'no or wrong API Key', |
55
|
|
|
'data' => null, |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
[ |
59
|
|
|
'{"code":0,"message":"ok","data":null}', |
60
|
|
|
[ |
61
|
|
|
'code' => 0, |
62
|
|
|
'message' => 'ok', |
63
|
|
|
'data' => null, |
64
|
|
|
], |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testGetData() |
70
|
|
|
{ |
71
|
|
|
$rawResponse = '{"code":0,"message":"ok","data":{"groupID": "g.s8oes9dhwrvt0zif"}}'; |
72
|
|
|
$response = $this->getMockedResponse($rawResponse); |
73
|
|
|
|
74
|
|
|
$this->assertEquals('g.s8oes9dhwrvt0zif', $response->getData('groupID')); |
75
|
|
|
$this->assertNull($response->getData('padID')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $rawResponse |
80
|
|
|
* @return Response |
81
|
|
|
*/ |
82
|
|
|
private function getMockedResponse($rawResponse) |
83
|
|
|
{ |
84
|
|
|
$httpResponse = $this->getMockBuilder('\GuzzleHttp\Psr7\Response') |
85
|
|
|
->setConstructorArgs([200, [], $rawResponse]) |
86
|
|
|
->getMock(); |
87
|
|
|
|
88
|
|
|
$httpResponse |
89
|
|
|
->expects($this->once()) |
90
|
|
|
->method('getStatusCode') |
91
|
|
|
->will($this->returnValue(200)); |
92
|
|
|
|
93
|
|
|
$httpResponse |
94
|
|
|
->expects($this->once()) |
95
|
|
|
->method('getBody') |
96
|
|
|
->will($this->returnValue($rawResponse)); |
97
|
|
|
|
98
|
|
|
/** @var ResponseInterface $httpResponse */ |
99
|
|
|
$response = new Response($httpResponse); |
100
|
|
|
|
101
|
|
|
return $response; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|