Skip to content

Commit

Permalink
Cover empty body buffer flush
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Feb 27, 2024
1 parent 04f920f commit 3ed2f01
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/SapiEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ private function emitBody(ResponseInterface $response): void
while (!$body->eof()) {
$output = $body->read($this->bufferSize);
if ($output === '') {
while (ob_get_level() > $level) {
ob_end_flush();
}
continue;
}
echo $output;
Expand Down
22 changes: 19 additions & 3 deletions tests/SapiEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,30 @@ public function testObLevel(): void
$this->assertSame($expectedLevel, $actualLevel);
}

public function testExtraObLevel(): void
public static function dataExtraObLevel(): iterable
{
yield 'empty response' => [
'',
1,
];
yield 'some response' => [
'Example body',
2,
];
}

/**
* @dataProvider dataExtraObLevel
*/
public function testExtraObLevel(string $responseBody, int $expectedFlushes): void
{
$expectedLevel = ob_get_level();
$stream = $this->createMock(StreamInterface::class);
$stream->method('read')->willReturnCallback(static function () {
$stream->method('read')->willReturnCallback(static function () use ($responseBody) {
ob_start();
ob_start();
ob_start();
return '-';
return $responseBody;
});
$stream->method('isReadable')->willReturn(true);
$stream->method('eof')->willReturnOnConsecutiveCalls(false, true);
Expand All @@ -281,6 +296,7 @@ public function testExtraObLevel(): void

$actualLevel = ob_get_level();
$this->assertSame($expectedLevel, $actualLevel);
$this->assertSame($expectedFlushes, HTTPFunctions::getFlushTimes());
}

public function testFlushWithBody(): void
Expand Down

0 comments on commit 3ed2f01

Please sign in to comment.