|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://doc.hyperf.io |
| 9 | + * @contact group@hyperf.io |
| 10 | + * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | + |
| 13 | +namespace HyperfTest\HttpMessage; |
| 14 | + |
| 15 | +use Hyperf\HttpMessage\Server\Response; |
| 16 | +use Hyperf\HttpMessage\Stream\SwooleFileStream; |
| 17 | +use Hyperf\HttpMessage\Stream\SwooleStream; |
| 18 | +use Mockery; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Swoole\Http\Response as SwooleResponse; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + * @coversNothing |
| 25 | + */ |
| 26 | +class SwooleStreamTest extends TestCase |
| 27 | +{ |
| 28 | + public function testSwooleFileStream() |
| 29 | + { |
| 30 | + $swooleResponse = Mockery::mock(SwooleResponse::class); |
| 31 | + $file = __FILE__; |
| 32 | + $swooleResponse->shouldReceive('sendfile')->with($file)->once()->andReturn(null); |
| 33 | + $swooleResponse->shouldReceive('status')->with(Mockery::any())->once()->andReturn(200); |
| 34 | + |
| 35 | + $response = new Response($swooleResponse); |
| 36 | + $response = $response->withBody(new SwooleFileStream($file)); |
| 37 | + |
| 38 | + $this->assertSame(null, $response->send()); |
| 39 | + } |
| 40 | + |
| 41 | + public function testSwooleStream() |
| 42 | + { |
| 43 | + $swooleResponse = Mockery::mock(SwooleResponse::class); |
| 44 | + $content = '{"id":1}'; |
| 45 | + $swooleResponse->shouldReceive('end')->with($content)->once()->andReturn(null); |
| 46 | + $swooleResponse->shouldReceive('status')->with(Mockery::any())->once()->andReturn(200); |
| 47 | + $swooleResponse->shouldReceive('header')->with('TOKEN', 'xxx')->once()->andReturn(null); |
| 48 | + |
| 49 | + $response = new Response($swooleResponse); |
| 50 | + $response = $response->withBody(new SwooleStream($content))->withHeader('TOKEN', 'xxx'); |
| 51 | + |
| 52 | + $this->assertSame(null, $response->send()); |
| 53 | + } |
| 54 | +} |
0 commit comments