FormmaterTest::testOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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