1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Guzzle HTTP JSON-RPC |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @see http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE |
11
|
|
|
* @link http://github.com/graze/guzzle-jsonrpc |
12
|
|
|
*/ |
13
|
|
|
namespace Graze\GuzzleHttp\JsonRpc\Message; |
14
|
|
|
|
15
|
|
|
use Graze\GuzzleHttp\JsonRpc\ClientInterface; |
16
|
|
|
use Graze\GuzzleHttp\JsonRpc\Test\UnitTestCase; |
17
|
|
|
|
18
|
|
|
class ResponseTest extends UnitTestCase |
19
|
|
|
{ |
20
|
|
|
public function setUp() |
21
|
|
|
{ |
22
|
|
|
$this->stream = $this->mockStream(); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testInterface() |
26
|
|
|
{ |
27
|
|
|
$this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\Message\ResponseInterface', new Response(200)); |
28
|
|
|
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', new Response(200)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testGetRpcId() |
32
|
|
|
{ |
33
|
|
|
$response = new Response(200, [], json_encode([ |
34
|
|
|
'id' => 123 |
35
|
|
|
])); |
36
|
|
|
|
37
|
|
|
$this->assertEquals(123, $response->getRpcId()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testGetRpcIdIsNull() |
41
|
|
|
{ |
42
|
|
|
$response = new Response(200); |
43
|
|
|
|
44
|
|
|
$this->assertNull($response->getRpcId()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testGetRpcErrorCode() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$response = new Response(200, [], json_encode([ |
50
|
|
|
'error' => ['code'=>123] |
51
|
|
|
])); |
52
|
|
|
|
53
|
|
|
$this->assertEquals(123, $response->getRpcErrorCode()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetRpcErrorCodeIsNull() |
57
|
|
|
{ |
58
|
|
|
$response = new Response(200); |
59
|
|
|
|
60
|
|
|
$this->assertNull($response->getRpcErrorCode()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetRpcErrorMessage() |
64
|
|
|
{ |
65
|
|
|
$response = new Response(200, [], json_encode([ |
66
|
|
|
'error' => ['message'=>'foo'] |
67
|
|
|
])); |
68
|
|
|
|
69
|
|
|
$this->assertEquals('foo', $response->getRpcErrorMessage()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGetRpcErrorMessageIsNull() |
73
|
|
|
{ |
74
|
|
|
$response = new Response(200); |
75
|
|
|
|
76
|
|
|
$this->assertNull($response->getRpcErrorMessage()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function testGetRpcErrorData() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$response = new Response(200, [], json_encode([ |
82
|
|
|
'error' => ['data' => []] |
83
|
|
|
])); |
84
|
|
|
|
85
|
|
|
$this->assertEquals([], $response->getRpcErrorData()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testGetRpcErrorDataIsNull() |
89
|
|
|
{ |
90
|
|
|
$response = new Response(200); |
91
|
|
|
|
92
|
|
|
$this->assertNull($response->getRpcErrorData()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testGetRpcResult() |
96
|
|
|
{ |
97
|
|
|
$response = new Response(200, [], json_encode([ |
98
|
|
|
'result' => 'foo' |
99
|
|
|
])); |
100
|
|
|
|
101
|
|
|
$this->assertEquals('foo', $response->getRpcResult()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testGetRpcResultIsNull() |
105
|
|
|
{ |
106
|
|
|
$response = new Response(200); |
107
|
|
|
|
108
|
|
|
$this->assertNull($response->getRpcResult()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
public function testGetRpcVersion() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$response = new Response(200, [], json_encode([ |
114
|
|
|
'jsonrpc' => 'foo' |
115
|
|
|
])); |
116
|
|
|
|
117
|
|
|
$this->assertEquals('foo', $response->getRpcVersion()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testGetRpcVersionIsNull() |
121
|
|
|
{ |
122
|
|
|
$response = new Response(200); |
123
|
|
|
|
124
|
|
|
$this->assertNull($response->getRpcVersion()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: