1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scaytrase\Api\Rpc\Tests; |
4
|
|
|
|
5
|
|
|
use ScayTrase\Api\Rpc\RpcErrorInterface; |
6
|
|
|
use ScayTrase\Api\Rpc\RpcRequestInterface; |
7
|
|
|
use ScayTrase\Api\Rpc\RpcResponseInterface; |
8
|
|
|
use ScayTrase\Api\Rpc\Test\MockClientException; |
9
|
|
|
use ScayTrase\Api\Rpc\Test\RpcMockClient; |
10
|
|
|
|
11
|
|
|
class RpcMockClientTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
public function testClientReturnsResponse() |
14
|
|
|
{ |
15
|
|
|
$client = new RpcMockClient(); |
16
|
|
|
$client->push($this->creatResponseMock(true, 5)); |
17
|
|
|
$request = $this->createRequestMock('test'); |
18
|
|
|
$response = $client->invoke($request)->getResponse($request); |
19
|
|
|
self::assertTrue($response->isSuccessful()); |
20
|
|
|
self::assertNull($response->getError()); |
21
|
|
|
self::assertSame(5, $response->getBody()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @expectedException \ScayTrase\Api\Rpc\Test\MockClientException |
26
|
|
|
*/ |
27
|
|
|
public function testClientFiltersResponse() |
28
|
|
|
{ |
29
|
|
|
$client = new RpcMockClient(); |
30
|
|
|
$client->push( |
31
|
|
|
$this->creatResponseMock(true, 5), |
32
|
|
|
function (RpcRequestInterface $request) { |
33
|
|
|
return $request->getMethod() === 'entity'; |
34
|
|
|
} |
35
|
|
|
); |
36
|
|
|
$request = $this->createRequestMock('test'); |
37
|
|
|
$client->invoke($request)->getResponse($request); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testClientReturnsMultipleResponses() |
41
|
|
|
{ |
42
|
|
|
$client = new RpcMockClient(); |
43
|
|
|
$client->push($this->creatResponseMock(true, 5)); |
44
|
|
|
$client->push($this->creatResponseMock(false, null, $this->createErrorMock(-1, 'invalid'))); |
45
|
|
|
$client->push( |
46
|
|
|
$this->creatResponseMock(true, []), |
47
|
|
|
function (RpcRequestInterface $request) { |
48
|
|
|
return $request->getMethod() === 'entity'; |
49
|
|
|
} |
50
|
|
|
); |
51
|
|
|
self::assertCount(3, $client->getQueue()); |
52
|
|
|
$request1 = $this->createRequestMock('test'); |
53
|
|
|
$request2 = $this->createRequestMock('test2'); |
54
|
|
|
$request3 = $this->createRequestMock('entity'); |
55
|
|
|
$response1 = $client->invoke($request1)->getResponse($request1); |
56
|
|
|
self::assertCount(2, $client->getQueue()); |
57
|
|
|
self::assertTrue($response1->isSuccessful()); |
58
|
|
|
self::assertNull($response1->getError()); |
59
|
|
|
self::assertSame(5, $response1->getBody()); |
60
|
|
|
|
61
|
|
|
$coll = $client->invoke([$request2, $request3]); |
62
|
|
|
self::assertCount(0, $client->getQueue()); |
63
|
|
|
$response2 = $coll->getResponse($request2); |
64
|
|
|
$response3 = $coll->getResponse($request3); |
65
|
|
|
|
66
|
|
|
self::assertFalse($response2->isSuccessful()); |
67
|
|
|
self::assertNull($response2->getBody()); |
68
|
|
|
self::assertNotNull($response2->getError()); |
69
|
|
|
self::assertSame(-1, $response2->getError()->getCode()); |
70
|
|
|
self::assertSame('invalid', $response2->getError()->getMessage()); |
71
|
|
|
|
72
|
|
|
self::assertTrue($response3->isSuccessful()); |
73
|
|
|
self::assertNull($response3->getError()); |
74
|
|
|
self::assertSame([], $response3->getBody()); |
75
|
|
|
|
76
|
|
|
$request4 = $this->createRequestMock('empty'); |
77
|
|
|
self::assertCount(0, $client->getQueue()); |
78
|
|
|
try { |
79
|
|
|
$client->invoke($request4); |
80
|
|
|
} catch (MockClientException $exception) { |
81
|
|
|
self::assertEquals($request4, $exception->getRequest()); |
82
|
|
|
self::assertEquals('Mock queue is empty while calling "empty"', $exception->getMessage()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
self::assertCount(2, $coll); |
86
|
|
|
foreach ($coll as $response) { |
87
|
|
|
self::assertContains($response, [$response2, $response3]); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $method |
94
|
|
|
* @param \stdClass|array|null $parameters |
95
|
|
|
* |
96
|
|
|
* @return RpcRequestInterface |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
private function createRequestMock($method, $parameters = null) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$mock = $this->prophesize(RpcRequestInterface::class); |
101
|
|
|
$mock->getMethod()->willReturn((string)$method); |
|
|
|
|
102
|
|
|
$mock->getParameters()->willReturn($parameters); |
103
|
|
|
|
104
|
|
|
return $mock->reveal(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param bool $success |
109
|
|
|
* @param \stdClass|array|null|mixed $body |
110
|
|
|
* @param RpcErrorInterface $error |
111
|
|
|
* |
112
|
|
|
* @return RpcResponseInterface |
113
|
|
|
*/ |
114
|
|
|
private function creatResponseMock($success = true, $body = null, RpcErrorInterface $error = null) |
115
|
|
|
{ |
116
|
|
|
$mock = $this->prophesize(RpcResponseInterface::class); |
117
|
|
|
$mock->isSuccessful()->willReturn($success); |
118
|
|
|
$mock->getError()->willReturn($success ? null : $error); |
119
|
|
|
$mock->getBody()->willReturn($success ? $body : null); |
120
|
|
|
|
121
|
|
|
return $mock->reveal(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function createErrorMock($code, $message) |
125
|
|
|
{ |
126
|
|
|
$mock = $this->prophesize(RpcErrorInterface::class); |
127
|
|
|
$mock->getCode()->willReturn($code); |
128
|
|
|
$mock->getMessage()->willReturn($message); |
129
|
|
|
|
130
|
|
|
return $mock->reveal(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.