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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Tests\Psr7;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Slim\Psr7\NonBufferedBody;
use Slim\Psr7\Response;
use Slim\Tests\Psr7\Assets\HeaderStack;
use function ob_get_clean;
use function ob_get_level;
use function ob_start;
use function strlen;
class NonBufferedBodyTest extends TestCase
{
protected function setUp(): void
{
HeaderStack::reset();
}
protected function tearDown(): void
{
HeaderStack::reset();
}
public function testTheStreamContract()
{
$body = new NonBufferedBody();
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->getContents(), 'Data cannot be retrieved once written');
self::assertNull($body->getMetadata(), 'Metadata mechanism is not implemented');
}
public function testWrite()
{
$this->markTestSkipped('Needs "adriansuter/php-autoload-override" to override a built-in PHP function');
$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');
$this->markTestSkipped('Needs "adriansuter/php-autoload-override" to override a built-in PHP function');
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');
$this->markTestSkipped('Needs "adriansuter/php-autoload-override" to override a built-in PHP function');
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');
$this->markTestSkipped('Needs "adriansuter/php-autoload-override" to override a built-in PHP function');
self::assertSame([], HeaderStack::stack());
}
public function testCloseThrowsRuntimeException()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('A NonBufferedBody is not closable.');
(new NonBufferedBody())->close();
}
public function testSeekThrowsRuntimeException()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('A NonBufferedBody is not seekable.');
(new NonBufferedBody())->seek(10);
}
public function testRewindThrowsRuntimeException()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('A NonBufferedBody is not rewindable.');
(new NonBufferedBody())->rewind();
}
public function testReadThrowsRuntimeException()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('A NonBufferedBody is not readable.');
(new NonBufferedBody())->read(10);
}
}
|