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

RequestFunctionalTest::testBarAsyncRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 21
loc 21
rs 9.3142
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\Subscriber\ErrorSubscriber;
16
use Graze\GuzzleHttp\JsonRpc\Test\FunctionalTestCase;
17
18
class RequestFunctionalTest extends FunctionalTestCase
19
{
20
    public function setUp()
21
    {
22
        $this->client = $this->createClient();
0 ignored issues
show
Bug introduced by
The property client 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...
23
    }
24
25
    public function tearDown()
26
    {
27
        if (isset($this->promise)) {
28
            $this->promise->wait(false); // Stop PHPUnit closing before async assertions
0 ignored issues
show
Bug introduced by
The property promise 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...
29
            unset($this->promise);
30
        }
31
    }
32
33 View Code Duplication
    public function testConcatRequest()
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...
34
    {
35
        $id = 123;
36
        $method = 'concat';
37
        $params = ['foo'=>'abc', 'bar'=>'def'];
38
        $request = $this->client->request($id, $method, $params);
39
        $response = $this->client->send($request);
40
41
        $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
42
        $this->assertEquals($id, $request->getRpcId());
43
        $this->assertEquals($method, $request->getRpcMethod());
44
        $this->assertEquals($params, $request->getRpcParams());
45
46
        $this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion());
47
        $this->assertEquals(implode('', $params), $response->getRpcResult());
48
        $this->assertEquals($id, $response->getRpcId());
49
        $this->assertEquals(null, $response->getRpcErrorCode());
50
        $this->assertEquals(null, $response->getRpcErrorMessage());
51
        $this->assertEquals(null, $response->getRpcErrorData());
52
    }
53
54 View Code Duplication
    public function testConcatAsyncRequest()
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...
55
    {
56
        $id = 123;
57
        $method = 'concat';
58
        $params = ['foo'=>'abc', 'bar'=>'def'];
59
        $request = $this->client->request($id, $method, $params);
60
        $this->promise = $this->client->sendAsync($request);
61
62
        $this->promise->then(function ($response) use ($request, $id, $method, $params) {
63
            $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
64
            $this->assertEquals($id, $request->getRpcId());
65
            $this->assertEquals($method, $request->getRpcMethod());
66
            $this->assertEquals($params, $request->getRpcParams());
67
68
            $this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion());
69
            $this->assertEquals(implode('', $params), $response->getRpcResult());
70
            $this->assertEquals($id, $response->getRpcId());
71
            $this->assertEquals(null, $response->getRpcErrorCode());
72
            $this->assertEquals(null, $response->getRpcErrorMessage());
73
            $this->assertEquals(null, $response->getRpcErrorData());
74
        });
75
    }
76
77 View Code Duplication
    public function testSumRequest()
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...
78
    {
79
        $id = 'abc';
80
        $method = 'sum';
81
        $params = ['foo'=>123, 'bar'=>456];
82
        $request = $this->client->request($id, $method, $params);
83
        $response = $this->client->send($request);
84
85
        $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
86
        $this->assertEquals($id, $request->getRpcId());
87
        $this->assertEquals($method, $request->getRpcMethod());
88
        $this->assertEquals($params, $request->getRpcParams());
89
90
        $this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion());
91
        $this->assertEquals(array_sum($params), $response->getRpcResult());
92
        $this->assertEquals($id, $response->getRpcId());
93
        $this->assertEquals(null, $response->getRpcErrorCode());
94
        $this->assertEquals(null, $response->getRpcErrorMessage());
95
        $this->assertEquals(null, $response->getRpcErrorData());
96
    }
97
98 View Code Duplication
    public function testSumAsyncRequest()
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...
99
    {
100
        $id = 'abc';
101
        $method = 'sum';
102
        $params = ['foo'=>123, 'bar'=>456];
103
        $request = $this->client->request($id, $method, $params);
104
        $this->promise = $this->client->sendAsync($request);
105
106
        $this->promise->then(function ($response) use ($request, $id, $method, $params) {
107
            $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
108
            $this->assertEquals($id, $request->getRpcId());
109
            $this->assertEquals($method, $request->getRpcMethod());
110
            $this->assertEquals($params, $request->getRpcParams());
111
112
            $this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion());
113
            $this->assertEquals(array_sum($params), $response->getRpcResult());
114
            $this->assertEquals($id, $response->getRpcId());
115
            $this->assertEquals(null, $response->getRpcErrorCode());
116
            $this->assertEquals(null, $response->getRpcErrorMessage());
117
            $this->assertEquals(null, $response->getRpcErrorData());
118
        });
119
    }
120
121 View Code Duplication
    public function testFooRequest()
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...
122
    {
123
        $id = 'abc';
124
        $method = 'foo';
125
        $request = $this->client->request($id, $method, []);
126
        $response = $this->client->send($request);
127
128
        $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
129
        $this->assertEquals($id, $request->getRpcId());
130
        $this->assertEquals($method, $request->getRpcMethod());
131
        $this->assertEquals(null, $request->getRpcParams());
132
133
        $this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion());
134
        $this->assertEquals('foo', $response->getRpcResult());
135
        $this->assertEquals($id, $response->getRpcId());
136
        $this->assertEquals(null, $response->getRpcErrorCode());
137
        $this->assertEquals(null, $response->getRpcErrorMessage());
138
        $this->assertEquals(null, $response->getRpcErrorData());
139
    }
140
141 View Code Duplication
    public function testFooAsyncRequest()
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...
142
    {
143
        $id = 'abc';
144
        $method = 'foo';
145
        $request = $this->client->request($id, $method, []);
146
        $this->promise = $this->client->sendAsync($request);
147
148
        $this->promise->then(function ($response) use ($request, $id, $method) {
149
            $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
150
            $this->assertEquals($id, $request->getRpcId());
151
            $this->assertEquals($method, $request->getRpcMethod());
152
            $this->assertEquals(null, $request->getRpcParams());
153
154
            $this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion());
155
            $this->assertEquals('foo', $response->getRpcResult());
156
            $this->assertEquals($id, $response->getRpcId());
157
            $this->assertEquals(null, $response->getRpcErrorCode());
158
            $this->assertEquals(null, $response->getRpcErrorMessage());
159
            $this->assertEquals(null, $response->getRpcErrorData());
160
        });
161
    }
162
163 View Code Duplication
    public function testBarRequest()
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...
164
    {
165
        $id = 'abc';
166
        $method = 'bar';
167
        $request = $this->client->request($id, $method, []);
168
        $response = $this->client->send($request);
169
170
        $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
171
        $this->assertEquals($id, $request->getRpcId());
172
        $this->assertEquals($method, $request->getRpcMethod());
173
        $this->assertEquals(null, $request->getRpcParams());
174
175
        $this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion());
176
        $this->assertEquals(null, $response->getRpcResult());
177
        $this->assertEquals($id, $response->getRpcId());
178
        $this->assertTrue(is_int($response->getRpcErrorCode()));
179
        $this->assertTrue(is_string($response->getRpcErrorMessage()));
180
        $this->assertEquals(null, $response->getRpcErrorData());
181
    }
182
183 View Code Duplication
    public function testBarAsyncRequest()
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...
184
    {
185
        $id = 'abc';
186
        $method = 'bar';
187
        $request = $this->client->request($id, $method, []);
188
        $this->promise = $this->client->sendAsync($request);
189
190
        $this->promise->then(function ($response) use ($request, $id, $method) {
191
            $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
192
            $this->assertEquals($id, $request->getRpcId());
193
            $this->assertEquals($method, $request->getRpcMethod());
194
            $this->assertEquals(null, $request->getRpcParams());
195
196
            $this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion());
197
            $this->assertEquals(null, $response->getRpcResult());
198
            $this->assertEquals($id, $response->getRpcId());
199
            $this->assertTrue(is_int($response->getRpcErrorCode()));
200
            $this->assertTrue(is_string($response->getRpcErrorMessage()));
201
            $this->assertEquals(null, $response->getRpcErrorData());
202
        });
203
    }
204
205
    public function testBarRequestThrows()
206
    {
207
        $id = 'abc';
208
        $method = 'bar';
209
        $client = $this->createClient(null, ['rpc_error' => true]);
210
        $request = $client->request($id, $method, []);
211
212
        $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
213
        $this->assertEquals($id, $request->getRpcId());
214
        $this->assertEquals($method, $request->getRpcMethod());
215
        $this->assertEquals(null, $request->getRpcParams());
216
217
        $this->setExpectedException('Graze\GuzzleHttp\JsonRpc\Exception\ClientException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
218
        $response = $client->send($request);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
219
    }
220
221
    public function testBarAsyncRequestIsRejected()
222
    {
223
        $id = 'abc';
224
        $method = 'bar';
225
        $client = $this->createClient(null, ['rpc_error' => true]);
226
        $request = $client->request($id, $method, []);
227
        $this->promise = $client->sendAsync($request);
228
229
        $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
230
        $this->assertEquals($id, $request->getRpcId());
231
        $this->assertEquals($method, $request->getRpcMethod());
232
        $this->assertEquals(null, $request->getRpcParams());
233
234
        $this->promise->then(function ($response) use ($request, $id, $method) {
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
235
            $this->fail('This promise should not be fulfilled');
236
        }, function ($reason) {
237
            $this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\Exception\ClientException', $reason);
238
        });
239
    }
240
}
241