1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\Data; |
4
|
|
|
|
5
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Yiisoft\Http\Status; |
9
|
|
|
use Yiisoft\Yii\Web\Data\Formatter\JsonDataResponseFormatter; |
10
|
|
|
use Yiisoft\Yii\Web\Data\DataResponse; |
11
|
|
|
|
12
|
|
|
class DataResponseTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testCreateResponse(): void |
15
|
|
|
{ |
16
|
|
|
$factory = new Psr17Factory(); |
17
|
|
|
$dataResponse = new DataResponse('test', Status::OK, '', $factory); |
18
|
|
|
$dataResponse = $dataResponse->withHeader('Content-Type', 'application/json'); |
19
|
|
|
$dataResponse->getBody()->rewind(); |
20
|
|
|
|
21
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $dataResponse); |
22
|
|
|
$this->assertSame(['application/json'], $dataResponse->getResponse()->getHeader('Content-Type')); |
23
|
|
|
$this->assertSame(['application/json'], $dataResponse->getHeader('Content-Type')); |
24
|
|
|
$this->assertSame($dataResponse->getResponse()->getBody(), $dataResponse->getBody()); |
25
|
|
|
$this->assertSame('test', $dataResponse->getBody()->getContents()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testChangeResponseData(): void |
29
|
|
|
{ |
30
|
|
|
$factory = new Psr17Factory(); |
31
|
|
|
$dataResponse = new DataResponse('test', Status::OK, '', $factory); |
32
|
|
|
$data = $dataResponse->getData(); |
33
|
|
|
$data .= '-changed'; |
34
|
|
|
$dataResponse = $dataResponse->withData($data); |
35
|
|
|
$dataResponse->getBody()->rewind(); |
36
|
|
|
|
37
|
|
|
$this->assertSame('test-changed', $dataResponse->getBody()->getContents()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testSetResponseFormatter(): void |
41
|
|
|
{ |
42
|
|
|
$factory = new Psr17Factory(); |
43
|
|
|
$dataResponse = new DataResponse('test', Status::OK, '', $factory); |
44
|
|
|
$dataResponse = $dataResponse->withResponseFormatter(new JsonDataResponseFormatter()); |
45
|
|
|
|
46
|
|
|
$this->assertTrue($dataResponse->hasResponseFormatter()); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|