Passed
Pull Request — master (#233)
by Dmitriy
02:25
created

XmlFormatterTest::testFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.9332
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\WebResponse 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();
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);
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Yii\Web\Formatte...ormatter::__construct() has too many arguments starting with $streamFactory. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $formatter = /** @scrutinizer ignore-call */ new XmlResponseFormatter($streamFactory);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
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