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

XmlFormatterTest::testFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9666
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests;
4
5
use Nyholm\Psr7\Factory\Psr17Factory;
6
use PHPUnit\Framework\TestCase;
7
use Yiisoft\Yii\Web\Formatter\XmlResponseFormatter;
8
use Yiisoft\Yii\Web\WebResponse;
9
10
class XmlFormatterTest extends TestCase
11
{
12
    public function testFormatter(): void
13
    {
14
        $factory = new Psr17Factory();
15
        $webResponse = new WebResponse('test', 200, $factory, $factory);
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Yii\Web\WebResponse::__construct() has too many arguments starting with $factory. ( Ignorable by Annotation )

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

15
        $webResponse = /** @scrutinizer ignore-call */ new WebResponse('test', 200, $factory, $factory);

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...
16
        $formatter = new XmlResponseFormatter();
17
        $result = $formatter->format($webResponse);
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
        $webResponse = new WebResponse('test', 200, $factory, $factory);
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Yii\Web\WebResponse::__construct() has too many arguments starting with $factory. ( Ignorable by Annotation )

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

30
        $webResponse = /** @scrutinizer ignore-call */ new WebResponse('test', 200, $factory, $factory);

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...
31
        $formatter = new XmlResponseFormatter();
32
        $formatter->setEncoding('ISO-8859-1');
33
        $result = $formatter->format($webResponse);
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