From fd4e4be3a807036d74aeed9ec5840e7705f06805 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 30 Apr 2022 13:33:38 +0800 Subject: [PATCH 1/8] try to remove irrelevant information for phpunit --- bin/phpunit-swoole | 40 +++++++++++++++++++------- ext/lib/Printer.php | 27 ++++++++++++++++++ ext/lib/Version.php | 69 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 ext/lib/Printer.php create mode 100644 ext/lib/Version.php diff --git a/bin/phpunit-swoole b/bin/phpunit-swoole index 926fcf21..f25ce2c2 100755 --- a/bin/phpunit-swoole +++ b/bin/phpunit-swoole @@ -2,12 +2,25 @@ 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,24 @@ $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); $retcode = PHPUnit\TextUI\Command::main(false); - \ZM\Console\Console::setLevel(0); + 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(); diff --git a/ext/lib/Printer.php b/ext/lib/Printer.php new file mode 100644 index 00000000..15705aa4 --- /dev/null +++ b/ext/lib/Printer.php @@ -0,0 +1,27 @@ + 0) { + eval(<< + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PHPUnit\Runner; + +use SebastianBergmann\Version as VersionId; +use function array_slice; +use function dirname; +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 === '') { + $cmd = "grep -Eo '\\d+.\\d+.\\d+' --color=never " . __DIR__ . '/../../vendor/phpunit/phpunit/src/Runner/Version.php'; + self::$version = trim(exec($cmd)); + } + + 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(); + } +} From 8bf5ff4e7f8918a3d62eef5512f5ad8a8c717a28 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 30 Apr 2022 13:34:48 +0800 Subject: [PATCH 2/8] fix swoole exit error dumped --- src/ZM/Event/SwooleEvent/OnWorkerStart.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ZM/Event/SwooleEvent/OnWorkerStart.php b/src/ZM/Event/SwooleEvent/OnWorkerStart.php index d3bc0338..51d36aa3 100644 --- a/src/ZM/Event/SwooleEvent/OnWorkerStart.php +++ b/src/ZM/Event/SwooleEvent/OnWorkerStart.php @@ -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) { From 25d6ec3a9278b11cdebf43d402bc05e0b26f2764 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 30 Apr 2022 13:35:45 +0800 Subject: [PATCH 3/8] private-mode output nothing --- src/ZM/Framework.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ZM/Framework.php b/src/ZM/Framework.php index f970b0a5..68ca535e 100644 --- a/src/ZM/Framework.php +++ b/src/ZM/Framework.php @@ -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); } From 69637cf482cca11599a67b16df248e41d1d8891f Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 30 Apr 2022 13:36:40 +0800 Subject: [PATCH 4/8] update composer.json, docs and tests --- composer.json | 8 ++++++-- docs/update/build-update.md | 29 +++++++++++++++++++++++++++++ tests/ZM/GlobalFunctionsTest.php | 5 +++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 40434382..1fae00e7 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,10 @@ { "name": "jerry", "email": "admin@zhamao.me" + }, + { + "name": "sunxyw", + "email": "xy2496419818@gmail.com" } ], "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", diff --git a/docs/update/build-update.md b/docs/update/build-update.md index 859b7626..43555918 100644 --- a/docs/update/build-update.md +++ b/docs/update/build-update.md @@ -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 diff --git a/tests/ZM/GlobalFunctionsTest.php b/tests/ZM/GlobalFunctionsTest.php index 3e3a8600..88abda5f 100644 --- a/tests/ZM/GlobalFunctionsTest.php +++ b/tests/ZM/GlobalFunctionsTest.php @@ -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')); + } } From d95a5131f6e9174a32c584c3d3e86720ff00f638 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 30 Apr 2022 13:38:15 +0800 Subject: [PATCH 5/8] remove ctx function throw exception --- src/ZM/global_functions.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ZM/global_functions.php b/src/ZM/global_functions.php index ee192f4c..21f1dc28 100644 --- a/src/ZM/global_functions.php +++ b/src/ZM/global_functions.php @@ -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)); } /** From f1c730962bb1542eaee8df3bee39822650f24854 Mon Sep 17 00:00:00 2001 From: Jerry Ma Date: Sat, 30 Apr 2022 17:24:26 +0800 Subject: [PATCH 6/8] Fix ZM_TEST_LOG_DEBUG bug, remove redundant code --- bin/phpunit-swoole | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/phpunit-swoole b/bin/phpunit-swoole index f25ce2c2..36fa3e08 100755 --- a/bin/phpunit-swoole +++ b/bin/phpunit-swoole @@ -88,9 +88,8 @@ $framework = new Framework($options); $start = new OnStart(); $start->method = function () { try { - Console::setLevel(ZM_TEST_LOG_DEBUG); + Console::setLevel(ZM_TEST_LOG_DEBUG ? 4 : 0); $retcode = PHPUnit\TextUI\Command::main(false); - Console::setLevel(0); } finally { Console::setLevel(0); ZMUtil::stop(($retcode ?? 1) !== 0); From 47ce0a8e9ce9898ad9e76691908172e1af7ab55e Mon Sep 17 00:00:00 2001 From: Jerry Ma Date: Sat, 30 Apr 2022 20:34:32 +0800 Subject: [PATCH 7/8] fix action runner stty issue for phpunit (#109) --- ext/lib/Version.php | 7 +++---- phpstan.neon | 1 + 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/lib/Version.php b/ext/lib/Version.php index 9e5f73bc..5cac6b41 100644 --- a/ext/lib/Version.php +++ b/ext/lib/Version.php @@ -12,9 +12,7 @@ declare(strict_types=1); namespace PHPUnit\Runner; -use SebastianBergmann\Version as VersionId; use function array_slice; -use function dirname; use function explode; use function implode; use function strpos; @@ -44,8 +42,9 @@ final class Version } if (self::$version === '') { - $cmd = "grep -Eo '\\d+.\\d+.\\d+' --color=never " . __DIR__ . '/../../vendor/phpunit/phpunit/src/Runner/Version.php'; - self::$version = trim(exec($cmd)); + $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; diff --git a/phpstan.neon b/phpstan.neon index b20bc6c3..25b6cecf 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,3 +10,4 @@ parameters: - '#Unsafe usage of new static#' dynamicConstantNames: - SWOOLE_VERSION + - ZM_TEST_LOG_DEBUG From ec76d2ba7d52b89597a5472613d22fa208e09074 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 30 Apr 2022 20:43:19 +0800 Subject: [PATCH 8/8] change @sunxyw email --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1fae00e7..da298c14 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ }, { "name": "sunxyw", - "email": "xy2496419818@gmail.com" + "email": "dev@sunxyw.xyz" } ], "require": {