Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
test(response-processor): add a test for the StreamedResponseProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
supersmile2009 authored and k911 committed Feb 3, 2021
1 parent a55e064 commit bc4ba45
Showing 1 changed file with 66 additions and 0 deletions.
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);
}
}

0 comments on commit bc4ba45

Please sign in to comment.