FormmaterTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 36
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testOptions() 0 8 1
A dataProviderFormat() 0 8 1
A testFormat() 0 11 2
1
<?php
2
namespace RazonYang\Yii2\JSend\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use RazonYang\Yii2\JSend\Formatter;
6
use yii\web\Response;
7
use RazonYang\JSend\PayloadInterface;
8
use RazonYang\JSend\PayloadFactory;
9
10
class FormmaterTest extends TestCase
11
{
12
    public function testOptions(): void
13
    {
14
        $formater = new Formatter();
15
        $this->assertSame(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES, $formater->getOptions());
16
17
        $newOptions = 0;
18
        $formater->setOptions($newOptions);
19
        $this->assertSame($newOptions, $formater->getOptions());
20
    }
21
22
    /**
23
     * @dataProvider dataProviderFormat
24
     */
25
    public function testFormat(Response $response): void
26
    {
27
        $formater = new Formatter();
28
        $formater->format($response);
29
        $this->assertSame('application/json; charset=UTF-8', $response->getHeaders()->get('Content-Type'));
30
        if ($response->data instanceof PayloadInterface) {
31
            $data = $response->data->toArray();
32
        } else {
33
            $data = $response->data;
34
        }
35
        $this->assertSame(json_encode($data, $formater->getOptions()), $response->content);
36
    }
37
38
    public function dataProviderFormat(): array
39
    {
40
        $response1 = new Response(['data' => 'foo']);
41
        $response2 = new Response(['data' => PayloadFactory::fail('fail')]);
42
43
        return [
44
            [$response1],
45
            [$response2],
46
        ];
47
    }
48
}
49