Completed
Pull Request — master (#30)
by Harry
24:34
created

testAsyncBatchRequestWithMultipleChildren()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 71
Code Lines 62

Duplication

Lines 9
Ratio 12.68 %

Importance

Changes 0
Metric Value
cc 8
eloc 62
nc 1
nop 0
dl 9
loc 71
rs 6.4391
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Test\FunctionalTestCase;
16
use GuzzleHttp\Promise\PromiseInterface;
17
18
class BatchFunctionalTest extends FunctionalTestCase
19
{
20
    /** @var Client */
21
    private $client;
22
23
    public function setUp()
24
    {
25
        $this->client = $this->createClient();
26
    }
27
28
    public function testBatchRequestWithOneChild()
29
    {
30
        $id = 'abc';
31
        $method = 'sum';
32
        $params = ['foo'=>123, 'bar'=>456];
33
        $request = $this->client->request($id, $method, $params);
34
        $responses = $this->client->sendAll([$request]);
35
36
        $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
37
        $this->assertEquals($id, $request->getRpcId());
38
        $this->assertEquals($method, $request->getRpcMethod());
39
        $this->assertEquals($params, $request->getRpcParams());
40
41
        $this->assertTrue(is_array($responses));
42
        $this->assertEquals(ClientInterface::SPEC, $responses[0]->getRpcVersion());
43
        $this->assertEquals(array_sum($params), $responses[0]->getRpcResult());
44
        $this->assertEquals($id, $responses[0]->getRpcId());
45
        $this->assertEquals(null, $responses[0]->getRpcErrorCode());
46
        $this->assertEquals(null, $responses[0]->getRpcErrorMessage());
47
    }
48
49
    public function testAsyncBatchRequestWithOneChild()
50
    {
51
        $id = 'abc';
52
        $method = 'sum';
53
        $params = ['foo' => 123, 'bar' => 456];
54
        $request = $this->client->request($id, $method, $params);
55
        $promise = $this->client->sendAllAsync([$request]);
56
57
        $promise->then(function ($responses) use ($request, $id, $method, $params) {
58
            $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
59
            $this->assertEquals($id, $request->getRpcId());
60
            $this->assertEquals($method, $request->getRpcMethod());
61
            $this->assertEquals($params, $request->getRpcParams());
62
63
            $this->assertTrue(is_array($responses));
64
            $this->assertEquals(ClientInterface::SPEC, $responses[0]->getRpcVersion());
65
            $this->assertEquals(array_sum($params), $responses[0]->getRpcResult());
66
            $this->assertEquals($id, $responses[0]->getRpcId());
67
            $this->assertEquals(null, $responses[0]->getRpcErrorCode());
68
            $this->assertEquals(null, $responses[0]->getRpcErrorMessage());
69
        })->wait();
70
    }
71
72
    public function testBatchRequestWithMultipleChildren()
73
    {
74
        $idA = 123;
75
        $idC = 'abc';
76
        $idD = 'def';
77
        $methodA = 'concat';
78
        $methodB = 'nofify';
79
        $methodC = 'sum';
80
        $methodD = 'bar';
81
        $paramsA = ['foo'=>'abc', 'bar'=>'def'];
82
        $paramsB = ['foo'=>false];
83
        $paramsC = ['foo'=>123, 'bar'=>456];
84
        $paramsD = ['foo'=>123, 'bar'=>456];
85
        $requestA = $this->client->request($idA, $methodA, $paramsA);
86
        $requestB = $this->client->notification($methodB, $paramsB);
87
        $requestC = $this->client->request($idC, $methodC, $paramsC);
88
        $requestD = $this->client->request($idD, $methodD, $paramsD);
89
        $responses = $this->client->sendAll([$requestA, $requestB, $requestC, $requestD]);
90
91
        $this->assertEquals(ClientInterface::SPEC, $requestA->getRpcVersion());
92
        $this->assertEquals($idA, $requestA->getRpcId());
93
        $this->assertEquals($methodA, $requestA->getRpcMethod());
94
        $this->assertEquals($paramsA, $requestA->getRpcParams());
95
        $this->assertEquals(ClientInterface::SPEC, $requestB->getRpcVersion());
96
        $this->assertEquals(null, $requestB->getRpcId());
97
        $this->assertEquals($methodB, $requestB->getRpcMethod());
98
        $this->assertEquals($paramsB, $requestB->getRpcParams());
99
        $this->assertEquals(ClientInterface::SPEC, $requestC->getRpcVersion());
100
        $this->assertEquals($idC, $requestC->getRpcId());
101
        $this->assertEquals($methodC, $requestC->getRpcMethod());
102
        $this->assertEquals($paramsC, $requestC->getRpcParams());
103
        $this->assertEquals(ClientInterface::SPEC, $requestD->getRpcVersion());
104
        $this->assertEquals($idD, $requestD->getRpcId());
105
        $this->assertEquals($methodD, $requestD->getRpcMethod());
106
        $this->assertEquals($paramsD, $requestD->getRpcParams());
107
108
        $this->assertTrue(is_array($responses));
109
        $this->assertEquals(3, count($responses));
110
111
        $responseA = $responseC = $responseD = null;
112 View Code Duplication
        foreach ($responses as $response) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
113
            if ($response->getRpcId() === $idA) {
114
                $responseA = $response;
115
            } elseif ($response->getRpcId() === $idC) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $response->getRpcId() (integer) and $idC (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
116
                $responseC = $response;
117
            } elseif ($response->getRpcId() === $idD) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $response->getRpcId() (integer) and $idD (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
118
                $responseD = $response;
119
            }
120
        }
121
        if (is_null($responseA) || is_null($responseC) || is_null($responseD)) {
122
            $this->fail('Invalid responses');
123
        }
124
125
        $this->assertEquals(ClientInterface::SPEC, $responseA->getRpcVersion());
126
        $this->assertEquals(implode('', $paramsA), $responseA->getRpcResult());
127
        $this->assertEquals($idA, $responseA->getRpcId());
128
        $this->assertEquals(null, $responseA->getRpcErrorCode());
129
        $this->assertEquals(null, $responseA->getRpcErrorMessage());
130
        $this->assertEquals(ClientInterface::SPEC, $responseC->getRpcVersion());
131
        $this->assertEquals(array_sum($paramsC), $responseC->getRpcResult());
132
        $this->assertEquals($idC, $responseC->getRpcId());
133
        $this->assertEquals(null, $responseC->getRpcErrorCode());
134
        $this->assertEquals(null, $responseC->getRpcErrorMessage());
135
        $this->assertEquals(ClientInterface::SPEC, $responseD->getRpcVersion());
136
        $this->assertEquals(null, $responseD->getRpcResult());
137
        $this->assertEquals($idD, $responseD->getRpcId());
138
        $this->assertTrue(is_int($responseD->getRpcErrorCode()));
139
        $this->assertTrue(is_string($responseD->getRpcErrorMessage()));
140
    }
141
142
    public function testAsyncBatchRequestWithMultipleChildren()
143
    {
144
        $idA = 123;
145
        $idC = 'abc';
146
        $idD = 'def';
147
        $methodA = 'concat';
148
        $methodB = 'nofify';
149
        $methodC = 'sum';
150
        $methodD = 'bar';
151
        $paramsA = ['foo'=>'abc', 'bar'=>'def'];
152
        $paramsB = ['foo'=>false];
153
        $paramsC = ['foo'=>123, 'bar'=>456];
154
        $paramsD = ['foo'=>123, 'bar'=>456];
155
        $requestA = $this->client->request($idA, $methodA, $paramsA);
156
        $requestB = $this->client->notification($methodB, $paramsB);
157
        $requestC = $this->client->request($idC, $methodC, $paramsC);
158
        $requestD = $this->client->request($idD, $methodD, $paramsD);
159
        $promise = $this->client->sendAllAsync([$requestA, $requestB, $requestC, $requestD]);
160
161
        $promise->then(function ($responses) use ($requestA, $requestB, $requestC, $requestD, $idA, $idC, $idD, $methodA, $methodB, $methodC, $methodD, $paramsA, $paramsB, $paramsC, $paramsD) {
162
            $this->assertEquals(ClientInterface::SPEC, $requestA->getRpcVersion());
163
            $this->assertEquals($idA, $requestA->getRpcId());
164
            $this->assertEquals($methodA, $requestA->getRpcMethod());
165
            $this->assertEquals($paramsA, $requestA->getRpcParams());
166
            $this->assertEquals(ClientInterface::SPEC, $requestB->getRpcVersion());
167
            $this->assertEquals(null, $requestB->getRpcId());
168
            $this->assertEquals($methodB, $requestB->getRpcMethod());
169
            $this->assertEquals($paramsB, $requestB->getRpcParams());
170
            $this->assertEquals(ClientInterface::SPEC, $requestC->getRpcVersion());
171
            $this->assertEquals($idC, $requestC->getRpcId());
172
            $this->assertEquals($methodC, $requestC->getRpcMethod());
173
            $this->assertEquals($paramsC, $requestC->getRpcParams());
174
            $this->assertEquals(ClientInterface::SPEC, $requestD->getRpcVersion());
175
            $this->assertEquals($idD, $requestD->getRpcId());
176
            $this->assertEquals($methodD, $requestD->getRpcMethod());
177
            $this->assertEquals($paramsD, $requestD->getRpcParams());
178
179
            $this->assertTrue(is_array($responses));
180
            $this->assertEquals(3, count($responses));
181
182
            $responseA = $responseC = $responseD = null;
183 View Code Duplication
            foreach ($responses as $response) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
                if ($response->getRpcId() === $idA) {
185
                    $responseA = $response;
186
                } elseif ($response->getRpcId() === $idC) {
187
                    $responseC = $response;
188
                } elseif ($response->getRpcId() === $idD) {
189
                    $responseD = $response;
190
                }
191
            }
192
            if (is_null($responseA) || is_null($responseC) || is_null($responseD)) {
193
                $this->fail('Invalid responses');
194
            }
195
196
            $this->assertEquals(ClientInterface::SPEC, $responseA->getRpcVersion());
197
            $this->assertEquals(implode('', $paramsA), $responseA->getRpcResult());
198
            $this->assertEquals($idA, $responseA->getRpcId());
199
            $this->assertEquals(null, $responseA->getRpcErrorCode());
200
            $this->assertEquals(null, $responseA->getRpcErrorMessage());
201
            $this->assertEquals(ClientInterface::SPEC, $responseC->getRpcVersion());
202
            $this->assertEquals(array_sum($paramsC), $responseC->getRpcResult());
203
            $this->assertEquals($idC, $responseC->getRpcId());
204
            $this->assertEquals(null, $responseC->getRpcErrorCode());
205
            $this->assertEquals(null, $responseC->getRpcErrorMessage());
206
            $this->assertEquals(ClientInterface::SPEC, $responseD->getRpcVersion());
207
            $this->assertEquals(null, $responseD->getRpcResult());
208
            $this->assertEquals($idD, $responseD->getRpcId());
209
            $this->assertTrue(is_int($responseD->getRpcErrorCode()));
210
            $this->assertTrue(is_string($responseD->getRpcErrorMessage()));
211
        })->wait();
212
    }
213
}
214