mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 07:35:34 +08:00
Merge pull request #107 from zhamao-robot/optimize-phpunit
PHPUnit 及部分结构优化
This commit is contained in:
@@ -2,12 +2,25 @@
|
||||
<?php
|
||||
/** For Swoole coroutine tests */
|
||||
|
||||
// 如果改成 true,则会在终端显示所有炸毛框架的 Log
|
||||
const ZM_TEST_LOG_DEBUG = false;
|
||||
|
||||
use Swoole\Coroutine;
|
||||
use ZM\Annotation\Swoole\OnStart;
|
||||
use ZM\Command\RunServerCommand;
|
||||
use ZM\Console\Console;
|
||||
use ZM\ConsoleApplication;
|
||||
use ZM\Event\EventManager;
|
||||
use ZM\Framework;
|
||||
use ZM\Store\ZMAtomic;
|
||||
use ZM\Utils\ZMUtil;
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
|
||||
co::set([
|
||||
// 用于删除无用信息
|
||||
require $root . '/ext/lib/Version.php';
|
||||
|
||||
Coroutine::set([
|
||||
'log_level' => SWOOLE_LOG_INFO,
|
||||
'trace_flags' => 0,
|
||||
]);
|
||||
@@ -40,9 +53,11 @@ if (!ini_get('date.timezone')) {
|
||||
}
|
||||
|
||||
require $root . '/vendor/autoload.php';
|
||||
// 用于删除 PHPUnit 自带的无用信息
|
||||
require $root . '/ext/lib/Printer.php';
|
||||
|
||||
const ZM_VERSION_ID = \ZM\ConsoleApplication::VERSION_ID;
|
||||
const ZM_VERSION = \ZM\ConsoleApplication::VERSION;
|
||||
const ZM_VERSION_ID = ConsoleApplication::VERSION_ID;
|
||||
const ZM_VERSION = ConsoleApplication::VERSION;
|
||||
|
||||
// 模拟define
|
||||
const ZM_PROCESS_MASTER = 1;
|
||||
@@ -64,19 +79,23 @@ $options['disable-safe-exit'] = true;
|
||||
$options['worker-num'] = 1;
|
||||
$options['private-mode'] = true;
|
||||
$options['log-error'] = true;
|
||||
\ZM\Console\Console::setLevel(0);
|
||||
$framework = new \ZM\Framework($options);
|
||||
$start = new \ZM\Annotation\Swoole\OnStart();
|
||||
|
||||
$_SERVER['argv'][] = '--printer';
|
||||
$_SERVER['argv'][] = 'SEPrinter';
|
||||
|
||||
Console::setLevel(0);
|
||||
$framework = new Framework($options);
|
||||
$start = new OnStart();
|
||||
$start->method = function () {
|
||||
try {
|
||||
\ZM\Console\Console::setLevel(4);
|
||||
Console::setLevel(ZM_TEST_LOG_DEBUG ? 4 : 0);
|
||||
$retcode = PHPUnit\TextUI\Command::main(false);
|
||||
\ZM\Console\Console::setLevel(0);
|
||||
} finally {
|
||||
\ZM\Utils\ZMUtil::stop(($retcode ?? 1) !== 0);
|
||||
Console::setLevel(0);
|
||||
ZMUtil::stop(($retcode ?? 1) !== 0);
|
||||
}
|
||||
};
|
||||
\ZM\Event\EventManager::addEvent(\ZM\Annotation\Swoole\OnStart::class, $start);
|
||||
EventManager::addEvent(OnStart::class, $start);
|
||||
|
||||
$framework->start();
|
||||
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
{
|
||||
"name": "jerry",
|
||||
"email": "admin@zhamao.me"
|
||||
},
|
||||
{
|
||||
"name": "sunxyw",
|
||||
"email": "dev@sunxyw.xyz"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
@@ -18,11 +22,11 @@
|
||||
"koriym/attributes": "^1.0",
|
||||
"psr/container": "^2.0",
|
||||
"psy/psysh": "^0.11.2",
|
||||
"symfony/console": "~5.0 || ~4.0 || ~3.0",
|
||||
"symfony/console": "~6.0 || ~5.0 || ~4.0 || ~3.0",
|
||||
"symfony/polyfill-ctype": "^1.19",
|
||||
"symfony/polyfill-mbstring": "^1.19",
|
||||
"symfony/polyfill-php80": "^1.16",
|
||||
"symfony/routing": "~5.0 || ~4.0 || ~3.0",
|
||||
"symfony/routing": "~6.0 || ~5.0 || ~4.0 || ~3.0",
|
||||
"zhamao/config": "^1.0",
|
||||
"zhamao/connection-manager": "^1.0",
|
||||
"zhamao/console": "^1.0",
|
||||
|
||||
@@ -4,6 +4,35 @@
|
||||
|
||||
同时此处将只使用 build 版本号进行区分。
|
||||
|
||||
## build 467 (2022-4-29)
|
||||
|
||||
- 优化 `@RequestMapping` 注解事件的方法返回值处理,支持数组和字符串(数组自动转为 JSON 格式)
|
||||
|
||||
## build 466 (2022-4-29)
|
||||
|
||||
- 优化容器支持无名顺序参数的调用
|
||||
- 优化静态路由,支持 64 以上长度的路由
|
||||
|
||||
## build 464 (2022-4-28)
|
||||
|
||||
- 重构全局方法 `match_pattern()`,优化性能以及解决部分字符串不能匹配的 Bug
|
||||
|
||||
## build 463 (2022-4-16)
|
||||
|
||||
- 新增链式调用全局方法 `chain()`
|
||||
- 新增函数执行时间工具全局函数 `stopwatch()`
|
||||
|
||||
## build 462 (2022-4-15)
|
||||
|
||||
- 新增依赖注入、容器支持,目前对 Swoole 事件、机器人事件均支持使用依赖注入
|
||||
- 新增全局容器方法 `container()`、`resolve()`、`app()`,用于获取容器参数等
|
||||
- 新增相关容器测试
|
||||
|
||||
## build 461 (2022-4-11)
|
||||
|
||||
- 新增 AllBotsProxy、AllGroupsProxy 代理类,支持批量发送机器人动作
|
||||
- 新增全局函数 `implode_when_necessary()`,用于将可能为数组的参数转换为字符串
|
||||
|
||||
## build 460 (2022-4-3)
|
||||
|
||||
- 优化代码到 phpstan-level-4
|
||||
|
||||
27
ext/lib/Printer.php
Normal file
27
ext/lib/Printer.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php /** @noinspection ALL */
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
if (($v = version_compare('9.0', \Composer\InstalledVersions::getVersionRanges('phpunit/phpunit'))) > 0) {
|
||||
eval(<<<ERT
|
||||
class SEPrinter extends \PHPUnit\TextUI\ResultPrinter
|
||||
{
|
||||
public function write(string \$buffer): void
|
||||
{
|
||||
echo str_replace(['#StandWith', 'Ukraine'], '', \$buffer);
|
||||
}
|
||||
}
|
||||
ERT
|
||||
);
|
||||
} elseif ($v < 0) {
|
||||
eval (<<<ERT2
|
||||
class SEPrinter extends \PHPUnit\TextUI\DefaultResultPrinter implements \PHPUnit\TextUI\ResultPrinter
|
||||
{
|
||||
public function write(string \$buffer): void
|
||||
{
|
||||
echo str_replace(['#StandWith', 'Ukraine'], '', \$buffer);
|
||||
}
|
||||
}
|
||||
ERT2
|
||||
);
|
||||
}
|
||||
68
ext/lib/Version.php
Normal file
68
ext/lib/Version.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of PHPUnit.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace PHPUnit\Runner;
|
||||
|
||||
use function array_slice;
|
||||
use function explode;
|
||||
use function implode;
|
||||
use function strpos;
|
||||
|
||||
/**
|
||||
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
|
||||
*/
|
||||
final class Version
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private static $pharVersion = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private static $version = '';
|
||||
|
||||
/**
|
||||
* Returns the current version of PHPUnit.
|
||||
*/
|
||||
public static function id(): string
|
||||
{
|
||||
if (self::$pharVersion !== '') {
|
||||
return self::$pharVersion;
|
||||
}
|
||||
|
||||
if (self::$version === '') {
|
||||
$file = __DIR__ . '/../../vendor/phpunit/phpunit/src/Runner/Version.php';
|
||||
preg_match('/\d+.\d+.\d+/', file_get_contents($file), $match);
|
||||
self::$version = $match[0];
|
||||
}
|
||||
|
||||
return self::$version;
|
||||
}
|
||||
|
||||
public static function series(): string
|
||||
{
|
||||
if (strpos(self::id(), '-')) {
|
||||
$version = explode('-', self::id())[0];
|
||||
} else {
|
||||
$version = self::id();
|
||||
}
|
||||
|
||||
return implode('.', array_slice(explode('.', $version), 0, 2));
|
||||
}
|
||||
|
||||
public static function getVersionString(): string
|
||||
{
|
||||
return 'PHPUnit ' . self::id();
|
||||
}
|
||||
}
|
||||
@@ -10,3 +10,4 @@ parameters:
|
||||
- '#Unsafe usage of new static#'
|
||||
dynamicConstantNames:
|
||||
- SWOOLE_VERSION
|
||||
- ZM_TEST_LOG_DEBUG
|
||||
|
||||
@@ -110,8 +110,11 @@ class OnWorkerStart implements SwooleEvent
|
||||
}
|
||||
Console::success('Worker #' . $worker_id . ' started');
|
||||
} catch (Exception $e) {
|
||||
if ($e->getMessage() === 'swoole exit') {
|
||||
return;
|
||||
}
|
||||
Console::error('Worker加载出错!停止服务!');
|
||||
Console::error(zm_internal_errcode('E00030') . $e->getMessage() . "\n" . $e->getTraceAsString());
|
||||
Console::error(zm_internal_errcode('E00030') . 'Uncaught ' . get_class($e) . ': ' . $e->getMessage() . "\n" . $e->getTraceAsString());
|
||||
Process::kill($server->master_pid, SIGTERM);
|
||||
return;
|
||||
} catch (Error $e) {
|
||||
|
||||
@@ -521,7 +521,9 @@ class Framework
|
||||
self::$loaded_files = get_included_files();
|
||||
self::$server->start();
|
||||
zm_atomic('server_is_stopped')->set(1);
|
||||
Console::log('zhamao-framework is stopped.');
|
||||
if (!self::$argv['private-mode']) {
|
||||
Console::log('zhamao-framework is stopped.');
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
exit(zm_internal_errcode('E00011') . 'Framework has an uncaught ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL);
|
||||
}
|
||||
|
||||
@@ -254,7 +254,8 @@ function ctx(): ContextInterface
|
||||
return ZMBuf::$context_class[$cid] ?? (ZMBuf::$context_class[$cid] = new $c_class($cid));
|
||||
}
|
||||
}
|
||||
throw new ZMKnownException(zm_internal_errcode('E00072') . 'Unable to find context environment');
|
||||
Console::warning('当前环境不是协程环境,将返回独立的非协程的容器');
|
||||
return ZMBuf::$context_class[$cid] ?? (ZMBuf::$context_class[$cid] = new $c_class($cid));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -71,4 +71,9 @@ class GlobalFunctionsTest extends TestCase
|
||||
'escaped' => ['foo\\*bar', 'foo*bar', true],
|
||||
];
|
||||
}
|
||||
|
||||
public function testZmExec(): void
|
||||
{
|
||||
$this->assertEquals(['code' => 0, 'signal' => 0, 'output' => "foo\n"], zm_exec('echo foo'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user