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

BatchFunctionalTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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\Test\FunctionalTestCase;
16
17
class BatchFunctionalTest extends FunctionalTestCase
18
{
19
    public function setUp()
20
    {
21
        $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...
22
    }
23
24
    public function tearDown()
25
    {
26
        if (isset($this->promise)) {
27
            $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...
28
            unset($this->promise);
29
        }
30
    }
31
32
    public function testBatchRequestWithOneChild()
33
    {
34
        $id = 'abc';
35
        $method = 'sum';
36
        $params = ['foo'=>123, 'bar'=>456];
37
        $request = $this->client->request($id, $method, $params);
38
        $responses = $this->client->sendAll([$request]);
39
40
        $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
41
        $this->assertEquals($id, $request->getRpcId());
42
        $this->assertEquals($method, $request->getRpcMethod());
43
        $this->assertEquals($params, $request->getRpcParams());
44
45
        $this->assertTrue(is_array($responses));
46
        $this->assertEquals(ClientInterface::SPEC, $responses[0]->getRpcVersion());
47
        $this->assertEquals(array_sum($params), $responses[0]->getRpcResult());
48
        $this->assertEquals($id, $responses[0]->getRpcId());
49
        $this->assertEquals(null, $responses[0]->getRpcErrorCode());
50
        $this->assertEquals(null, $responses[0]->getRpcErrorMessage());
51
    }
52
53
    public function testAsyncBatchRequestWithOneChild()
54
    {
55
        $id = 'abc';
56
        $method = 'sum';
57
        $params = ['foo'=>123, 'bar'=>456];
58
        $request = $this->client->request($id, $method, $params);
59
        $this->promise = $this->client->sendAllAsync([$request]);
60
61
        $this->promise->then(function ($response) use ($request, $id, $method, $params) {
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...
62
            $this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion());
63
            $this->assertEquals($id, $request->getRpcId());
64
            $this->assertEquals($method, $request->getRpcMethod());
65
            $this->assertEquals($params, $request->getRpcParams());
66
67
            $this->assertTrue(is_array($responses));
0 ignored issues
show
Bug introduced by
The variable $responses does not exist. Did you mean $response?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
68
            $this->assertEquals(ClientInterface::SPEC, $responses[0]->getRpcVersion());
0 ignored issues
show
Bug introduced by
The variable $responses does not exist. Did you mean $response?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
69
            $this->assertEquals(array_sum($params), $responses[0]->getRpcResult());
0 ignored issues
show
Bug introduced by
The variable $responses does not exist. Did you mean $response?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
70
            $this->assertEquals($id, $responses[0]->getRpcId());
0 ignored issues
show
Bug introduced by
The variable $responses does not exist. Did you mean $response?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
71
            $this->assertEquals(null, $responses[0]->getRpcErrorCode());
0 ignored issues
show
Bug introduced by
The variable $responses does not exist. Did you mean $response?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
72
            $this->assertEquals(null, $responses[0]->getRpcErrorMessage());
0 ignored issues
show
Bug introduced by
The variable $responses does not exist. Did you mean $response?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
73
        });
74
    }
75
76
    public function testBatchRequestWithMultipleChildren()
77
    {
78
        $idA = 123;
79
        $idC = 'abc';
80
        $idD = 'def';
81
        $methodA = 'concat';
82
        $methodB = 'nofify';
83
        $methodC = 'sum';
84
        $methodD = 'bar';
85
        $paramsA = ['foo'=>'abc', 'bar'=>'def'];
86
        $paramsB = ['foo'=>false];
87
        $paramsC = ['foo'=>123, 'bar'=>456];
88
        $paramsD = ['foo'=>123, 'bar'=>456];
89
        $requestA = $this->client->request($idA, $methodA, $paramsA);
90
        $requestB = $this->client->notification($methodB, $paramsB);
91
        $requestC = $this->client->request($idC, $methodC, $paramsC);
92
        $requestD = $this->client->request($idD, $methodD, $paramsD);
93
        $responses = $this->client->sendAll([$requestA, $requestB, $requestC, $requestD]);
94
95
        $this->assertEquals(ClientInterface::SPEC, $requestA->getRpcVersion());
96
        $this->assertEquals($idA, $requestA->getRpcId());
97
        $this->assertEquals($methodA, $requestA->getRpcMethod());
98
        $this->assertEquals($paramsA, $requestA->getRpcParams());
99
        $this->assertEquals(ClientInterface::SPEC, $requestB->getRpcVersion());
100
        $this->assertEquals(null, $requestB->getRpcId());
101
        $this->assertEquals($methodB, $requestB->getRpcMethod());
102
        $this->assertEquals($paramsB, $requestB->getRpcParams());
103
        $this->assertEquals(ClientInterface::SPEC, $requestC->getRpcVersion());
104
        $this->assertEquals($idC, $requestC->getRpcId());
105
        $this->assertEquals($methodC, $requestC->getRpcMethod());
106
        $this->assertEquals($paramsC, $requestC->getRpcParams());
107
        $this->assertEquals(ClientInterface::SPEC, $requestD->getRpcVersion());
108
        $this->assertEquals($idD, $requestD->getRpcId());
109
        $this->assertEquals($methodD, $requestD->getRpcMethod());
110
        $this->assertEquals($paramsD, $requestD->getRpcParams());
111
112
        $this->assertTrue(is_array($responses));
113
        $this->assertEquals(3, count($responses));
114
115 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...
116
            if ($response->getRpcId() === $idA) {
117
                $responseA = $response;
118
            } elseif ($response->getRpcId() === $idC) {
119
                $responseC = $response;
120
            } elseif ($response->getRpcId() === $idD) {
121
                $responseD = $response;
122
            }
123
        }
124
        if (!isset($responseA) || !isset($responseC) || !isset($responseD)) {
125
            $this->fail('Invalid responses');
126
        }
127
128
        $this->assertEquals(ClientInterface::SPEC, $responseA->getRpcVersion());
0 ignored issues
show
Bug introduced by
The variable $responseA does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
129
        $this->assertEquals(implode('', $paramsA), $responseA->getRpcResult());
130
        $this->assertEquals($idA, $responseA->getRpcId());
131
        $this->assertEquals(null, $responseA->getRpcErrorCode());
132
        $this->assertEquals(null, $responseA->getRpcErrorMessage());
133
        $this->assertEquals(ClientInterface::SPEC, $responseC->getRpcVersion());
0 ignored issues
show
Bug introduced by
The variable $responseC does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
134
        $this->assertEquals(array_sum($paramsC), $responseC->getRpcResult());
135
        $this->assertEquals($idC, $responseC->getRpcId());
136
        $this->assertEquals(null, $responseC->getRpcErrorCode());
137
        $this->assertEquals(null, $responseC->getRpcErrorMessage());
138
        $this->assertEquals(ClientInterface::SPEC, $responseD->getRpcVersion());
0 ignored issues
show
Bug introduced by
The variable $responseD does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
139
        $this->assertEquals(null, $responseD->getRpcResult());
140
        $this->assertEquals($idD, $responseD->getRpcId());
141
        $this->assertTrue(is_int($responseD->getRpcErrorCode()));
142
        $this->assertTrue(is_string($responseD->getRpcErrorMessage()));
143
    }
144
145
    public function testAsyncBatchRequestWithMultipleChildren()
146
    {
147
        $idA = 123;
148
        $idC = 'abc';
149
        $idD = 'def';
150
        $methodA = 'concat';
151
        $methodB = 'nofify';
152
        $methodC = 'sum';
153
        $methodD = 'bar';
154
        $paramsA = ['foo'=>'abc', 'bar'=>'def'];
155
        $paramsB = ['foo'=>false];
156
        $paramsC = ['foo'=>123, 'bar'=>456];
157
        $paramsD = ['foo'=>123, 'bar'=>456];
158
        $requestA = $this->client->request($idA, $methodA, $paramsA);
159
        $requestB = $this->client->notification($methodB, $paramsB);
160
        $requestC = $this->client->request($idC, $methodC, $paramsC);
161
        $requestD = $this->client->request($idD, $methodD, $paramsD);
162
        $this->promise = $this->client->sendAllAsync([$requestA, $requestB, $requestC, $requestD]);
163
164
        $this->promise->then(function ($responses) use ($requestA, $requestB, $requestC, $requestD, $idA, $idC, $idD, $methodA, $methodB, $methodC, $methodD, $paramsA, $paramsB, $paramsC, $paramsD) {
165
            $this->assertEquals(ClientInterface::SPEC, $requestA->getRpcVersion());
166
            $this->assertEquals($idA, $requestA->getRpcId());
167
            $this->assertEquals($methodA, $requestA->getRpcMethod());
168
            $this->assertEquals($paramsA, $requestA->getRpcParams());
169
            $this->assertEquals(ClientInterface::SPEC, $requestB->getRpcVersion());
170
            $this->assertEquals(null, $requestB->getRpcId());
171
            $this->assertEquals($methodB, $requestB->getRpcMethod());
172
            $this->assertEquals($paramsB, $requestB->getRpcParams());
173
            $this->assertEquals(ClientInterface::SPEC, $requestC->getRpcVersion());
174
            $this->assertEquals($idC, $requestC->getRpcId());
175
            $this->assertEquals($methodC, $requestC->getRpcMethod());
176
            $this->assertEquals($paramsC, $requestC->getRpcParams());
177
            $this->assertEquals(ClientInterface::SPEC, $requestD->getRpcVersion());
178
            $this->assertEquals($idD, $requestD->getRpcId());
179
            $this->assertEquals($methodD, $requestD->getRpcMethod());
180
            $this->assertEquals($paramsD, $requestD->getRpcParams());
181
182
            $this->assertTrue(is_array($responses));
183
            $this->assertEquals(3, count($responses));
184
185 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...
186
                if ($response->getRpcId() === $idA) {
187
                    $responseA = $response;
188
                } elseif ($response->getRpcId() === $idC) {
189
                    $responseC = $response;
190
                } elseif ($response->getRpcId() === $idD) {
191
                    $responseD = $response;
192
                }
193
            }
194
            if (!isset($responseA) || !isset($responseC) || !isset($responseD)) {
195
                $this->fail('Invalid responses');
196
            }
197
198
            $this->assertEquals(ClientInterface::SPEC, $responseA->getRpcVersion());
0 ignored issues
show
Bug introduced by
The variable $responseA does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
199
            $this->assertEquals(implode('', $paramsA), $responseA->getRpcResult());
200
            $this->assertEquals($idA, $responseA->getRpcId());
201
            $this->assertEquals(null, $responseA->getRpcErrorCode());
202
            $this->assertEquals(null, $responseA->getRpcErrorMessage());
203
            $this->assertEquals(ClientInterface::SPEC, $responseC->getRpcVersion());
0 ignored issues
show
Bug introduced by
The variable $responseC does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
204
            $this->assertEquals(array_sum($paramsC), $responseC->getRpcResult());
205
            $this->assertEquals($idC, $responseC->getRpcId());
206
            $this->assertEquals(null, $responseC->getRpcErrorCode());
207
            $this->assertEquals(null, $responseC->getRpcErrorMessage());
208
            $this->assertEquals(ClientInterface::SPEC, $responseD->getRpcVersion());
0 ignored issues
show
Bug introduced by
The variable $responseD does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
209
            $this->assertEquals(null, $responseD->getRpcResult());
210
            $this->assertEquals($idD, $responseD->getRpcId());
211
            $this->assertTrue(is_int($responseD->getRpcErrorCode()));
212
            $this->assertTrue(is_string($responseD->getRpcErrorMessage()));
213
        });
214
    }
215
}
216