Passed
Pull Request — master (#255)
by
unknown
26:08 queued 11:10
created

XmlDataResponseFormatterTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 54
c 2
b 0
f 1
dl 0
loc 84
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testFormatterEncoding() 0 14 1
A testFormatter() 0 13 1
A testFormatterItemTagWhenNameIsEmptyOrInvalid() 0 19 1
A testFormatterRootTag() 0 14 1
A testFormatterVersion() 0 14 1
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\Data\Formatter;
4
5
use Nyholm\Psr7\Factory\Psr17Factory;
6
use PHPUnit\Framework\TestCase;
7
use Yiisoft\Yii\Web\Data\Formatter\XmlDataResponseFormatter;
8
use Yiisoft\Yii\Web\Data\DataResponse;
9
10
class XmlDataResponseFormatterTest extends TestCase
11
{
12
    public function testFormatter(): void
13
    {
14
        $factory = new Psr17Factory();
15
        $dataResponse = new DataResponse('test', 200, '', $factory);
16
        $formatter = new XmlDataResponseFormatter();
17
        $result = $formatter->format($dataResponse);
18
        $result->getBody()->rewind();
19
20
        $this->assertSame(
21
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>test</response>\n",
22
            $result->getBody()->getContents()
23
        );
24
        $this->assertSame(['application/xml; UTF-8'], $result->getHeader('Content-Type'));
25
    }
26
27
    public function testFormatterEncoding(): void
28
    {
29
        $factory = new Psr17Factory();
30
        $dataResponse = new DataResponse('test', 200, '', $factory);
31
        $formatter = new XmlDataResponseFormatter();
32
        $formatter = $formatter->withEncoding('ISO-8859-1');
33
        $result = $formatter->format($dataResponse);
34
        $result->getBody()->rewind();
35
36
        $this->assertSame(
37
            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<response>test</response>\n",
38
            $result->getBody()->getContents()
39
        );
40
        $this->assertSame(['application/xml; ISO-8859-1'], $result->getHeader('Content-Type'));
41
    }
42
43
    public function testFormatterVersion(): void
44
    {
45
        $factory = new Psr17Factory();
46
        $dataResponse = new DataResponse('test', 200, '', $factory);
47
        $formatter = new XmlDataResponseFormatter();
48
        $formatter = $formatter->withVersion('1.1');
49
        $result = $formatter->format($dataResponse);
50
        $result->getBody()->rewind();
51
52
        $this->assertSame(
53
            "<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n<response>test</response>\n",
54
            $result->getBody()->getContents()
55
        );
56
        $this->assertSame(['application/xml; UTF-8'], $result->getHeader('Content-Type'));
57
    }
58
59
    public function testFormatterRootTag(): void
60
    {
61
        $factory = new Psr17Factory();
62
        $dataResponse = new DataResponse('test', 200, '', $factory);
63
        $formatter = new XmlDataResponseFormatter();
64
        $formatter = $formatter->withRootTag('exampleRootTag');
65
        $result = $formatter->format($dataResponse);
66
        $result->getBody()->rewind();
67
68
        $this->assertSame(
69
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<exampleRootTag>test</exampleRootTag>\n",
70
            $result->getBody()->getContents()
71
        );
72
        $this->assertSame(['application/xml; UTF-8'], $result->getHeader('Content-Type'));
73
    }
74
75
    public function testFormatterItemTagWhenNameIsEmptyOrInvalid(): void
76
    {
77
        $factory = new Psr17Factory();
78
        $data = [
79
            'test',
80
            'validName' => 'test',
81
            '1_invalidName' => 'test'
82
        ];
83
        $dataResponse = new DataResponse($data, 200, '', $factory);
84
        $formatter = new XmlDataResponseFormatter();
85
        $formatter = $formatter->withItemTag('customItemTag');
86
        $result = $formatter->format($dataResponse);
87
        $result->getBody()->rewind();
88
89
        $this->assertSame(
90
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response><customItemTag>test</customItemTag><validName>test</validName><customItemTag>test</customItemTag></response>\n",
91
            $result->getBody()->getContents()
92
        );
93
        $this->assertSame(['application/xml; UTF-8'], $result->getHeader('Content-Type'));
94
    }
95
}
96