Passed
Push — master ( 4b20bd...439edc )
by Alexander
06:09
created

testFormatterWithoutObjectTags()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.8666
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
        $result = (new XmlDataResponseFormatter())
17
            ->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
        $result = (new XmlDataResponseFormatter())
32
            ->withEncoding('ISO-8859-1')
33
            ->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
        $result = (new XmlDataResponseFormatter())
48
            ->withVersion('1.1')
49
            ->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
        $result = (new XmlDataResponseFormatter())
64
            ->withRootTag('exampleRootTag')
65
            ->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
        $result = (new XmlDataResponseFormatter())
85
            ->withItemTag('customItemTag')
86
            ->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
    public function testFormatterWithObjectTags(): void
97
    {
98
        $data = new \stdClass();
99
        $data->attribute = 'test';
100
101
        $factory = new Psr17Factory();
102
        $dataResponse = new DataResponse($data, 200, '', $factory);
103
        $result = (new XmlDataResponseFormatter())
104
            ->withUseObjectTags(true)
105
            ->format($dataResponse);
106
        $result->getBody()->rewind();
107
108
        $this->assertSame(
109
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response><stdClass><attribute>test</attribute></stdClass></response>\n",
110
            $result->getBody()->getContents()
111
        );
112
        $this->assertSame(['application/xml; UTF-8'], $result->getHeader('Content-Type'));
113
    }
114
115
    public function testFormatterWithoutObjectTags(): void
116
    {
117
        $data = new \stdClass();
118
        $data->attribute = 'test';
119
120
        $factory = new Psr17Factory();
121
        $dataResponse = new DataResponse($data, 200, '', $factory);
122
        $result = (new XmlDataResponseFormatter())
123
            ->withUseObjectTags(false)
124
            ->format($dataResponse);
125
        $result->getBody()->rewind();
126
127
        $this->assertSame(
128
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response><attribute>test</attribute></response>\n",
129
            $result->getBody()->getContents()
130
        );
131
        $this->assertSame(['application/xml; UTF-8'], $result->getHeader('Content-Type'));
132
    }
133
134
    public function testFormatterWithContentType(): void
135
    {
136
        $factory = new Psr17Factory();
137
        $dataResponse = new DataResponse('test', 200, '', $factory);
138
        $result = (new XmlDataResponseFormatter())
139
            ->withContentType('text/xml')
140
            ->format($dataResponse);
141
        $result->getBody()->rewind();
142
143
        $this->assertSame(
144
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>test</response>\n",
145
            $result->getBody()->getContents()
146
        );
147
        $this->assertSame(['text/xml; UTF-8'], $result->getHeader('Content-Type'));
148
    }
149
}
150