|
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
|
|
|
|