1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\JsonRpc\Tests; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
8
|
|
|
use GuzzleHttp\HandlerStack; |
9
|
|
|
use GuzzleHttp\Promise\Promise; |
10
|
|
|
use GuzzleHttp\Psr7\Response; |
11
|
|
|
use GuzzleHttp\Psr7\Uri; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Prophecy\Argument; |
14
|
|
|
use Psr\Http\Message\RequestInterface; |
15
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcClient; |
16
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcNotification; |
17
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcRequest; |
18
|
|
|
use ScayTrase\Api\Rpc\RpcErrorInterface; |
19
|
|
|
|
20
|
|
|
abstract class AbstractJsonRpcClientTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** @var MockHandler */ |
23
|
|
|
protected $queue; |
24
|
|
|
/** @var ClientInterface */ |
25
|
|
|
protected $client; |
26
|
|
|
|
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
$this->queue = null; |
30
|
|
|
$this->client = null; |
31
|
|
|
parent::setUp(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function tearDown() |
35
|
|
|
{ |
36
|
|
|
if (null !== $this->queue) { |
37
|
|
|
self::assertEquals(0, $this->getQueue()->count()); |
38
|
|
|
} |
39
|
|
|
parent::tearDown(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** @return MockHandler */ |
43
|
|
|
protected function getQueue() |
44
|
|
|
{ |
45
|
|
|
if (null === $this->queue) { |
46
|
|
|
$this->queue = new MockHandler(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->queue; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @return ClientInterface */ |
53
|
|
|
protected function getClient() |
54
|
|
|
{ |
55
|
|
|
if (null === $this->client) { |
56
|
|
|
$handler = HandlerStack::create($this->getQueue()); |
57
|
|
|
$this->client = new Client(['handler' => $handler]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->client; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function createRequestForSingleInvocation($method, array $parameters, $result) |
64
|
|
|
{ |
65
|
|
|
$hash = $this->getRandomHash(); |
66
|
|
|
|
67
|
|
|
$this->pushResult($result, $hash); |
68
|
|
|
|
69
|
|
|
return new JsonRpcRequest($method, $parameters, $hash); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function getRandomHash() |
76
|
|
|
{ |
77
|
|
|
return bin2hex(random_bytes(20)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function createNotificationForSingleInvocation($method, array $parameters) |
81
|
|
|
{ |
82
|
|
|
$notification = new JsonRpcNotification($method, $parameters); |
83
|
|
|
$this->getQueue()->append(new Response(200, [], null)); |
84
|
|
|
|
85
|
|
|
return $notification; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $method |
90
|
|
|
* @param mixed $params |
91
|
|
|
* @param bool $batch |
92
|
|
|
* @return JsonRpcClient |
93
|
|
|
*/ |
94
|
|
|
protected function getProphetClient($method, $params, $batch) |
95
|
|
|
{ |
96
|
|
|
$guzzle = $this->prophesize(ClientInterface::class); |
97
|
|
|
$self = $this; |
98
|
|
|
$guzzle->sendAsync(Argument::type(RequestInterface::class))->will( |
99
|
|
|
function ($args) use ($method, $self, $params, $batch) { |
100
|
|
|
/** @var RequestInterface $request */ |
101
|
|
|
$request = $args[0]; |
102
|
|
|
$content = $request->getBody()->getContents(); |
103
|
|
|
$data = json_decode($content); |
104
|
|
|
if ($batch) { |
105
|
|
|
$self::assertTrue(is_array($data)); |
106
|
|
|
$self::assertNotEmpty($data); |
107
|
|
|
$data = array_shift($data); |
108
|
|
|
} |
109
|
|
|
$self::assertEquals(JSON_ERROR_NONE, json_last_error()); |
110
|
|
|
$self::assertObjectHasAttribute('id', $data); |
111
|
|
|
$self::assertObjectHasAttribute('method', $data); |
112
|
|
|
$self::assertObjectHasAttribute('params', $data); |
113
|
|
|
$self::assertObjectHasAttribute('jsonrpc', $data); |
114
|
|
|
$self::assertEquals('test', $data->id); |
115
|
|
|
$self::assertEquals('2.0', $data->jsonrpc); |
116
|
|
|
$self::assertEquals($method, $data->method); |
117
|
|
|
$self::assertEquals($params, $data->params); |
118
|
|
|
|
119
|
|
|
return new Promise( |
120
|
|
|
function () { |
121
|
|
|
}, |
122
|
|
|
function () { |
123
|
|
|
} |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
return new JsonRpcClient($guzzle->reveal(), new Uri('http://localhost/')); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $result |
133
|
|
|
* @param $hash |
134
|
|
|
*/ |
135
|
|
|
protected function pushResult($result, $hash = null) |
136
|
|
|
{ |
137
|
|
|
if ($result instanceof GuzzleException) { |
138
|
|
|
$this->getQueue()->append($result); |
139
|
|
|
} elseif ($result instanceof RpcErrorInterface) { |
140
|
|
|
$this->getQueue()->append( |
141
|
|
|
new Response( |
142
|
|
|
200, |
143
|
|
|
[], |
144
|
|
|
json_encode( |
145
|
|
|
[ |
146
|
|
|
[ |
147
|
|
|
'jsonrpc' => '2.0', |
148
|
|
|
'id' => $hash, |
149
|
|
|
'error' => [ |
150
|
|
|
'code' => $result->getCode(), |
151
|
|
|
'message' => $result->getMessage(), |
152
|
|
|
], |
153
|
|
|
], |
154
|
|
|
] |
155
|
|
|
) |
156
|
|
|
) |
157
|
|
|
); |
158
|
|
|
} else { |
159
|
|
|
$this->getQueue()->append( |
160
|
|
|
new Response( |
161
|
|
|
200, |
162
|
|
|
[], |
163
|
|
|
json_encode( |
164
|
|
|
[ |
165
|
|
|
[ |
166
|
|
|
'jsonrpc' => '2.0', |
167
|
|
|
'id' => $hash, |
168
|
|
|
'result' => $result, |
169
|
|
|
], |
170
|
|
|
] |
171
|
|
|
) |
172
|
|
|
) |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|