diff --git a/bin/phpunit-swoole b/bin/phpunit-swoole index bd79ba74..0e2d1003 100644 --- a/bin/phpunit-swoole +++ b/bin/phpunit-swoole @@ -59,6 +59,7 @@ require PHPUNIT_COMPOSER_INSTALL; $starttime = microtime(true); go(function () { try { + require_once __DIR__.'/../test/bootstrap.php'; PHPUnit\TextUI\Command::main(false); } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; diff --git a/docs/update/build-update.md b/docs/update/build-update.md index c218ba92..4f5cf685 100644 --- a/docs/update/build-update.md +++ b/docs/update/build-update.md @@ -4,6 +4,12 @@ 同时此处将只使用 build 版本号进行区分。 +## build 418 (2021-9-10) + +- 修复 ZMAtomic 在 test 环境下的 bug +- 修复 MessageUtil 的报错 +- + ## build 417 (2021-8-29) - 新增 AnnotationException,统一框架内部的抛出异常的类型 diff --git a/src/ZM/ConsoleApplication.php b/src/ZM/ConsoleApplication.php index 74ed0d92..0f48093a 100644 --- a/src/ZM/ConsoleApplication.php +++ b/src/ZM/ConsoleApplication.php @@ -28,7 +28,7 @@ class ConsoleApplication extends Application { private static $obj = null; - const VERSION_ID = 417; + const VERSION_ID = 418; const VERSION = "2.5.2"; /** diff --git a/src/ZM/Store/ZMAtomic.php b/src/ZM/Store/ZMAtomic.php index ba3abce7..12a133a4 100644 --- a/src/ZM/Store/ZMAtomic.php +++ b/src/ZM/Store/ZMAtomic.php @@ -32,6 +32,7 @@ class ZMAtomic self::$atomics["wait_msg_id"] = new Atomic(0); self::$atomics["_event_id"] = new Atomic(0); self::$atomics["server_is_stopped"] = new Atomic(0); + if (!defined("ZM_WORKER_NUM")) define("ZM_WORKER_NUM", 1); for($i = 0; $i < ZM_WORKER_NUM; ++$i) { self::$atomics["_#worker_".$i] = new Atomic(0); } diff --git a/src/ZM/Utils/MessageUtil.php b/src/ZM/Utils/MessageUtil.php index 7dae8473..e9411e81 100644 --- a/src/ZM/Utils/MessageUtil.php +++ b/src/ZM/Utils/MessageUtil.php @@ -23,7 +23,7 @@ class MessageUtil */ public static function downloadCQImage($msg, $path = null) { $path = $path ?? DataProvider::getDataFolder() . "images/"; - if (!is_dir($path)) mkdir($path); + if (!is_dir($path)) @mkdir($path); $path = realpath($path); if ($path === false) { Console::warning(zm_internal_errcode("E00059") . "指定的路径错误不存在!"); diff --git a/test/ZM/Utils/MessageUtilTest.php b/test/ZM/Utils/MessageUtilTest.php new file mode 100644 index 00000000..537bcb18 --- /dev/null +++ b/test/ZM/Utils/MessageUtilTest.php @@ -0,0 +1,63 @@ +opcode = WEBSOCKET_OPCODE_PONG; + } + + public function testGetImageCQFromLocal() { + file_put_contents("/tmp/a.jpg", "fake photo"); + $this->assertEquals( + MessageUtil::getImageCQFromLocal("/tmp/a.jpg"), + "[CQ:image,file=base64://".base64_encode("fake photo")."]" + ); + } + + public function testSplitCommand() { + $msg_sample_1 = "你好啊 233\n\nhello"; + $msg_sample_2 = ""; + $this->assertCount(3, MessageUtil::splitCommand($msg_sample_1)); + $this->assertCount(1, MessageUtil::splitCommand($msg_sample_2)); + } + + public function testIsAtMe() { + $this->assertTrue(MessageUtil::isAtMe("[CQ:at,qq=123]", 123)); + $this->assertFalse(MessageUtil::isAtMe("[CQ:at,qq=]", 0)); + } + + public function testDownloadCQImage() { + if (file_exists(WORKING_DIR."/zm_data/images/abc.jpg")) + unlink(WORKING_DIR."/zm_data/images/abc.jpg"); + ob_start(); + $msg = "[CQ:image,file=abc.jpg,url=https://zhamao.xin/file/hello.jpg]"; + $result = MessageUtil::downloadCQImage($msg, "/home/jerry/fweewfwwef/wef"); + $this->assertFalse($result); + $this->assertStringContainsString("E00059", ob_get_clean()); + $result = MessageUtil::downloadCQImage($msg); + $this->assertIsArray($result); + $this->assertFileExists(WORKING_DIR."/zm_data/images/abc.jpg"); + $result = MessageUtil::downloadCQImage($msg.$msg); + $this->assertIsArray($result); + $this->assertCount(2, $result); + } + + public function testContainsImage() { + $msg_sample = "hello\n[CQ:imag2]"; + $this->assertFalse(MessageUtil::containsImage($msg_sample)); + $this->assertTrue(MessageUtil::containsImage($msg_sample."[CQ:image,file=123]")); + } + + public function testMatchCommand() { + + } +} diff --git a/test/ZM/Utils/TerminalTest.php b/test/ZM/Utils/TerminalTest.php new file mode 100644 index 00000000..0f1b030f --- /dev/null +++ b/test/ZM/Utils/TerminalTest.php @@ -0,0 +1,27 @@ +assertStringContainsString("debug msg", ob_get_clean()); + } + + public function testBc() { + ob_start(); + Terminal::executeCommand("bc ".base64_encode("echo 'hello';")); + $this->assertStringContainsString("hello", ob_get_clean()); + } +} diff --git a/test/ZM/Utils/ZMUtilTest.php b/test/ZM/Utils/ZMUtilTest.php index f1acd36a..c3792bf9 100644 --- a/test/ZM/Utils/ZMUtilTest.php +++ b/test/ZM/Utils/ZMUtilTest.php @@ -16,4 +16,9 @@ class ZMUtilTest extends TestCase $this->assertContains(TimerMiddleware::class, ZMUtil::getClassesPsr4(DataProvider::getSourceRootDir()."/src/Module", "Module")); $this->assertContains(Framework::class, ZMUtil::getClassesPsr4(DataProvider::getSourceRootDir()."/src/ZM", "ZM")); } + + public function testGetModInstance() { + $class = Hello::class; + $this->assertTrue(ZMUtil::getModInstance($class) instanceof Hello); + } } \ No newline at end of file diff --git a/test/bootstrap.php b/test/bootstrap.php index 136d2f1a..7b0bb7a0 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -3,11 +3,29 @@ * @since 2.5 */ +use ZM\Config\ZMConfig; +use ZM\Console\Console; +use ZM\Store\LightCacheInside; +use ZM\Store\ZMAtomic; +use ZM\Utils\DataProvider; +use ZM\Utils\Terminal; + set_coroutine_params([]); // 模拟define -chdir(__DIR__.'/../'); +chdir(__DIR__ . '/../'); define("WORKING_DIR", getcwd()); define("SOURCE_ROOT_DIR", WORKING_DIR); +define("ZM_DATA", WORKING_DIR . "/zm_data/"); define("LOAD_MODE", 0); define("FRAMEWORK_ROOT_DIR", realpath(__DIR__ . "/../")); + +ZMConfig::setDirectory(WORKING_DIR."/config/"); +ZMConfig::setEnv(""); +if (ZMConfig::get("global") === false) { + die (zm_internal_errcode("E00007") . "Global config load failed: " . ZMConfig::$last_error . "\nError path: " . DataProvider::getSourceRootDir() . "\nPlease init first!\nSee: https://github.com/zhamao-robot/zhamao-framework/issues/37\n"); +} +LightCacheInside::init(); +ZMAtomic::init(); +Terminal::init(); +Console::setLevel(4); \ No newline at end of file