Passed
Pull Request — master (#139)
by Zhukov
01:48
created

shouldNotOutputBodyWhenResponseCodeIs204()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\Emitter;
4
5
use Nyholm\Psr7\Response;
6
use PHPUnit\Framework\TestCase;
7
use Yiisoft\Yii\Web\Emitter\SapiEmitter;
8
9
/**
10
 * @runTestsInSeparateProcesses
11
 */
12
class SapiEmitterTest extends TestCase
13
{
14
    public function testEmit()
15
    {
16
        $body = 'Example body';
17
        $response = new Response(200, ['X-Test' => 1], $body);
18
19
        ob_start();
20
        (new SapiEmitter())->emit($response);
21
        $result = ob_get_contents();
22
        ob_end_clean();
23
24
        $this->assertEquals(200, http_response_code());
25
        $this->checkHeadersEquals([
26
            'X-Test: 1',
27
            'Content-Length: ' . strlen($body),
28
        ]);
29
        $this->assertEquals($body, $result);
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function shouldNotOutputBodyWhenResponseCodeIs204()
36
    {
37
        $response = new Response(204, ['X-Test' => 1], 'Example body');
38
39
        ob_start();
40
        (new SapiEmitter())->emit($response);
41
        $result = ob_get_contents();
42
        ob_end_clean();
43
44
        $this->assertEquals(204, http_response_code());
45
        $this->checkHeadersEquals([
46
            'X-Test: 1',
47
        ]);
48
        $this->assertEmpty($result);
49
    }
50
51
    /*
52
     * @test
53
     */
54
    public function shouldNotOutputBodyIfEmitToldSo()
55
    {
56
        $response = new Response(200, ['X-Test' => 1], 'Example body');
57
58
        ob_start();
59
        (new SapiEmitter())->emit($response, true);
60
        $result = ob_get_contents();
61
        ob_end_clean();
62
63
        $this->assertEquals(200, http_response_code());
64
        $this->checkHeadersEquals([
65
            'X-Test: 1',
66
        ]);
67
        $this->assertEmpty($result);
68
    }
69
70
    /*
71
     * @test
72
     */
73
    public function contentLengthShouldNotBeOverwrittenIfPresent()
74
    {
75
        $length = 100;
76
        $response = new Response(200, ['Content-length' => $length, 'X-Test' => 1], '');
77
78
        ob_start();
79
        (new SapiEmitter())->emit($response);
80
        $result = ob_get_contents();
81
        ob_end_clean();
82
83
        $this->assertEquals(200, http_response_code());
84
        $this->checkHeadersEquals([
85
            'X-Test: 1',
86
            'Content-Length: ' . $length,
87
        ]);
88
        $this->assertEmpty($result);
89
    }
90
91
    /**
92
     * @param array $expected
93
     */
94
    private function checkHeadersEquals($expected)
95
    {
96
        if (function_exists('xdebug_get_headers')) {
97
            $this->assertEquals($expected, xdebug_get_headers());
98
        } else {
99
            $this->markAsRisky();
100
        }
101
    }
102
}
103