1. Go to this page and download the library: Download tourze/phpunit-workerman library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tourze / phpunit-workerman example snippets
use Tourze\PHPUnitWorkerman\Core\WorkermanTestCase;
use Workerman\Worker;
class MyWorkerTest extends WorkermanTestCase
{
public function testBasicWorker(): void
{
// 创建测试 Worker
$worker = $this->createWorker('tcp://127.0.0.1:8080');
$messageReceived = false;
$worker->onMessage = function ($connection, $data) use (&$messageReceived) {
$messageReceived = true;
$connection->send('echo: ' . $data);
};
// 启动 Worker
$this->startWorker($worker);
// 模拟发送数据
$this->sendDataToWorker($worker, 'hello');
// 验证结果
$this->assertTrue($messageReceived);
}
}
use Tourze\PHPUnitWorkerman\Core\AsyncTestCase;
use Workerman\Timer;
class TimerTest extends AsyncTestCase
{
public function testTimer(): void
{
$executed = false;
// 创建定时器
Timer::add(0.5, function () use (&$executed) {
$executed = true;
}, [], false);
// 快进时间
$this->advanceTime(0.6);
// 验证定时器执行
$this->assertTrue($executed);
}
public function testRepeatingTimer(): void
{
$count = 0;
Timer::add(0.1, function () use (&$count) {
$count++;
}, [], true);
// 快进 1 秒,应该执行 10 次
$this->advanceTime(1.0);
$this->assertEquals(10, $count);
}
}
use Tourze\PHPUnitWorkerman\Core\WorkermanTestCase;
use Workerman\Connection\TcpConnection;
class ConnectionTest extends WorkermanTestCase
{
public function testConnection(): void
{
$worker = $this->createWorker();
$events = [];
$worker->onConnect = function ($connection) use (&$events) {
$events[] = 'connected';
};
$worker->onMessage = function ($connection, $data) use (&$events) {
$events[] = "message: $data";
};
$this->startWorker($worker);
// 模拟连接
$connection = $this->createMockConnection($worker);
$worker->onConnect($connection);
// 模拟消息
$this->sendDataToWorker($worker, 'test');
$this->assertEquals(['connected', 'message: test'], $events);
}
}
abstract class WorkermanTestCase extends TestCase
{
// 创建测试 Worker
protected function createWorker(string $socketName = ''): Worker;
// 启动 Worker
protected function startWorker(Worker $worker): void;
// 模拟发送数据到 Worker
protected function sendDataToWorker(Worker $worker, string $data): void;
// 时间快进
protected function fastForward(float $seconds): void;
// 等待条件满足
protected function waitFor(callable $condition, float $timeout = 5.0): void;
}
abstract class AsyncTestCase extends TestCase
{
// 时间推进
protected function advanceTime(float $seconds): void;
// 等待直到条件满足
protected function waitUntil(callable $condition, float $timeout = 5.0): void;
// 运行异步操作
protected function runAsync(callable $asyncOperation, float $timeout = 5.0);
// 并行运行多个异步操作
protected function runAsyncParallel(array $operations, float $timeout = 5.0): array;
}
class MockEventLoop implements EventInterface
{
// 时间快进
public function fastForward(float $seconds): void;
// 触发事件
public function triggerRead($stream): void;
public function triggerWrite($stream): void;
public function triggerSignal(int $signal): void;
// 状态查询
public function getCurrentTime(): float;
public function getTimers(): array;
public function hasReadEvents(): bool;
}
trait TimeControl
{
protected function fastForward(float $seconds): void;
protected function fastForwardToNextTimer(): bool;
protected function executeAllPendingTimers(float $maxTime = 3600.0): int;
protected function setCurrentTime(float $time): void;
protected function getCurrentTime(): float;
}
trait AsyncAssertions
{
protected function assertAsyncCallbackCalled(callable $trigger, float $timeout = 5.0): void;
protected function assertAsyncCallbackCalledTimes(int $expectedCount, callable $trigger): void;
protected function assertAsyncResult($expectedValue, callable $trigger): void;
protected function waitForCondition(callable $condition, float $timeout = 5.0): void;
}
trait ConnectionMocking
{
protected function createMockConnection(?Worker $worker = null, string $remoteAddress = '127.0.0.1:12345'): TcpConnection;
protected function mockConnectionSend(TcpConnection $connection, string $data): bool;
protected function mockConnectionReceive(TcpConnection $connection, string $data): void;
protected function mockConnectionClose(TcpConnection $connection): void;
protected function assertConnectionStatus(TcpConnection $connection, int $expectedStatus): void;
}
public function testComplexAsyncFlow(): void
{
$results = [];
// 模拟复杂异步操作链
$this->runAsync(function ($resolve, $reject) use (&$results) {
Timer::add(0.1, function () use (&$results, $resolve) {
$results[] = 'step1';
Timer::add(0.2, function () use (&$results, $resolve) {
$results[] = 'step2';
Timer::add(0.3, function () use (&$results, $resolve) {
$results[] = 'step3';
$resolve($results);
}, [], false);
}, [], false);
}, [], false);
});
$this->assertEquals(['step1', 'step2', 'step3'], $results);
}