|
| 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://hyperf.wiki |
| 9 | + |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | +namespace HyperfTest\WebSocketServer; |
| 13 | + |
| 14 | +use Hyperf\Contract\StdoutLoggerInterface; |
| 15 | +use Hyperf\Dispatcher\HttpDispatcher; |
| 16 | +use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher; |
| 17 | +use Hyperf\HttpServer\ResponseEmitter; |
| 18 | +use Hyperf\Utils\ApplicationContext; |
| 19 | +use Hyperf\Utils\Coroutine; |
| 20 | +use Hyperf\Utils\Reflection\ClassInvoker; |
| 21 | +use Hyperf\Utils\Waiter; |
| 22 | +use Hyperf\WebSocketServer\Server; |
| 23 | +use HyperfTest\WebSocketServer\Stub\WebSocketStub; |
| 24 | +use Mockery; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | +use Psr\Container\ContainerInterface; |
| 27 | +use Swoole\Http\Request as SwooleRequest; |
| 28 | +use Swoole\Http\Response as SwooleResponse; |
| 29 | + |
| 30 | +/** |
| 31 | + * @internal |
| 32 | + * @coversNothing |
| 33 | + */ |
| 34 | +class ServerTest extends TestCase |
| 35 | +{ |
| 36 | + protected function tearDown(): void |
| 37 | + { |
| 38 | + Mockery::close(); |
| 39 | + } |
| 40 | + |
| 41 | + public function testDeferOnOpenInCoroutineStyleServer() |
| 42 | + { |
| 43 | + $container = Mockery::mock(ContainerInterface::class); |
| 44 | + ApplicationContext::setContainer($container); |
| 45 | + $container->shouldReceive('get')->with(WebSocketStub::class)->andReturn(new WebSocketStub()); |
| 46 | + $container->shouldReceive('get')->with(Waiter::class)->andReturn(new Waiter()); |
| 47 | + |
| 48 | + $server = new Server( |
| 49 | + $container, |
| 50 | + Mockery::mock(HttpDispatcher::class), |
| 51 | + Mockery::mock(ExceptionHandlerDispatcher::class), |
| 52 | + Mockery::mock(ResponseEmitter::class), |
| 53 | + Mockery::mock(StdoutLoggerInterface::class), |
| 54 | + ); |
| 55 | + |
| 56 | + $server = new ClassInvoker($server); |
| 57 | + $server->deferOnOpen(new SwooleRequest(), WebSocketStub::class, new SwooleResponse()); |
| 58 | + $this->assertNotEquals(Coroutine::id(), WebSocketStub::$coroutineId); |
| 59 | + $this->assertFalse(\Swoole\Coroutine::exists(WebSocketStub::$coroutineId)); |
| 60 | + } |
| 61 | +} |
0 commit comments