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