1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests; |
4
|
|
|
|
5
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
6
|
|
|
use Nyholm\Psr7\Response; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Yiisoft\Yii\Web\Formatter\XmlResponseFormatter; |
9
|
|
|
use Yiisoft\Yii\Web\Response as WebResponse; |
10
|
|
|
|
11
|
|
|
class XmlFormatterTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function testFormatter(): void |
14
|
|
|
{ |
15
|
|
|
$streamFactory = new Psr17Factory(); |
16
|
|
|
$response = new Response(); |
17
|
|
|
$webResponse = new WebResponse('test', $response, $streamFactory); |
18
|
|
|
$formatter = new XmlResponseFormatter($streamFactory); |
19
|
|
|
$result = $formatter->format($webResponse); |
20
|
|
|
$result->getBody()->rewind(); |
21
|
|
|
|
22
|
|
|
$this->assertSame( |
23
|
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>test</response>\n", |
24
|
|
|
$response->getBody()->getContents() |
25
|
|
|
); |
26
|
|
|
$this->assertSame(['application/xml; UTF-8'], $result->getHeader('Content-Type')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testFormatterEncoding(): void |
30
|
|
|
{ |
31
|
|
|
$streamFactory = new Psr17Factory(); |
32
|
|
|
$response = new Response(); |
33
|
|
|
$webResponse = new WebResponse('test', $response, $streamFactory); |
34
|
|
|
$formatter = new XmlResponseFormatter($streamFactory); |
35
|
|
|
$formatter->setEncoding('ISO-8859-1'); |
36
|
|
|
$result = $formatter->format($webResponse); |
37
|
|
|
$result->getBody()->rewind(); |
38
|
|
|
|
39
|
|
|
$this->assertSame( |
40
|
|
|
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<response>test</response>\n", |
41
|
|
|
$response->getBody()->getContents() |
42
|
|
|
); |
43
|
|
|
$this->assertSame(['application/xml; ISO-8859-1'], $result->getHeader('Content-Type')); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|