File: NonBufferedBodyTest.php

package info (click to toggle)
php-slim 3.12.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,396 kB
  • sloc: php: 11,581; makefile: 18; xml: 10; sh: 3
file content (132 lines) | stat: -rw-r--r-- 4,080 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
 * Slim Framework (https://slimframework.com)
 *
 * @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
 */

namespace Slim\Tests\Http;

use PHPUnit;
use Slim\Http\NonBufferedBody;
use Slim\Http\Response;
use Slim\Tests\Assets\HeaderStack;

class NonBufferedBodyTest extends PHPUnit\Framework\TestCase
{
    protected function setUp(): void
    {
        HeaderStack::reset();
    }

    protected function tearDown(): void
    {
        HeaderStack::reset();
    }

    public function testTheStreamContract()
    {
        $body = new NonBufferedBody();
        $body->close();
        $body->seek(0);
        $body->rewind();

        self::assertSame('', (string) $body, 'Casting to string returns no data, since the class does not store any');
        self::assertNull($body->detach(), 'Returns null since there is no such underlying stream');
        self::assertNull($body->getSize(), 'Current size is undefined');
        self::assertSame(0, $body->tell(), 'Pointer is considered to be at position 0 to conform');
        self::assertTrue($body->eof(), 'Always considered to be at EOF');
        self::assertFalse($body->isSeekable(), 'Cannot seek');
        self::assertTrue($body->isWritable(), 'Body is writable');
        self::assertFalse($body->isReadable(), 'Body is not readable');
        self::assertSame('', $body->read(10), 'Data cannot be retrieved once written');
        self::assertSame('', $body->getContents(), 'Data cannot be retrieved once written');
        self::assertNull($body->getMetadata(), 'Metadata mechanism is not implemented');
    }

    public function testWrite()
    {
        $ob_initial_level = ob_get_level();

        // Start output buffering.
        ob_start();

        // Start output buffering again to test the while-loop in the `write()`
        // method that calls `ob_get_clean()` as long as the ob level is bigger
        // than 0.
        ob_start();
        echo 'buffer content: ';

        // Set the ob level shift that should be applied in the `ob_get_level()`
        // function override. That way, the `write()` method would only flush
        // the second ob, not the first one. We will add the initial ob level
        // because phpunit may have started ob too.
        $GLOBALS['ob_get_level_shift'] = -($ob_initial_level + 1);

        $body    = new NonBufferedBody();
        $length0 = $body->write('hello ');
        $length1 = $body->write('world');

        unset($GLOBALS['ob_get_level_shift']);
        $contents = ob_get_clean();

        $this->assertEquals(strlen('buffer content: ') + strlen('hello '), $length0);
        $this->assertEquals(strlen('world'), $length1);
        $this->assertEquals('buffer content: hello world', $contents);
    }

    /**
     * @runInSeparateProcess
     */
    public function testWithHeader()
    {
        (new Response())
            ->withBody(new NonBufferedBody())
            ->withHeader('Foo', 'Bar');

        self::assertSame([
            [
                'header' => 'Foo: Bar',
                'replace' => true,
                'status_code' => null
            ]
        ], HeaderStack::stack());
    }

    /**
     * @runInSeparateProcess
     */
    public function testWithAddedHeader()
    {
        (new Response())
            ->withBody(new NonBufferedBody())
            ->withHeader('Foo', 'Bar')
            ->withAddedHeader('Foo', 'Baz');

        self::assertSame([
            [
                'header' => 'Foo: Bar',
                'replace' => true,
                'status_code' => null
            ],
            [
                'header' => 'Foo: Bar,Baz',
                'replace' => true,
                'status_code' => null
            ]
        ], HeaderStack::stack());
    }

    /**
     * @runInSeparateProcess
     */
    public function testWithoutHeader()
    {
        (new Response())
            ->withBody(new NonBufferedBody())
            ->withHeader('Foo', 'Bar')
            ->withoutHeader('Foo');

        self::assertSame([], HeaderStack::stack());
    }
}