|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Rpc\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Prophecy\Argument; |
|
6
|
|
|
use ScayTrase\Api\Rpc\ResponseCollectionInterface; |
|
7
|
|
|
use ScayTrase\Api\Rpc\RpcClientInterface; |
|
8
|
|
|
use ScayTrase\Api\Rpc\RpcErrorInterface; |
|
9
|
|
|
use ScayTrase\Api\Rpc\RpcRequestInterface; |
|
10
|
|
|
use ScayTrase\Api\Rpc\RpcResponseInterface; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractRpcTest extends \PHPUnit_Framework_TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param string $method |
|
16
|
|
|
* @param array $params |
|
17
|
|
|
* |
|
18
|
|
|
* @return RpcRequestInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected function getRequestMock($method, array $params = []) |
|
21
|
|
|
{ |
|
22
|
|
|
$request = $this->prophesize(RpcRequestInterface::class); |
|
23
|
|
|
$request->getMethod()->willReturn($method); |
|
|
|
|
|
|
24
|
|
|
$request->getParameters()->willReturn((object)$params); |
|
25
|
|
|
|
|
26
|
|
|
return $request->reveal(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param bool $success |
|
31
|
|
|
* @param \stdClass|array|null|mixed $body |
|
32
|
|
|
* @param RpcErrorInterface $error |
|
33
|
|
|
* |
|
34
|
|
|
* @return RpcResponseInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function getResponseMock($success = true, $body = null, RpcErrorInterface $error = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$mock = $this->prophesize(RpcResponseInterface::class); |
|
39
|
|
|
$mock->isSuccessful()->willReturn($success); |
|
40
|
|
|
$mock->getError()->willReturn($success ? null : $error); |
|
41
|
|
|
$mock->getBody()->willReturn($success ? $body : null); |
|
42
|
|
|
|
|
43
|
|
|
return $mock->reveal(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param mixed $code |
|
48
|
|
|
* @param string $message |
|
49
|
|
|
* |
|
50
|
|
|
* @return RpcErrorInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function getErrorMock($code, $message) |
|
53
|
|
|
{ |
|
54
|
|
|
$mock = $this->prophesize(RpcErrorInterface::class); |
|
55
|
|
|
$mock->getCode()->willReturn($code); |
|
56
|
|
|
$mock->getMessage()->willReturn($message); |
|
57
|
|
|
|
|
58
|
|
|
return $mock->reveal(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param RpcRequestInterface[] $requests |
|
63
|
|
|
* @param RpcResponseInterface[] $responses |
|
64
|
|
|
* |
|
65
|
|
|
* @return RpcClientInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getClientMock(array $requests = [], array $responses = []) |
|
68
|
|
|
{ |
|
69
|
|
|
self::assertEquals(count($requests), count($responses)); |
|
70
|
|
|
|
|
71
|
|
|
$client = $this->prophesize(RpcClientInterface::class); |
|
72
|
|
|
$that = $this; |
|
73
|
|
|
$client->invoke(Argument::type('array'))->will( |
|
74
|
|
|
function ($args) use ($that, $requests, $responses) { |
|
75
|
|
|
$collection = $that->prophesize(ResponseCollectionInterface::class); |
|
76
|
|
|
foreach ($requests as $key => $request) { |
|
77
|
|
|
if (in_array($request, $args[0], true)) { |
|
78
|
|
|
$collection->getResponse(Argument::exact($request))->willReturn($responses[$key]); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $collection->reveal(); |
|
83
|
|
|
} |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
return $client->reveal(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.