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

WebResponseTest::testCreateResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9332
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests;
4
5
use Nyholm\Psr7\Factory\Psr17Factory;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Http\Message\ResponseInterface;
8
use Yiisoft\Yii\Web\Formatter\JsonResponseFormatter;
9
use Yiisoft\Yii\Web\WebResponse;
10
11
class WebResponseTest extends TestCase
12
{
13
    public function testCreateResponse(): void
14
    {
15
        $factory = new Psr17Factory();
16
        $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

16
        $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...
17
        $webResponse = $webResponse->withHeader('Content-Type', 'application/json');
18
        $webResponse->getBody()->rewind();
19
20
        $this->assertInstanceOf(ResponseInterface::class, $webResponse);
21
        $this->assertInstanceOf(ResponseInterface::class, $webResponse->getResponse());
22
        $this->assertSame(['application/json'], $webResponse->getResponse()->getHeader('Content-Type'));
23
        $this->assertSame(['application/json'], $webResponse->getHeader('Content-Type'));
24
        $this->assertSame('', $webResponse->getResponse()->getBody()->getContents());
25
        $this->assertSame('test', $webResponse->getBody()->getContents());
26
    }
27
28
    public function testChangeResponseData(): void
29
    {
30
        $factory = new Psr17Factory();
31
        $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

31
        $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...
32
        $data = $webResponse->getData();
33
        $data .= '-changed';
34
        $webResponse = $webResponse->withData($data);
35
        $webResponse->getBody()->rewind();
36
37
        $this->assertSame('test-changed', $webResponse->getBody()->getContents());
38
    }
39
40
    public function testSetResponseFormatter(): void
41
    {
42
        $factory = new Psr17Factory();
43
        $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

43
        $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...
44
        $webResponse = $webResponse->withResponseFormatter(new JsonResponseFormatter());
45
46
        $this->assertTrue($webResponse->hasResponseFormatter());
47
    }
48
}
49