Completed
Push — master ( 1fa74b...2f70be )
by Pavel
10:37
created

testInvalidJsonRpcResultsIn400Response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Bankiru\Api\JsonRpc\Test\Tests;
4
5
use Bankiru\Api\JsonRpc\JsonRpcBundle;
6
use ScayTrase\Api\JsonRpc\JsonRpcError;
7
use ScayTrase\Api\JsonRpc\JsonRpcResponseInterface;
8
use ScayTrase\Api\JsonRpc\SyncResponse;
9
10
class ExceptionHandlingTest extends JsonRpcTestCase
11
{
12
    public function testBatchWithFailingMethod()
13
    {
14
        $client = self::createClient();
15
16
        $response = $this->sendRequest(
17
            $client,
18
            '/test/',
19
            [
20
                [
21
                    'jsonrpc' => JsonRpcBundle::VERSION,
22
                    'method'  => 'sample',
23
                    'id'      => 'test',
24
                    'params'  => [
25
                        'param1' => 'value1',
26
                        'param2' => 100500,
27
                    ],
28
                ],
29
                [
30
                    'jsonrpc' => JsonRpcBundle::VERSION,
31
                    'method'  => 'exception',
32
                    'id'      => 'test2',
33
                    'params'  => [
34
                        'param1' => 'value1',
35
                        'param2' => 100500,
36
                    ],
37
                ],
38
                [
39
                    //notification - no id
40
                    'jsonrpc' => JsonRpcBundle::VERSION,
41
                    'method'  => 'notification',
42
                    'param'   => [
43
                        'notification' => 'message',
44
                    ],
45
                ],
46
            ]
47
        );
48
49
        self::assertTrue($response->isSuccessful(), $response->getContent());
50
        $body = json_decode($response->getContent());
51
        self::assertTrue(is_array($body));
52
        self::assertCount(2, $body, json_encode($body, JSON_PRETTY_PRINT));
53
54
        /** @var JsonRpcResponseInterface[] $jsonResponses */
55
        $jsonResponses = [];
56
        foreach ($body as $responseBody) {
57
            $jsonResponses[] = new SyncResponse($responseBody);
58
        }
59
60
        self::assertTrue($jsonResponses[0]->isSuccessful());
61
        self::assertFalse($jsonResponses[1]->isSuccessful());
62
        self::assertEquals(JsonRpcError::INTERNAL_ERROR, $jsonResponses[1]->getError()->getCode());
63
        self::assertEquals('This is the malfunction controller', $jsonResponses[1]->getError()->getMessage());
64
    }
65
66
    public function getInvalidRequests()
67
    {
68
        return [
69
            'No method'        => [
70
                [
71
                    'jsonrpc' => JsonRpcBundle::VERSION,
72
                    'id'      => 'test',
73
                    'params'  => [],
74
                ],
75
            ],
76
            'No version'       => [
77
                [
78
                    'id'     => 'test',
79
                    'method' => 'sample',
80
                    'params' => [],
81
                ],
82
            ],
83
            'No id, no method' => [
84
                [
85
                    'id'     => 'test',
86
                    'params' => [],
87
                ],
88
            ],
89
        ];
90
    }
91
92
    /**
93
     * @dataProvider             getInvalidRequests
94
     *
95
     * @param array $request
96
     *
97
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
98
     * @expectedExceptionMessageRegExp |^Invalid JSONRPC 2.0 Request. Missing fields .+$|
99
     */
100
    public function testInvalidJsonRpcResultsIn400Response(array $request)
101
    {
102
        $client = self::createClient();
103
104
        $this->sendRequest(
105
            $client,
106
            '/test/',
107
            $request
108
        );
109
    }
110
}
111