update to build 418

This commit is contained in:
2021-09-10 11:24:32 +08:00
parent e2f49968b3
commit 2b4d308783
9 changed files with 124 additions and 3 deletions

View File

@@ -0,0 +1,63 @@
<?php
namespace ZM\Utils;
use PHPUnit\Framework\TestCase;
use Swoole\WebSocket\Frame;
use ZM\Requests\ZMRequest;
class MessageUtilTest extends TestCase
{
public function setUp(): void {
ZMRequest::websocket();
$a = new Frame();
$a->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() {
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace ZM\Utils;
use PHPUnit\Framework\TestCase;
use ZM\Config\ZMConfig;
use ZM\Console\Console;
use ZM\Store\LightCacheInside;
use ZM\Store\ZMAtomic;
class TerminalTest extends TestCase
{
public function setUp(): void {
}
public function testExecuteCommand() {
ob_start();
Terminal::executeCommand("logtest");
$this->assertStringContainsString("debug msg", ob_get_clean());
}
public function testBc() {
ob_start();
Terminal::executeCommand("bc ".base64_encode("echo 'hello';"));
$this->assertStringContainsString("hello", ob_get_clean());
}
}

View File

@@ -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);
}
}

View File

@@ -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);