Completed
Pull Request — master (#175)
by
unknown
02:09
created

HTTPFunctionsTest::testAddedHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.9332
1
<?php
2
namespace Yiisoft\Yii\Web\Tests\Emitter;
3
4
include 'includeMocks.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 testReset(): void
24
    {
25
        // check initial state
26
        $this->assertEquals(200, $this->getResponseCode());
27
        $this->assertEquals([], $this->getHeaders());
28
29
        HTTPFunctions::header('X-Test: 3', false, 404);
30
31
        $this->assertEquals(404, $this->getResponseCode());
32
        $this->assertEquals(['X-Test: 3'], $this->getHeaders());
33
34
        HTTPFunctions::reset();
35
36
        $this->assertEquals(200, $this->getResponseCode());
37
        $this->assertEquals([], $this->getHeaders());
38
    }
39
40
    public function testHeaderAndHasHeader(): void
41
    {
42
        $this->assertFalse(HTTPFunctions::hasHeader('x-test'));
43
44
        HTTPFunctions::header('X-Test: 1');
45
46
        $this->assertTrue(HTTPFunctions::hasHeader('x-test'));
47
    }
48
49
    public function testAddedHeaders(): void
50
    {
51
        // first header
52
        HTTPFunctions::header('X-Test: 1');
53
        // added header, change status
54
        HTTPFunctions::header('X-Test: 2', false, 300);
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(300, $this->getResponseCode());
61
62
        // replace x-test headers, change status
63
        HTTPFunctions::header('X-Test: 3', true, 404);
64
        $this->assertEquals(['X-Test: 3'], $this->getHeaders());
65
        $this->assertEquals(404, $this->getResponseCode());
66
    }
67
68
    public function testHeaderRemove(): void
69
    {
70
        HTTPFunctions::header('X-Test: 1');
71
        HTTPFunctions::header('Y-Test: 2');
72
        HTTPFunctions::header('Z-Test: 3', false, 404);
73
74
        HTTPFunctions::header_remove('y-test');
75
        $this->assertEquals(['X-Test: 1', 'Z-Test: 3'], $this->getHeaders());
76
77
        HTTPFunctions::header_remove();
78
79
        $this->assertEquals(404, $this->getResponseCode());
80
        $this->assertEquals([], $this->getHeaders());
81
    }
82
83
    private function getHeaders(): array
84
    {
85
        return HTTPFunctions::headers_list();
86
    }
87
88
    private function getResponseCode(): int
89
    {
90
        return HTTPFunctions::http_response_code();
91
    }
92
}
93