| Code Coverage
| Lines | Functions and Methods | Classes and Traits
Total | 76.00% covered (warning) | 76.00% | 19 / 25 | 50.00% covered (danger) | 50.00% | 2 / 4 | CRAP |  | n/a | 0 / 0
onWebSocketConnect | 100.00% covered (success) | 100.00% | 1 / 1 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
onWebSocketMessage | 81.82% covered (warning) | 81.82% | 9 / 11 | 0.00% covered (danger) | 0.00% | 0 / 1 | 3.05 |
onWebSocketClose | 100.00% covered (success) | 100.00% | 1 / 1 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
onWorkerStopping | 33.33% covered (danger) | 33.33% | 2 / 6 | 0.00% covered (danger) | 0.00% | 0 / 1 | 1.30 |

1 | <?php
2 |
3 | use Workerman\Worker;
4 | use app\actions\GetPHPInfo;
5 |
6 | require_once __DIR__ . '/../vendor/autoload.php';
7 |
8 | // 启动 Xdebug 覆盖率收集
9 | xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
10 |
11 | // 可选测试
12 | // $test = new GetPHPInfo();
13 | // $result = $test->handle(['sel' => true]);
14 | // echo "Getphpinfo handle 返回: " . json_encode($result) . "\n";
15 |
16 | // 创建一个 Websocket server
17 | $ws_worker = new Worker('websocket://0.0.0.0:2346');
18 | $ws_worker->onConnect = 'onWebSocketConnect';
19 | $ws_worker->onMessage = 'onWebSocketMessage';
20 | $ws_worker->onClose = 'onWebSocketClose';
21 | $ws_worker->onWorkerStop = 'onWorkerStopping';
22 |
23 | Worker::runAll();
24 |
25 | function onWebSocketConnect($connection)
26 | {
27 | echo "New connection\n";
28 | }
29 |
30 | function onWebSocketMessage($connection, $data)
31 | {
32 | $request = json_decode($data, true);
33 |
34 | if (isset($request['action'])) {
35 | $action = $request['action'];
36 | $className = '\app\actions\\' . ucfirst($action);
37 |
38 | if (class_exists($className)) {
39 | $instance = new $className();
40 | $param = $request['param'] ?? [];
41 | $response = $instance->handle($param);
42 | $connection->send(json_encode($response));
43 | } else {
44 | $connection->send(json_encode(['error' => 'Class not found']));
45 | }
46 | } else {
47 | $connection->send(json_encode(['error' => 'Invalid request']));
48 | }
49 | }
50 |
51 | function onWebSocketClose($connection)
52 | {
53 | echo "Connection closed\n";
54 | }
55 |
56 | function onWorkerStopping()
57 | {
58 | echo "\nWorker is stopping...\n";
59 |
60 | // 停止 Xdebug 并保存结果
61 | $coverage = xdebug_get_code_coverage();
62 | xdebug_stop_code_coverage();
63 |
64 | // 将覆盖率数据写入文件
65 | $filename = __DIR__ . '/coverage.json';
66 | file_put_contents($filename, json_encode($coverage, JSON_PRETTY_PRINT));
67 | echo "Coverage saved to $filename\n";
68 | }