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
|
|
|
|
2022-05-10 00:31:40 +08:00
|
|
|
use Exception;
|
2022-03-28 20:36:44 +08:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-04-02 02:45:24 +08:00
|
|
|
use Throwable;
|
2022-03-28 20:36:44 +08:00
|
|
|
use ZM\Annotation\CQ\CQCommand;
|
|
|
|
|
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
|
|
|
|
|
{
|
2022-05-10 00:31:40 +08:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2022-03-28 20:36:44 +08:00
|
|
|
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
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
{
|
2022-09-26 22:44:41 +08:00
|
|
|
file_put_contents(TMP_DIR . '/test.jpg', 'test');
|
2022-03-28 20:36:44 +08:00
|
|
|
$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']],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-02 02:45:24 +08:00
|
|
|
/**
|
|
|
|
|
* @throws Throwable
|
|
|
|
|
*/
|
2022-03-28 20:36:44 +08:00
|
|
|
public function testDownloadCQImage(): void
|
|
|
|
|
{
|
2022-05-10 00:31:40 +08:00
|
|
|
if (file_exists(DataProvider::getDataFolder('images') . '/test.png')) {
|
|
|
|
|
unlink(DataProvider::getDataFolder('images') . '/test.png');
|
2022-03-29 02:08:11 +08:00
|
|
|
}
|
2022-05-10 00:31:40 +08:00
|
|
|
$msg = '[CQ:image,file=test.png,url=https://zhamao.xin/file/hello.png]';
|
2022-04-02 02:45:24 +08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$result = MessageUtil::downloadCQImage($msg);
|
|
|
|
|
$this->assertIsArray($result);
|
|
|
|
|
$this->assertCount(1, $result);
|
2022-05-10 00:31:40 +08:00
|
|
|
$this->assertFileExists(DataProvider::getDataFolder('images') . '/test.png');
|
|
|
|
|
unlink(DataProvider::getDataFolder('images') . '/test.png');
|
2022-04-02 02:45:24 +08:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
|
if (strpos($e->getMessage(), 'enable-openssl') !== false) {
|
|
|
|
|
$this->markTestSkipped('OpenSSL is not enabled');
|
|
|
|
|
}
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
2022-03-28 20:36:44 +08:00
|
|
|
}
|
|
|
|
|
}
|