Passed
Pull Request — master (#602)
by Aleksei
07:06
created

HTTPFunctionsTest::testReset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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