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