PHP-TEST/tests/Test.php
james 70b28111d7
Some checks failed
Run Unit Tests / test (push) Failing after 9s
更新 tests/Test.php
2025-06-03 15:15:18 +08:00

50 lines
1.2 KiB
PHP

<?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);
}
}