zhamao-framework/tests/ZM/Utils/MessageUtilTest.php

173 lines
5.7 KiB
PHP
Raw Normal View History

2022-03-28 20:36:44 +08:00
<?php
declare(strict_types=1);
2022-03-28 23:42:50 +08:00
namespace Tests\ZM\Utils;
2022-03-28 20:36:44 +08:00
use PHPUnit\Framework\TestCase;
use ZM\Annotation\CQ\CQCommand;
2022-03-29 18:15:13 +08:00
use ZM\API\CQ;
2022-03-28 20:36:44 +08:00
use ZM\Event\EventManager;
use ZM\Utils\DataProvider;
use ZM\Utils\MessageUtil;
2022-03-29 02:08:11 +08:00
/**
* @internal
*/
2022-03-28 20:36:44 +08:00
class MessageUtilTest extends TestCase
{
public function testAddShortCommand(): void
{
2022-03-29 02:38:19 +08:00
EventManager::$events[CQCommand::class] = [];
MessageUtil::addShortCommand('test', 'test');
$this->assertCount(1, EventManager::$events[CQCommand::class]);
2022-03-28 20:36:44 +08:00
}
/**
* @dataProvider providerTestContainsImage
*/
2022-03-29 02:38:19 +08:00
public function testContainsImage(string $msg, bool $expected): void
2022-03-28 20:36:44 +08:00
{
$this->assertEquals($expected, MessageUtil::containsImage($msg));
}
public function providerTestContainsImage(): array
{
return [
'empty' => ['', false],
'text only' => ['hello world', false],
'image only' => ['[CQ:image,file=123456.jpg]', true],
'image' => ['hello world![CQ:image,file=123456.jpg]', true],
'two image' => ['hello world![CQ:image,file=123456.jpg][CQ:image,file=123456.jpg]', true],
2022-03-29 02:08:11 +08:00
// 'malformed image' => ['[CQ:image,file=]', false],
2022-03-28 20:36:44 +08:00
];
}
public function testGenerateCommandHelp(): void
{
2022-03-29 18:15:13 +08:00
EventManager::$events[CQCommand::class] = [];
$cmd = new CQCommand('测试命令');
$cmd->class = self::class;
$cmd->method = __FUNCTION__;
EventManager::addEvent(CQCommand::class, $cmd);
$help = MessageUtil::generateCommandHelp();
$this->assertEquals('测试命令:无描述', $help[0]);
2022-03-28 20:36:44 +08:00
}
/**
* @dataProvider providerTestArrayToStr
*/
2022-03-29 02:38:19 +08:00
public function testArrayToStr(array $array, string $expected): void
2022-03-28 20:36:44 +08:00
{
$this->assertEquals($expected, MessageUtil::arrayToStr($array));
}
public function providerTestArrayToStr(): array
{
$tmp = $this->providerTestStrToArray();
$result = [];
foreach ($tmp as $desc => $case) {
$result[$desc] = [$case[1], $case[0]];
}
return $result;
}
public function testMatchCommand(): void
{
// 这里理论上需要覆盖所有条件,但先暂时这样好了
EventManager::$events[CQCommand::class] = [
new CQCommand('测试命令'),
];
$this->assertEquals(true, MessageUtil::matchCommand('测试命令', [
'user_id' => '123456',
'group_id' => '123456',
'message_type' => 'group',
])->status);
}
/**
* @dataProvider providerTestIsAtMe
*/
2022-03-29 02:38:19 +08:00
public function testIsAtMe(string $msg, bool $expected): void
2022-03-28 20:36:44 +08:00
{
$this->assertEquals($expected, MessageUtil::isAtMe($msg, 123456789));
}
public function providerTestIsAtMe(): array
{
return [
'me only' => ['[CQ:at,qq=123456789]', true],
'empty qq' => ['[CQ:at,qq=]', false],
'message behind' => ['[CQ:at,qq=123456789] hello', true],
'message front' => ['hello [CQ:at,qq=123456789]', true],
'message surround' => ['hello [CQ:at,qq=123456789] world', true],
'not at me' => ['hello world', false],
'other' => ['[CQ:at,qq=123456789] hello [CQ:at,qq=987654321]', true],
'other only' => ['[CQ:at,qq=987654321]', false],
];
}
public function testGetImageCQFromLocal(): void
{
file_put_contents('/tmp/test.jpg', 'test');
$this->assertEquals('[CQ:image,file=base64://' . base64_encode('test') . ']', MessageUtil::getImageCQFromLocal('/tmp/test.jpg'));
unlink('/tmp/test.jpg');
}
/**
* @dataProvider providerTestStrToArray
*/
2022-03-29 02:38:19 +08:00
public function testStrToArray(string $str, array $expected): void
2022-03-28 20:36:44 +08:00
{
$this->assertEquals($expected, MessageUtil::strToArray($str));
}
public function providerTestStrToArray(): array
{
$text = static function ($str): array {
return ['type' => 'text', 'data' => ['text' => $str]];
};
return [
'empty string' => ['', []],
'pure string' => ['foobar', [$text('foobar')]],
'spaced string' => ['hello world', [$text('hello world')]],
'spaced and multiline string' => ["hello\n world", [$text("hello\n world")]],
'string containing CQ' => ['[CQ:at,qq=123456789]', [['type' => 'at', 'data' => ['qq' => '123456789']]]],
];
}
/**
* @dataProvider providerTestSplitCommand
*/
2022-03-29 02:38:19 +08:00
public function testSplitCommand(string $msg, array $expected): void
2022-03-28 20:36:44 +08:00
{
$this->assertEquals($expected, MessageUtil::splitCommand($msg));
}
public function providerTestSplitCommand(): array
{
return [
2022-03-28 23:42:50 +08:00
'empty' => ['', ['']],
2022-03-28 20:36:44 +08:00
'spaced' => ['hello world', ['hello', 'world']],
'multiline' => ["hello\nworld", ['hello', 'world']],
'many spaces' => ['hello world', ['hello', 'world']],
'many spaces and multiline' => ["hello\n world", ['hello', 'world']],
'many parts' => ['hello world foo bar', ['hello', 'world', 'foo', 'bar']],
];
}
public function testDownloadCQImage(): void
{
2022-03-29 02:08:11 +08:00
if (file_exists(DataProvider::getDataFolder('images') . '/test.jpg')) {
unlink(DataProvider::getDataFolder('images') . '/test.jpg');
}
2022-04-02 02:18:31 +08:00
$msg = '[CQ:image,file=test.jpg,url=https://zhamao.xin/file/hello.jpg]';
2022-03-28 20:36:44 +08:00
$result = MessageUtil::downloadCQImage($msg);
$this->assertIsArray($result);
$this->assertCount(1, $result);
$this->assertFileExists(DataProvider::getDataFolder('images') . '/test.jpg');
unlink(DataProvider::getDataFolder('images') . '/test.jpg');
}
}