Completed
Pull Request — master (#30)
by Harry
02:53 queued 01:19
created

ClientTest::testSendAllAsync()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 36
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of Guzzle HTTP JSON-RPC
4
 *
5
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @see  http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE
11
 * @link http://github.com/graze/guzzle-jsonrpc
12
 */
13
namespace Graze\GuzzleHttp\JsonRpc;
14
15
use Graze\GuzzleHttp\JsonRpc\Message\RequestInterface;
16
use Graze\GuzzleHttp\JsonRpc\Test\UnitTestCase;
17
use Mockery;
18
19
class ClientTest extends UnitTestCase
20
{
21
    public function setup()
22
    {
23
        $this->httpClient = $this->mockHttpClient();
0 ignored issues
show
Bug introduced by
The property httpClient does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        $this->httpHandler = $this->mockHttpHandler();
0 ignored issues
show
Bug introduced by
The property httpHandler does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
        $this->messageFactory = $this->mockMessageFactory();
0 ignored issues
show
Bug introduced by
The property messageFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
27
        $this->httpClient->shouldReceive('getConfig')->once()->with('handler')->andReturn($this->httpHandler);
28
        $this->httpHandler->shouldReceive('push')->times(4);
29
30
        $this->client = new Client($this->httpClient, $this->messageFactory);
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
31
    }
32
33
    public function testInterface()
34
    {
35
        $this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\ClientInterface', $this->client);
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
36
    }
37
38
    public function testStaticFactory()
39
    {
40
        $this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\ClientInterface', Client::factory('http://foo'));
41
    }
42
43
    public function testNotification()
44
    {
45
        $request = $this->mockRequest();
46
        $jsonrpc = ['jsonrpc'=>ClientInterface::SPEC, 'method'=>'foo'];
47
        $type = RequestInterface::NOTIFICATION;
48
        $uri = 'http://foo';
49
50
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
51
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
52
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request);
53
54
        $this->assertSame($request, $this->client->notification('foo'));
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
55
    }
56
57
    public function testNotificationWithParams()
58
    {
59
        $request = $this->mockRequest();
60
        $jsonrpc = ['jsonrpc'=>ClientInterface::SPEC, 'method'=>'foo', 'params'=>['bar'=>true]];
61
        $type = RequestInterface::NOTIFICATION;
62
        $uri = 'http://foo';
63
64
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
65
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
66
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request);
67
68
        $this->assertSame($request, $this->client->notification('foo', ['bar'=>true]));
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
69
    }
70
71 View Code Duplication
    public function testRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
72
    {
73
        $request = $this->mockRequest();
74
        $jsonrpc = ['jsonrpc'=>ClientInterface::SPEC, 'method'=>'foo', 'id'=>123];
75
        $type = RequestInterface::REQUEST;
76
        $uri = 'http://foo';
77
78
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
79
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
80
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request);
81
82
        $this->assertSame($request, $this->client->request(123, 'foo'));
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
83
    }
84
85
    public function testRequestWithParams()
86
    {
87
        $request = $this->mockRequest();
88
        $jsonrpc = ['jsonrpc'=>ClientInterface::SPEC, 'method'=>'foo', 'params'=>['bar'=>true], 'id'=>123];
89
        $type = RequestInterface::REQUEST;
90
        $uri = 'http://foo';
91
92
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
93
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
94
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request);
95
96
        $this->assertSame($request, $this->client->request(123, 'foo', ['bar'=>true]));
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
97
    }
98
99 View Code Duplication
    public function testRequestWithEmptyParams()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
100
    {
101
        $request = $this->mockRequest();
102
        $jsonrpc = ['jsonrpc'=>ClientInterface::SPEC, 'method'=>'foo', 'id'=>123];
103
        $type = RequestInterface::REQUEST;
104
        $uri = 'http://foo';
105
106
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
107
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
108
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request);
109
110
        $this->assertSame($request, $this->client->request(123, 'foo', []));
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
111
    }
112
113
    public function testSendNotification()
114
    {
115
        $request = $this->mockRequest();
116
        $response = $this->mockResponse();
117
        $promise = $this->mockPromise();
118
119
        $request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn(null);
120
        $this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise);
121
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) {
122
            return null === $args($response);
123
        }))->andReturn($promise);
124
        $promise->shouldReceive('wait')->once()->withNoArgs()->andReturn(null);
125
126
        $this->assertNull($this->client->send($request));
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
127
    }
128
129
    public function testSendNotificationAsync()
130
    {
131
        $request = $this->mockRequest();
132
        $response = $this->mockResponse();
133
        $promise = $this->mockPromise();
134
135
        $request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn(null);
136
        $this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise);
137
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) {
138
            return null === $args($response);
139
        }))->andReturn($promise);
140
141
        $this->assertSame($promise, $this->client->sendAsync($request));
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
142
    }
143
144
    public function testSendRequest()
145
    {
146
        $request = $this->mockRequest();
147
        $response = $this->mockResponse();
148
        $promise = $this->mockPromise();
149
150
        $request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn('foo');
151
        $this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise);
152
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) {
153
            return $response === $args($response);
154
        }))->andReturn($promise);
155
        $promise->shouldReceive('wait')->once()->withNoArgs()->andReturn($response);
156
157
        $this->assertSame($response, $this->client->send($request));
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
158
    }
159
160
    public function testSendRequestAsync()
161
    {
162
        $request = $this->mockRequest();
163
        $response = $this->mockResponse();
164
        $promise = $this->mockPromise();
165
166
        $request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn('foo');
167
        $this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise);
168
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) {
169
            return $response === $args($response);
170
        }))->andReturn($promise);
171
172
        $this->assertSame($promise, $this->client->sendAsync($request));
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
173
    }
174
175
    public function testSendAll()
176
    {
177
        $promise = $this->mockPromise();
178
        $batchRequest = $this->mockRequest();
179
        $requestA = $this->mockRequest();
180
        $requestB = $this->mockRequest();
181
        $batchResponse = $this->mockResponse();
182
        $responseA = $this->mockResponse();
183
        $responseB = $this->mockResponse();
184
185
        $factory = $this->mockMessageFactory();
186
        $this->httpClient->messageFactory = $factory;
187
188
        $requestA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["foo"]');
189
        $requestB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["bar"]');
190
191
        $type = RequestInterface::BATCH;
192
        $uri = 'http://foo';
193
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], [['foo'], ['bar']])->andReturn($batchRequest);
194
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
195
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
196
        $this->httpClient->shouldReceive('sendAsync')->once()->with($batchRequest)->andReturn($promise);
197
198
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($batchResponse, $responseA, $responseB) {
199
            return [$responseA, $responseB] === $args($batchResponse);
200
        }))->andReturn($promise);
201
        $promise->shouldReceive('wait')->once()->withNoArgs()->andReturn([$responseA, $responseB]);
202
203
        $batchResponse->shouldReceive('getBody')->once()->withNoArgs()->andReturn('[["foo"], ["bar"]]');
204
        $batchResponse->shouldReceive('getStatusCode')->times(2)->withNoArgs()->andReturn(200);
205
        $batchResponse->shouldReceive('getHeaders')->times(2)->withNoArgs()->andReturn(['headers']);
206
207
        $this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['foo'])->andReturn($responseA);
208
        $this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['bar'])->andReturn($responseB);
209
210
        $this->assertSame([$responseA, $responseB], $this->client->sendAll([$requestA, $requestB]));
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
211
    }
212
213
    public function testSendAllAsync()
214
    {
215
        $promise = $this->mockPromise();
216
        $batchRequest = $this->mockRequest();
217
        $requestA = $this->mockRequest();
218
        $requestB = $this->mockRequest();
219
        $batchResponse = $this->mockResponse();
220
        $responseA = $this->mockResponse();
221
        $responseB = $this->mockResponse();
222
223
        $factory = $this->mockMessageFactory();
224
        $this->httpClient->messageFactory = $factory;
225
226
        $requestA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["foo"]');
227
        $requestB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["bar"]');
228
229
        $type = RequestInterface::BATCH;
230
        $uri = 'http://foo';
231
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], [['foo'], ['bar']])->andReturn($batchRequest);
232
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
233
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
234
        $this->httpClient->shouldReceive('sendAsync')->once()->with($batchRequest)->andReturn($promise);
235
236
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($batchResponse, $responseA, $responseB) {
237
            return [$responseA, $responseB] === $args($batchResponse);
238
        }))->andReturn($promise);
239
240
        $batchResponse->shouldReceive('getBody')->once()->withNoArgs()->andReturn('[["foo"], ["bar"]]');
241
        $batchResponse->shouldReceive('getStatusCode')->times(2)->withNoArgs()->andReturn(200);
242
        $batchResponse->shouldReceive('getHeaders')->times(2)->withNoArgs()->andReturn(['headers']);
243
244
        $this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['foo'])->andReturn($responseA);
245
        $this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['bar'])->andReturn($responseB);
246
247
        $this->assertSame($promise, $this->client->sendAllAsync([$requestA, $requestB]));
0 ignored issues
show
Bug introduced by
The property client does not seem to exist. Did you mean httpClient?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
248
    }
249
}
250