Completed
Push — master ( a7ee47...bc94bf )
by Pavel
04:48
created

ExceptionHandlingTest::getInvalidRequests()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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