1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scaytrase\Api\Rpc\Tests; |
4
|
|
|
|
5
|
|
|
use ScayTrase\Api\Rpc\RpcRequestInterface; |
6
|
|
|
use ScayTrase\Api\Rpc\Test\MockClientException; |
7
|
|
|
use ScayTrase\Api\Rpc\Test\RpcMockClient; |
8
|
|
|
|
9
|
|
|
class RpcMockClientTest extends AbstractRpcTest |
10
|
|
|
{ |
11
|
|
|
public function testClientReturnsResponse() |
12
|
|
|
{ |
13
|
|
|
$client = new RpcMockClient(); |
14
|
|
|
$client->push($this->getResponseMock(true, 5)); |
15
|
|
|
$request = $this->getRequestMock('test'); |
16
|
|
|
$response = $client->invoke($request)->getResponse($request); |
17
|
|
|
self::assertTrue($response->isSuccessful()); |
18
|
|
|
self::assertNull($response->getError()); |
19
|
|
|
self::assertSame(5, $response->getBody()); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @expectedException \ScayTrase\Api\Rpc\Test\MockClientException |
24
|
|
|
*/ |
25
|
|
|
public function testClientFiltersResponse() |
26
|
|
|
{ |
27
|
|
|
$client = new RpcMockClient(); |
28
|
|
|
$client->push( |
29
|
|
|
$this->getResponseMock(true, 5), |
30
|
|
|
function (RpcRequestInterface $request) { |
31
|
|
|
return $request->getMethod() === 'entity'; |
32
|
|
|
} |
33
|
|
|
); |
34
|
|
|
$request = $this->getRequestMock('test'); |
35
|
|
|
$client->invoke($request)->getResponse($request); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testClientReturnsMultipleResponses() |
39
|
|
|
{ |
40
|
|
|
$client = new RpcMockClient(); |
41
|
|
|
$client->push($this->getResponseMock(true, 5)); |
42
|
|
|
$client->push($this->getResponseMock(false, null, $this->getErrorMock(-1, 'invalid'))); |
43
|
|
|
$client->push( |
44
|
|
|
$this->getResponseMock(true, []), |
45
|
|
|
function (RpcRequestInterface $request) { |
46
|
|
|
return $request->getMethod() === 'entity'; |
47
|
|
|
} |
48
|
|
|
); |
49
|
|
|
self::assertCount(3, $client->getQueue()); |
50
|
|
|
$request1 = $this->getRequestMock('test'); |
51
|
|
|
$request2 = $this->getRequestMock('test2'); |
52
|
|
|
$request3 = $this->getRequestMock('entity'); |
53
|
|
|
$response1 = $client->invoke($request1)->getResponse($request1); |
54
|
|
|
self::assertCount(2, $client->getQueue()); |
55
|
|
|
self::assertTrue($response1->isSuccessful()); |
56
|
|
|
self::assertNull($response1->getError()); |
57
|
|
|
self::assertSame(5, $response1->getBody()); |
58
|
|
|
|
59
|
|
|
$coll = $client->invoke([$request2, $request3]); |
60
|
|
|
self::assertCount(0, $client->getQueue()); |
61
|
|
|
$response2 = $coll->getResponse($request2); |
62
|
|
|
$response3 = $coll->getResponse($request3); |
63
|
|
|
|
64
|
|
|
self::assertFalse($response2->isSuccessful()); |
65
|
|
|
self::assertNull($response2->getBody()); |
66
|
|
|
self::assertNotNull($response2->getError()); |
67
|
|
|
self::assertSame(-1, $response2->getError()->getCode()); |
68
|
|
|
self::assertSame('invalid', $response2->getError()->getMessage()); |
69
|
|
|
|
70
|
|
|
self::assertTrue($response3->isSuccessful()); |
71
|
|
|
self::assertNull($response3->getError()); |
72
|
|
|
self::assertSame([], $response3->getBody()); |
73
|
|
|
|
74
|
|
|
$request4 = $this->getRequestMock('empty'); |
75
|
|
|
self::assertCount(0, $client->getQueue()); |
76
|
|
|
try { |
77
|
|
|
$client->invoke($request4); |
78
|
|
|
} catch (MockClientException $exception) { |
79
|
|
|
self::assertEquals($request4, $exception->getRequest()); |
80
|
|
|
self::assertEquals('Mock queue is empty while calling "empty"', $exception->getMessage()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
self::assertCount(2, $coll); |
84
|
|
|
foreach ($coll as $response) { |
85
|
|
|
self::assertContains($response, [$response2, $response3]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|