-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonRpcClientTest.php
More file actions
139 lines (102 loc) · 4.58 KB
/
JsonRpcClientTest.php
File metadata and controls
139 lines (102 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Sal\Seven\Tests\Client\JsonRpc;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;
use Sal\Seven\Adapter\Http\HttpAdapterInterface;
use Sal\Seven\Client\JsonRpc\JsonRpcClient;
use Sal\Seven\Factory\HttpHeaderFactory;
use Sal\Seven\Model\ContentType;
use Sal\Seven\Model\Http\HttpResponse;
use Sal\Seven\Model\JsonRpc\JsonRpcResponse;
use Symfony\Component\Serializer\SerializerInterface;
class JsonRpcClientTest extends TestCase
{
/** @var SerializerInterface|MockObject */
private SerializerInterface $serializer;
/** @var HttpAdapterInterface|MockObject */
private HttpAdapterInterface $http;
private JsonRpcClient $client;
protected function setUp(): void
{
$this->serializer = $this->createMock(SerializerInterface::class);
$this->http = $this->createMock(HttpAdapterInterface::class);
$this->http
->expects($this->once())
->method('addHeader')
->with(HttpHeaderFactory::contentType(ContentType::JSON));
$this->client = new JsonRpcClient($this->serializer, $this->http);
}
public function testThrowsIfEndpointMissing(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('The JSON-RPC endpoint not set.');
$this->client->call('method');
}
public function testThrowsOnInvalidJsonPayload(): void
{
$this->client->setEndpoint('http://test');
$this->client->setAuth("\xB1\x31"); // force json_decode to fail
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Invalid JSON-RPC payload.');
$this->client->call('method');
}
public function testThrowsIfHttpNotSuccessful(): void
{
$this->client->setEndpoint('http://test');
$this->client->setAuth('auth');
$stream = $this->createMock(StreamInterface::class);
$stream->method('getContents')->willReturn('ERR');
$httpResponse = $this->createMock(HttpResponse::class);
$httpResponse->method('isSuccessful')->willReturn(false);
$httpResponse->method('getStatusCode')->willReturn(500);
$httpResponse->method('getBody')->willReturn($stream);
$this->http->method('post')->willReturn($httpResponse);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage("500: 'ERR'");
$this->client->call('test');
}
public function testThrowsIfNoBody(): void
{
$this->client->setEndpoint('http://test');
$httpResponse = $this->createMock(HttpResponse::class);
$httpResponse->method('isSuccessful')->willReturn(true);
$httpResponse->method('getBody')->willReturn(null);
$this->http->method('post')->willReturn($httpResponse);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('No JSON-RPC response received.');
$this->client->call('test');
}
public function testThrowsIfInvalidResponseObject(): void
{
$this->client->setEndpoint('http://test');
$stream = $this->createMock(StreamInterface::class);
$stream->method('getContents')->willReturn('{"ok":1}');
$httpResponse = $this->createMock(HttpResponse::class);
$httpResponse->method('isSuccessful')->willReturn(true);
$httpResponse->method('getBody')->willReturn($stream);
$this->http->method('post')->willReturn($httpResponse);
$this->serializer->method('deserialize')->willReturn(new \stdClass());
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Invalid JSON-RPC response.');
$this->client->call('test');
}
public function testSuccessfulCall(): void
{
$this->client->setEndpoint('http://test');
$this->client->setAuth('auth');
$stream = $this->createMock(StreamInterface::class);
$stream->method('getContents')->willReturn('{"result":123}');
$httpResponse = $this->createMock(HttpResponse::class);
$httpResponse->method('isSuccessful')->willReturn(true);
$httpResponse->method('getBody')->willReturn($stream);
$this->http->method('post')->willReturn($httpResponse);
$expected = $this->createMock(JsonRpcResponse::class);
$this->serializer
->method('deserialize')
->with('{"result":123}', JsonRpcResponse::class, 'json')
->willReturn($expected);
$out = $this->client->call('method', ['a' => 1]);
$this->assertSame($expected, $out);
}
}