This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(response-processor): add a test for the StreamedResponseProcessor
- Loading branch information
1 parent
a55e064
commit bc4ba45
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
tests/Unit/Bridge/Symfony/HttpFoundation/StreamedResponseProcessorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Tests\Unit\Bridge\Symfony\HttpFoundation; | ||
|
||
use K911\Swoole\Bridge\Symfony\HttpFoundation\StreamedResponseProcessor; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Swoole\Http\Response as SwooleResponse; | ||
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
|
||
class StreamedResponseProcessorTest extends TestCase | ||
{ | ||
use \Prophecy\PhpUnit\ProphecyTrait; | ||
/** | ||
* @var StreamedResponseProcessor | ||
*/ | ||
protected $responseProcessor; | ||
|
||
/** | ||
* @var null|HttpFoundationResponse | ||
*/ | ||
protected $symfonyResponse; | ||
|
||
/** | ||
* @var null|ObjectProphecy|SwooleResponse | ||
*/ | ||
protected $swooleResponse; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $bufferSize; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->bufferSize = 10; | ||
$this->responseProcessor = new StreamedResponseProcessor($this->bufferSize); | ||
$this->swooleResponse = $this->prophesize(SwooleResponse::class); | ||
} | ||
|
||
public function testProcess(): void | ||
{ | ||
$expectedContentLength = $this->bufferSize * 3; | ||
$this->symfonyResponse = new StreamedResponse( | ||
static function () use ($expectedContentLength): void { | ||
for ($i = 0; $i < $expectedContentLength; ++$i) { | ||
echo 'A'; | ||
} | ||
}, | ||
200, | ||
); | ||
|
||
$remainingContentLength = $expectedContentLength; | ||
while ($remainingContentLength > 0) { | ||
$bufferedContentLength = \min($this->bufferSize, $remainingContentLength); | ||
$this->swooleResponse->write(\str_repeat('A', $bufferedContentLength))->shouldBeCalled(); | ||
$remainingContentLength -= $bufferedContentLength; | ||
} | ||
$this->swooleResponse->end()->shouldBeCalled(); | ||
$swooleResponse = $this->swooleResponse->reveal(); | ||
$this->responseProcessor->process($this->symfonyResponse, $swooleResponse); | ||
} | ||
} |