Completed
Branch master (888209)
by Pavel
04:20
created

ClientTest::getResponses()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace Bankiru\Api\BrowserKit\Tests;
4
5
use Bankiru\Api\BrowserKit\JsonRpcClient;
6
use Bankiru\Api\BrowserKit\JsonRpcResponseCollection;
7
use Prophecy\Argument;
8
use ScayTrase\Api\JsonRpc\JsonRpcError;
9
use ScayTrase\Api\JsonRpc\JsonRpcNotification;
10
use ScayTrase\Api\JsonRpc\JsonRpcRequest;
11
use ScayTrase\Api\JsonRpc\RequestTransformation;
12
use Symfony\Component\BrowserKit\Client;
13
use Symfony\Component\BrowserKit\Request;
14
use Symfony\Component\BrowserKit\Response;
15
16
class ClientTest extends \PHPUnit_Framework_TestCase
17
{
18
    private $client;
19
20
    public function getResponses()
21
    {
22
        return [
23
            'combo' => [
24
                [
25
                    [
26
                        'jsonrpc' => '2.0',
27
                        'id'      => 1,
28
                        'result'  => null,
29
                    ],
30
                    //noop for 2
31
                    [
32
                        'jsonrpc' => '2.0',
33
                        'id'      => 3,
34
                        'error'   => [
35
                            'code'    => JsonRpcError::INVALID_PARAMS,
36
                            'message' => 'Invalid data received',
37
                            'data'    => [
38
                                'asdasd' => 'test',
39
                            ],
40
                        ],
41
                    ],
42
                ],
43
            ],
44
        ];
45
    }
46
47
    /**
48
     * @dataProvider getResponses
49
     *
50
     * @param $responses
51
     */
52
    public function testJsonRpcClient($responses)
53
    {
54
        $client = $this->createClient();
55
        $this->push(new Response(json_encode($responses, JSON_PRETTY_PRINT)));
56
57
        $jsonRpcClient = new JsonRpcClient($client, '/');
58
59
        $request1 = new JsonRpcRequest('/test', [], 1);
60
        $request2 = new JsonRpcNotification('/test', null);
61
        $request3 = new JsonRpcRequest('/test', ['asdasd' => 'test'], 3);
62
63
        $collection = $jsonRpcClient->invoke([$request1, $request2, $request3]);
64
65
        $response1  = $collection->getResponse($request1);
66
        $response2  = $collection->getResponse($request2);
67
        $response3  = $collection->getResponse($request3);
68
        self::assertTrue($response1->isSuccessful());
69
        self::assertTrue($response2->isSuccessful());
70
        self::assertFalse($response3->isSuccessful());
71
72
        self::assertCount(2, $collection); // notifications are not iterated
73
        foreach ($collection as $response) {
74
            self::assertContains($response, [$response1, $response3]);
75
        }
76
    }
77
78
    /**
79
     * @dataProvider getResponses
80
     *
81
     * @param $responses
82
     */
83
    public function testResponseFetching($responses)
84
    {
85
        $httpResponse      = new Response(json_encode($responses, JSON_PRETTY_PRINT));
86
        $transformations   = [];
87
        $request1          = new JsonRpcRequest('/test', [], 1);
88
        $request2          = new JsonRpcNotification('/test', null);
89
        $request3          = new JsonRpcRequest('/test', ['asdasd' => 'test'], 3);
90
        $transformations[] = new RequestTransformation($request1, $request1);
91
        $transformations[] = new RequestTransformation($request2, $request2);
92
        $transformations[] = new RequestTransformation($request3, $request3);
93
94
        $collection = new JsonRpcResponseCollection($httpResponse, $transformations);
95
        $response1  = $collection->getResponse($request1);
96
        $response2  = $collection->getResponse($request2);
97
        $response3  = $collection->getResponse($request3);
98
        self::assertTrue($response1->isSuccessful());
99
        self::assertTrue($response2->isSuccessful());
100
        self::assertFalse($response3->isSuccessful());
101
102
        self::assertCount(2, $collection); // notifications are not iterated
103
        foreach ($collection as $response) {
104
            self::assertContains($response, [$response1, $response3]);
105
        }
106
    }
107
108
    private function createClient()
109
    {
110
        if (!$this->client) {
111
            $this->client = $mock = $this->prophesize(Client::class);
0 ignored issues
show
Unused Code introduced by
$mock 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...
112
            $this->client->restart()->willReturn(null);
113
        }
114
115
        return $this->client->reveal();
116
    }
117
118
    private function push(Response $response)
119
    {
120
        $this->client->request(
121
            Argument::type('string'),
122
            Argument::type('string'),
123
            Argument::any(),
124
            Argument::any(),
125
            Argument::type('array'),
126
            Argument::type('string')
127
        )->will(
128
129
            function (array $arguments) use ($response) {
130
                list($method, $uri, $parameters, $files, $server, $content) = $arguments;
131
132
                $this->getRequest()->willReturn(
0 ignored issues
show
Bug introduced by
The method getRequest() does not seem to exist on object<Bankiru\Api\BrowserKit\Tests\ClientTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
                    new Request($uri, $method, $parameters, $files, [], $server, $content)
134
                );
135
                $this->getResponse()->willReturn($response);
0 ignored issues
show
Bug introduced by
The method getResponse() does not exist on Bankiru\Api\BrowserKit\Tests\ClientTest. Did you maybe mean getResponses()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
136
            }
137
        );
138
    }
139
}
140