handler = new MyWebSocketHandler(); } public function testOnConnect() { $connection = $this->createMock(TcpConnection::class); $worker = $this->createMock(Worker::class); // 使用 ob_start + ob_get_clean 捕获输出 ob_start(); $this->handler->onConnect($connection); $output = ob_get_clean(); $this->assertStringContainsString('New connection', $output); } public function testOnMessage() { $connection = $this->createMock(TcpConnection::class); $connection->expects($this->once()) ->method('send') ->with($this->equalTo('Hello world')); $this->handler->onMessage($connection, 'world'); } public function testOnClose() { $connection = $this->createMock(TcpConnection::class); ob_start(); $this->handler->onClose($connection); $output = ob_get_clean(); $this->assertStringContainsString('Connection closed', $output); } }