Passed
Pull Request — master (#144)
by
unknown
01:43
created

JsonpRendererTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 64
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A simpleDataProvider() 0 27 1
A testSimple() 0 27 1
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\ErrorHandler;
4
5
use Nyholm\Psr7\ServerRequest;
6
use Yiisoft\Yii\Web\ErrorHandler\JsonpRenderer;
7
use Yiisoft\Yii\Web\Tests\ErrorHandler\Mock\ThrowableMock;
8
use PHPUnit\Framework\TestCase;
9
10
class JsonpRendererTest extends TestCase
11
{
12
13
    public function simpleDataProvider(): array
14
    {
15
        return [
16
            'get' => [
17
                [
18
                    'callback' => 'testCallback',
19
                ],
20
                'callback',
21
                'testCallback',
22
                null,
23
                'testCallback',
24
            ],
25
            'overrideGet' => [
26
                [
27
                    'callback' => 'testCallback',
28
                ],
29
                'callback',
30
                'testCallback',
31
                'anyCallback',
32
                'anyCallback',
33
            ],
34
            'callbackNotFound' => [
35
                [],
36
                'callback',
37
                'testCallback',
38
                null,
39
                'console.log',
40
            ],
41
        ];
42
    }
43
44
    /**
45
     * @dataProvider simpleDataProvider
46
     */
47
    public function testSimple(
48
        array $queryParams,
49
        string $callbackParam,
50
        ?string $callbackGet,
0 ignored issues
show
Unused Code introduced by
The parameter $callbackGet is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
        /** @scrutinizer ignore-unused */ ?string $callbackGet,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
        ?string $callback,
52
        string $expectedCallback
53
    ): void {
54
        $params = [
55
            'message' => 'test',
56
            'code' => 879,
57
        ];
58
        $throw = ThrowableMock::newInstance($params);
59
        $request = (new ServerRequest('GET', '/'))->withQueryParams($queryParams);
60
61
        $renderer = new JsonpRenderer();
62
        $renderer->setCallback($callback);
63
        $renderer->setCallbackParameter($callbackParam);
64
        $renderer->setRequest($request);
65
        $result = $renderer->render($throw);
66
        $this->assertEquals(1,
67
            \preg_match('/^' . \preg_quote($expectedCallback) . '[(](?<json>.+?)[)]$/sm', $result, $matches));
68
        $data = json_decode($matches['json'], true);
69
        $this->assertSame($params['message'], $data['message']);
70
        $this->assertSame($params['code'], $data['code']);
71
        $this->assertIsInt($data['line']);
72
        $this->assertIsString($data['file']);
73
        $this->assertIsArray($data['trace']);
74
    }
75
76
}
77