1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenEngine\Mika\Core\Components\Http\Message\Response\Tests; |
4
|
|
|
|
5
|
|
|
use OpenEngine\Mika\Core\Components\Http\Message\Response\Response; |
6
|
|
|
use OpenEngine\Mika\Core\Components\Http\Message\Stream\StreamFactory; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class ResponseTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testBody(): void |
12
|
|
|
{ |
13
|
|
|
$response = new Response('It is response body'); |
14
|
|
|
self::assertStringEndsWith('It is response body', $response->getBody()->__toString()); |
15
|
|
|
self::assertEquals(200, $response->getStatusCode()); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testCode(): void |
19
|
|
|
{ |
20
|
|
|
$response = new Response('It is response body', 201); |
21
|
|
|
self::assertStringEndsWith('It is response body', $response->getBody()->__toString()); |
22
|
|
|
self::assertEquals(201, $response->getStatusCode()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testReason(): void |
26
|
|
|
{ |
27
|
|
|
$response = new Response('It is response body', 403, 'Something went wrong'); |
28
|
|
|
self::assertStringEndsWith('It is response body', $response->getBody()->__toString()); |
29
|
|
|
self::assertNotEquals(200, $response->getStatusCode()); |
30
|
|
|
self::assertStringEndsWith('wrong', $response->getReasonPhrase()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testHeaders(): void |
34
|
|
|
{ |
35
|
|
|
$response = new Response('', 200, '', [ |
36
|
|
|
'Content-type' => ['charser=utf-8;text/html'], |
37
|
|
|
'Accept' => ['utf-8', 'ISO-8859-1', 'q=0.7'] |
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
self::assertArrayHasKey('content-type', $response->getHeaders()); |
41
|
|
|
self::assertStringEndsWith('utf-8, ISO-8859-1, q=0.7', $response->getHeaderLine('Accept')); |
42
|
|
|
self::assertEquals(200, $response->getStatusCode()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testWithoutHeader(): void |
46
|
|
|
{ |
47
|
|
|
$response = new Response('', 200, '', [ |
48
|
|
|
'Content-type' => ['charser=utf-8;text/html'], |
49
|
|
|
'Accept' => ['utf-8', 'ISO-8859-1', 'q=0.7'] |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
$response = $response->withoutHeader('Content-type'); |
53
|
|
|
|
54
|
|
|
self::assertArrayNotHasKey('content-type', $response->getHeaders()); |
55
|
|
|
self::assertArrayHasKey('accept', $response->getHeaders()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testWithBody(): void |
59
|
|
|
{ |
60
|
|
|
$response = new Response('Foo'); |
61
|
|
|
|
62
|
|
|
$response = $response->withBody((new StreamFactory)->createStream('Bar')); |
63
|
|
|
|
64
|
|
|
self::assertStringEndsWith('Bar', $response->getBody()->__toString()); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|