1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Web\Tests\Emitter\Support; |
6
|
|
|
|
7
|
|
|
include 'httpFunctionMocks.php'; |
8
|
|
|
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @runTestsInSeparateProcesses |
13
|
|
|
* @preserveGlobalState disabled |
14
|
|
|
*/ |
15
|
|
|
class HTTPFunctionsTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function setUp(): void |
18
|
|
|
{ |
19
|
|
|
HTTPFunctions::reset(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function tearDownAfterClass(): void |
23
|
|
|
{ |
24
|
|
|
HTTPFunctions::reset(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testInitialState(): void |
28
|
|
|
{ |
29
|
|
|
$this->assertEquals(200, $this->getResponseCode()); |
30
|
|
|
$this->assertEquals([], $this->getHeaders()); |
31
|
|
|
$this->assertFalse(HTTPFunctions::headers_sent()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testHeaderAndHasHeader(): void |
35
|
|
|
{ |
36
|
|
|
$this->assertFalse(HTTPFunctions::hasHeader('x-test')); |
37
|
|
|
|
38
|
|
|
HTTPFunctions::header('X-Test: 1'); |
39
|
|
|
|
40
|
|
|
$this->assertTrue(HTTPFunctions::hasHeader('x-test')); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testReset(): void |
44
|
|
|
{ |
45
|
|
|
HTTPFunctions::header('X-Test: 1'); |
46
|
|
|
HTTPFunctions::header('X-Test: 2', false, 500); |
47
|
|
|
HTTPFunctions::set_headers_sent(true, 'test', 123); |
48
|
|
|
|
49
|
|
|
HTTPFunctions::reset(); |
50
|
|
|
|
51
|
|
|
$this->assertEquals(200, $this->getResponseCode()); |
52
|
|
|
$this->assertEquals([], $this->getHeaders()); |
53
|
|
|
$this->assertFalse(HTTPFunctions::headers_sent($file, $line)); |
54
|
|
|
$this->assertEquals('', $file); |
55
|
|
|
$this->assertEquals(0, $line); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testHeadersSent(): void |
59
|
|
|
{ |
60
|
|
|
HTTPFunctions::set_headers_sent(true, 'path/to/test/file.php', 123); |
61
|
|
|
|
62
|
|
|
$this->assertTrue(HTTPFunctions::headers_sent($file, $line)); |
63
|
|
|
$this->assertEquals('path/to/test/file.php', $file); |
64
|
|
|
$this->assertEquals(123, $line); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testAddedHeaders(): void |
68
|
|
|
{ |
69
|
|
|
// first header |
70
|
|
|
HTTPFunctions::header('X-Test: 1'); |
71
|
|
|
// added header with new status |
72
|
|
|
HTTPFunctions::header('X-Test: 2', false, 500); |
73
|
|
|
HTTPFunctions::header('X-Test: 3', false); |
74
|
|
|
|
75
|
|
|
$this->assertContains('X-Test: 1', $this->getHeaders()); |
76
|
|
|
$this->assertContains('X-Test: 2', $this->getHeaders()); |
77
|
|
|
$this->assertContains('X-Test: 3', $this->getHeaders()); |
78
|
|
|
$this->assertEquals(500, $this->getResponseCode()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testReplacingHeaders(): void |
82
|
|
|
{ |
83
|
|
|
HTTPFunctions::header('X-Test: 1'); |
84
|
|
|
HTTPFunctions::header('X-Test: 2', false, 300); |
85
|
|
|
HTTPFunctions::header('X-Test: 3', false); |
86
|
|
|
|
87
|
|
|
// replace x-test headers with new status |
88
|
|
|
HTTPFunctions::header('X-Test: 42', true, 404); |
89
|
|
|
|
90
|
|
|
$this->assertEquals(['X-Test: 42'], $this->getHeaders()); |
91
|
|
|
$this->assertEquals(404, $this->getResponseCode()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testHeaderRemove(): void |
95
|
|
|
{ |
96
|
|
|
HTTPFunctions::header('X-Test: 1'); |
97
|
|
|
HTTPFunctions::header('Y-Test: 2'); |
98
|
|
|
HTTPFunctions::header('Z-Test: 3', false, 404); |
99
|
|
|
|
100
|
|
|
HTTPFunctions::header_remove('y-test'); |
101
|
|
|
|
102
|
|
|
$this->assertEquals(['X-Test: 1', 'Z-Test: 3'], $this->getHeaders()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testHeaderRemoveAll(): void |
106
|
|
|
{ |
107
|
|
|
HTTPFunctions::header('X-Test: 1'); |
108
|
|
|
HTTPFunctions::header('Y-Test: 2'); |
109
|
|
|
HTTPFunctions::header('Z-Test: 3', false, 404); |
110
|
|
|
|
111
|
|
|
HTTPFunctions::header_remove(); |
112
|
|
|
|
113
|
|
|
$this->assertEquals(404, $this->getResponseCode()); |
114
|
|
|
$this->assertEquals([], $this->getHeaders()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function getHeaders(): array |
118
|
|
|
{ |
119
|
|
|
return HTTPFunctions::headers_list(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function getResponseCode(): int |
123
|
|
|
{ |
124
|
|
|
return HTTPFunctions::http_response_code(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|