1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\Emitter; |
4
|
|
|
|
5
|
|
|
use Nyholm\Psr7\Response; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Yiisoft\Yii\Web\Emitter\SapiEmitter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @runTestsInSeparateProcesses |
11
|
|
|
*/ |
12
|
|
|
class SapiEmitterTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testEmit() |
15
|
|
|
{ |
16
|
|
|
$body = 'Example body'; |
17
|
|
|
$response = new Response(200, ['X-Test' => 1], $body); |
18
|
|
|
|
19
|
|
|
ob_start(); |
20
|
|
|
(new SapiEmitter())->emit($response); |
21
|
|
|
$result = ob_get_contents(); |
22
|
|
|
ob_end_clean(); |
23
|
|
|
|
24
|
|
|
$this->assertEquals(200, http_response_code()); |
25
|
|
|
$this->checkHeadersEquals([ |
26
|
|
|
'X-Test: 1', |
27
|
|
|
'Content-Length: ' . strlen($body), |
28
|
|
|
]); |
29
|
|
|
$this->assertEquals($body, $result); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testEmptyBodyOnCode() |
33
|
|
|
{ |
34
|
|
|
$response = new Response(204, ['X-Test' => 1], 'Example body'); |
35
|
|
|
|
36
|
|
|
ob_start(); |
37
|
|
|
(new SapiEmitter())->emit($response); |
38
|
|
|
$result = ob_get_contents(); |
39
|
|
|
ob_end_clean(); |
40
|
|
|
|
41
|
|
|
$this->assertEquals(204, http_response_code()); |
42
|
|
|
$this->checkHeadersEquals([ |
43
|
|
|
'X-Test: 1', |
44
|
|
|
]); |
45
|
|
|
$this->assertEmpty($result); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testEmptyBodyOnFlag() |
49
|
|
|
{ |
50
|
|
|
$response = new Response(200, ['X-Test' => 1], 'Example body'); |
51
|
|
|
|
52
|
|
|
ob_start(); |
53
|
|
|
(new SapiEmitter())->emit($response, true); |
54
|
|
|
$result = ob_get_contents(); |
55
|
|
|
ob_end_clean(); |
56
|
|
|
|
57
|
|
|
$this->assertEquals(200, http_response_code()); |
58
|
|
|
$this->checkHeadersEquals([ |
59
|
|
|
'X-Test: 1', |
60
|
|
|
]); |
61
|
|
|
$this->assertEmpty($result); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testContentLengthHeader() |
65
|
|
|
{ |
66
|
|
|
$length = 100; |
67
|
|
|
$response = new Response(200, ['Content-length' => $length, 'X-Test' => 1], ''); |
68
|
|
|
|
69
|
|
|
ob_start(); |
70
|
|
|
(new SapiEmitter())->emit($response); |
71
|
|
|
$result = ob_get_contents(); |
72
|
|
|
ob_end_clean(); |
73
|
|
|
|
74
|
|
|
$this->assertEquals(200, http_response_code()); |
75
|
|
|
$this->checkHeadersEquals([ |
76
|
|
|
'X-Test: 1', |
77
|
|
|
'Content-Length: ' . $length, |
78
|
|
|
]); |
79
|
|
|
$this->assertEmpty($result); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $expected |
84
|
|
|
*/ |
85
|
|
|
private function checkHeadersEquals($expected) |
86
|
|
|
{ |
87
|
|
|
if (function_exists('xdebug_get_headers')) { |
88
|
|
|
$this->assertEquals($expected, xdebug_get_headers()); |
89
|
|
|
} else { |
90
|
|
|
$this->markAsRisky(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|