Passed
Push — master ( 53af6f...4b20bd )
by Alexander
02:25 queued 10s
created

HTTPFunctionsTest::testHeaderRemoveAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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