Completed
Push — master ( 0115c1...e43345 )
by Alexander
16:00 queued 13:56
created

contentAlwaysShouldBeFullyEmitted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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(): void
15
    {
16
        $body = 'Example body';
17
        $response = new Response(200, ['X-Test' => 1], $body);
18
19
        (new SapiEmitter())->emit($response);
20
21
        $this->assertEquals(200, http_response_code());
22
        $this->checkHeadersEquals([
23
            'X-Test: 1',
24
            'Content-Length: ' . strlen($body),
25
        ]);
26
        $this->expectOutputString($body);
27
    }
28
29
    /**
30
     * @test
31
     */
32
    public function shouldNotOutputBodyWhenResponseCodeIs204(): void
33
    {
34
        $response = new Response(204, ['X-Test' => 1], 'Example body');
35
36
        (new SapiEmitter())->emit($response);
37
38
        $this->assertEquals(204, http_response_code());
39
        $this->checkHeadersEquals([
40
            'X-Test: 1',
41
        ]);
42
        $this->expectOutputString('');
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function shouldNotOutputBodyIfEmitToldSo(): void
49
    {
50
        $response = new Response(200, ['X-Test' => 1], 'Example body');
51
52
        (new SapiEmitter())->emit($response, true);
53
54
        $this->assertEquals(200, http_response_code());
55
        $this->checkHeadersEquals([
56
            'X-Test: 1',
57
        ]);
58
        $this->expectOutputString('');
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function contentLengthShouldNotBeOverwrittenIfPresent(): void
65
    {
66
        $length = 100;
67
        $response = new Response(200, ['Content-length' => $length, 'X-Test' => 1], '');
68
69
        (new SapiEmitter())->emit($response);
70
71
        $this->assertEquals(200, http_response_code());
72
        $this->checkHeadersEquals([
73
            'X-Test: 1',
74
            'Content-Length: ' . $length,
75
        ]);
76
        $this->expectOutputString('');
77
    }
78
79
    /**
80
     * @test
81
     */
82
    public function contentAlwaysShouldBeFullyEmitted(): void
83
    {
84
        $body = 'Example body';
85
        $response = new Response(200, ['Content-length' => 1, 'X-Test' => 1], $body);
86
87
        (new SapiEmitter())->emit($response);
88
89
        $this->expectOutputString($body);
90
    }
91
92
    /**
93
     * @param array $expected
94
     */
95
    private function checkHeadersEquals($expected)
96
    {
97
        if (function_exists('xdebug_get_headers')) {
98
            $this->assertEquals($expected, xdebug_get_headers());
99
        } else {
100
            $this->markAsRisky();
101
        }
102
    }
103
}
104