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
|
|
|
/** |
33
|
|
|
* @test |
34
|
|
|
*/ |
35
|
|
|
public function shouldNotOutputBodyWhenResponseCodeIs204() |
36
|
|
|
{ |
37
|
|
|
$response = new Response(204, ['X-Test' => 1], 'Example body'); |
38
|
|
|
|
39
|
|
|
ob_start(); |
40
|
|
|
(new SapiEmitter())->emit($response); |
41
|
|
|
$result = ob_get_contents(); |
42
|
|
|
ob_end_clean(); |
43
|
|
|
|
44
|
|
|
$this->assertEquals(204, http_response_code()); |
45
|
|
|
$this->checkHeadersEquals([ |
46
|
|
|
'X-Test: 1', |
47
|
|
|
]); |
48
|
|
|
$this->assertEmpty($result); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/* |
52
|
|
|
* @test |
53
|
|
|
*/ |
54
|
|
|
public function shouldNotOutputBodyIfEmitToldSo() |
55
|
|
|
{ |
56
|
|
|
$response = new Response(200, ['X-Test' => 1], 'Example body'); |
57
|
|
|
|
58
|
|
|
ob_start(); |
59
|
|
|
(new SapiEmitter())->emit($response, true); |
60
|
|
|
$result = ob_get_contents(); |
61
|
|
|
ob_end_clean(); |
62
|
|
|
|
63
|
|
|
$this->assertEquals(200, http_response_code()); |
64
|
|
|
$this->checkHeadersEquals([ |
65
|
|
|
'X-Test: 1', |
66
|
|
|
]); |
67
|
|
|
$this->assertEmpty($result); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testContentLengthHeader() |
71
|
|
|
{ |
72
|
|
|
$length = 100; |
73
|
|
|
$response = new Response(200, ['Content-length' => $length, 'X-Test' => 1], ''); |
74
|
|
|
|
75
|
|
|
ob_start(); |
76
|
|
|
(new SapiEmitter())->emit($response); |
77
|
|
|
$result = ob_get_contents(); |
78
|
|
|
ob_end_clean(); |
79
|
|
|
|
80
|
|
|
$this->assertEquals(200, http_response_code()); |
81
|
|
|
$this->checkHeadersEquals([ |
82
|
|
|
'X-Test: 1', |
83
|
|
|
'Content-Length: ' . $length, |
84
|
|
|
]); |
85
|
|
|
$this->assertEmpty($result); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $expected |
90
|
|
|
*/ |
91
|
|
|
private function checkHeadersEquals($expected) |
92
|
|
|
{ |
93
|
|
|
if (function_exists('xdebug_get_headers')) { |
94
|
|
|
$this->assertEquals($expected, xdebug_get_headers()); |
95
|
|
|
} else { |
96
|
|
|
$this->markAsRisky(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|