Completed
Push — master ( 299de3...93b43e )
by Alexander
02:01
created

HTTPFunctionsTest::testHeaderRemove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
namespace Yiisoft\Yii\Web\Tests\Emitter;
3
4
include 'httpFunctionMocks.php';
5
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * @runTestsInSeparateProcesses
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
    }
28
29
    public function testHeaderAndHasHeader(): void
30
    {
31
        $this->assertFalse(HTTPFunctions::hasHeader('x-test'));
32
33
        HTTPFunctions::header('X-Test: 1');
34
35
        $this->assertTrue(HTTPFunctions::hasHeader('x-test'));
36
    }
37
38
    public function testReset(): void
39
    {
40
        HTTPFunctions::header('X-Test: 1');
41
        HTTPFunctions::header('X-Test: 2', false, 500);
42
43
        HTTPFunctions::reset();
44
45
        $this->assertEquals(200, $this->getResponseCode());
46
        $this->assertEquals([], $this->getHeaders());
47
    }
48
49
    public function testAddedHeaders(): void
50
    {
51
        // first header
52
        HTTPFunctions::header('X-Test: 1');
53
        // added header with new status
54
        HTTPFunctions::header('X-Test: 2', false, 500);
55
        HTTPFunctions::header('X-Test: 3', false);
56
57
        $this->assertContains('X-Test: 1', $this->getHeaders());
58
        $this->assertContains('X-Test: 2', $this->getHeaders());
59
        $this->assertContains('X-Test: 3', $this->getHeaders());
60
        $this->assertEquals(500, $this->getResponseCode());
61
    }
62
63
    public function testReplacingHeaders(): void
64
    {
65
        HTTPFunctions::header('X-Test: 1');
66
        HTTPFunctions::header('X-Test: 2', false, 300);
67
        HTTPFunctions::header('X-Test: 3', false);
68
69
        // replace x-test headers with new status
70
        HTTPFunctions::header('X-Test: 42', true, 404);
71
72
        $this->assertEquals(['X-Test: 42'], $this->getHeaders());
73
        $this->assertEquals(404, $this->getResponseCode());
74
    }
75
76
    public function testHeaderRemove(): void
77
    {
78
        HTTPFunctions::header('X-Test: 1');
79
        HTTPFunctions::header('Y-Test: 2');
80
        HTTPFunctions::header('Z-Test: 3', false, 404);
81
82
        HTTPFunctions::header_remove('y-test');
83
84
        $this->assertEquals(['X-Test: 1', 'Z-Test: 3'], $this->getHeaders());
85
    }
86
87
    public function testHeaderRemoveAll(): void
88
    {
89
        HTTPFunctions::header('X-Test: 1');
90
        HTTPFunctions::header('Y-Test: 2');
91
        HTTPFunctions::header('Z-Test: 3', false, 404);
92
93
        HTTPFunctions::header_remove();
94
95
        $this->assertEquals(404, $this->getResponseCode());
96
        $this->assertEquals([], $this->getHeaders());
97
    }
98
99
    private function getHeaders(): array
100
    {
101
        return HTTPFunctions::headers_list();
102
    }
103
104
    private function getResponseCode(): int
105
    {
106
        return HTTPFunctions::http_response_code();
107
    }
108
}
109