53 lines
1.3 KiB
PHP
53 lines
1.3 KiB
PHP
<?php
|
|
|
|
// 手动加载类文件
|
|
require_once __DIR__ . '/../src/libs/MyWebSocketHandler.php';
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use App\MyWebSocketHandler;
|
|
use Workerman\Connection\TcpConnection;
|
|
use Workerman\Worker;
|
|
|
|
class MyWebSocketHandlerTest extends TestCase
|
|
{
|
|
private $handler;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->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);
|
|
}
|
|
} |