mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-17 20:54:52 +08:00
add phpunit windows support
This commit is contained in:
parent
2e61e2a366
commit
15383a6b92
7
.github/workflows/integration-test.yml
vendored
7
.github/workflows/integration-test.yml
vendored
@ -26,7 +26,7 @@ jobs:
|
||||
runs-on: ${{ matrix.operating-system }}
|
||||
strategy:
|
||||
matrix:
|
||||
operating-system: [ "ubuntu-latest", "macos-latest" ]
|
||||
operating-system: [ "ubuntu-latest", "macos-latest", "windows-latest" ]
|
||||
php-versions: [ "7.4", "8.0", "8.1" ]
|
||||
steps:
|
||||
- name: Checkout code
|
||||
@ -36,7 +36,7 @@ jobs:
|
||||
uses: "shivammathur/setup-php@v2"
|
||||
with:
|
||||
php-version: ${{ matrix.php-versions }}
|
||||
extensions: swoole, posix, json
|
||||
extensions: swoole, posix, json, mbstring
|
||||
env:
|
||||
SWOOLE_CONFIGURE_OPTS: --enable-openssl
|
||||
|
||||
@ -63,6 +63,9 @@ jobs:
|
||||
- name: Run Static Analysis
|
||||
run: "composer analyse"
|
||||
|
||||
- name: Run PHPUnit
|
||||
run: "composer test"
|
||||
|
||||
cs-check:
|
||||
name: PHP CS Fixer Check
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
@ -7,38 +7,49 @@ const ZM_TEST_LOG_DEBUG = false;
|
||||
|
||||
use OneBot\Driver\Event\Process\WorkerStartEvent;
|
||||
use PHPUnit\TextUI\Command;
|
||||
use Swoole\Atomic;
|
||||
use ZM\Command\Server\ServerStartCommand;
|
||||
use ZM\Event\EventProvider;
|
||||
use ZM\Framework;
|
||||
use ZM\Logger\ConsoleLogger;
|
||||
use ZM\Store\MockAtomic;
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
|
||||
require $root . '/vendor/autoload.php';
|
||||
// 引入自动加载
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
// 声明一个全局的原子计数,用于保存整个进程的退出状态码
|
||||
global $_swoole_atomic;
|
||||
$_swoole_atomic = new \Swoole\Atomic();
|
||||
// Windows 没有 Swoole,所以就使用了一个假的原子计数器,反正 Windows 只有单进程
|
||||
$_swoole_atomic = class_exists('\\Swoole\\Atomic') ? new Atomic() : new MockAtomic();
|
||||
|
||||
ob_logger_register(new ConsoleLogger('error'));
|
||||
// 注册 Logger 等级
|
||||
ob_logger_register(new ConsoleLogger(ZM_TEST_LOG_DEBUG ? 'debug' : 'error'));
|
||||
|
||||
// 使用框架自身的 EventProvider
|
||||
global $ob_event_provider;
|
||||
$ob_event_provider = new \ZM\Event\EventProvider();
|
||||
$ob_event_provider = new EventProvider();
|
||||
|
||||
// 注册一个最低级别的 WorkerStart 事件,用于在框架的事件初始化之后开始运行 PHPUnit
|
||||
ob_event_provider()->addEventListener(WorkerStartEvent::getName(), function () {
|
||||
try {
|
||||
// 不退出,而是返回 code
|
||||
$retcode = Command::main(false);
|
||||
} finally {
|
||||
global $_swoole_atomic;
|
||||
$_swoole_atomic->set($retcode ?? 0);
|
||||
if (DIRECTORY_SEPARATOR === '\\') {
|
||||
exit($retcode ?? 0);
|
||||
}
|
||||
Framework::getInstance()->stop();
|
||||
}
|
||||
}, 1);
|
||||
|
||||
try {
|
||||
$options = ServerStartCommand::exportOptionArray();
|
||||
$options['driver'] = 'swoole';
|
||||
$options['driver'] = DIRECTORY_SEPARATOR === '/' ? 'swoole' : 'workerman';
|
||||
$options['worker-num'] = 1;
|
||||
$options['private-mode'] = true;
|
||||
(new Framework($options))->init()->start();
|
||||
|
||||
exit($_swoole_atomic->get());
|
||||
} catch (Throwable $e) {
|
||||
echo $e->getMessage() . PHP_EOL;
|
||||
|
||||
3
bin/phpunit-zm.bat
Normal file
3
bin/phpunit-zm.bat
Normal file
@ -0,0 +1,3 @@
|
||||
@echo off
|
||||
|
||||
php %~dp0phpunit-zm %*
|
||||
@ -97,7 +97,9 @@ class WorkerEventListener
|
||||
public function onWorkerStop999()
|
||||
{
|
||||
logger()->debug('Worker #' . ProcessManager::getProcessId() . ' stopping');
|
||||
ProcessStateManager::removeProcessState(ZM_PROCESS_WORKER, ProcessManager::getProcessId());
|
||||
if (DIRECTORY_SEPARATOR !== '\\') {
|
||||
ProcessStateManager::removeProcessState(ZM_PROCESS_WORKER, ProcessManager::getProcessId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -12,7 +12,7 @@ class ProcessStateManager
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $process_mode;
|
||||
public static array $process_mode = [];
|
||||
|
||||
/**
|
||||
* @param null|int|string $id_or_name
|
||||
|
||||
20
src/ZM/Store/MockAtomic.php
Normal file
20
src/ZM/Store/MockAtomic.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Store;
|
||||
|
||||
class MockAtomic
|
||||
{
|
||||
private int $num = 0;
|
||||
|
||||
public function set(int $num)
|
||||
{
|
||||
$this->num = $num;
|
||||
}
|
||||
|
||||
public function get(): int
|
||||
{
|
||||
return $this->num;
|
||||
}
|
||||
}
|
||||
37
tests/ZM/Middleware/PipelineTest.php
Normal file
37
tests/ZM/Middleware/PipelineTest.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\ZM\Middleware;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ZM\Logger\ConsoleLogger;
|
||||
use ZM\Middleware\Pipeline;
|
||||
use ZM\Middleware\TimerMiddleware;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class PipelineTest extends TestCase
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
ob_logger_register(new ConsoleLogger('debug'));
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
ob_logger_register(new ConsoleLogger('error'));
|
||||
}
|
||||
|
||||
public function testPipeline()
|
||||
{
|
||||
$pipe = new Pipeline();
|
||||
$a = $pipe->send('APP')
|
||||
->through([TimerMiddleware::class])
|
||||
->then(function (string $value) {
|
||||
return $value;
|
||||
});
|
||||
$this->assertEquals('APP', $a);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user