Files
hdk-core/tests/Unit/UtilsServiceTest.php
2023-10-30 14:30:31 +08:00

275 lines
7.8 KiB
PHP

<?php
/**
* UtilsServiceTest.php@HDK-Core
*
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
* Powered by PhpStorm
* Created on 2023/1/12
*/
use Hyperf\Codec\Json;
use Singularity\HDK\Core\Service\UtilsService;
$utils = new UtilsService();
dataset('categories', [
[
'source' => <<<JSON
[
{
"id": 200002538,
"name": "空心菜类",
"level": 3,
"namePath": "蔬菜/豆制品,叶菜类,空心菜类"
},
{
"id": 200002537,
"name": "香菜类",
"level": 3,
"namePath": "蔬菜/豆制品,葱姜蒜椒/调味菜,香菜类"
},
{
"id": 200002536,
"name": "紫苏/苏子叶",
"level": 3,
"namePath": "蔬菜/豆制品,叶菜类,紫苏/苏子叶"
},
{
"id": 200002543,
"name": "乌塌菜/塌菜/乌菜",
"level": 3,
"namePath": "蔬菜/豆制品,叶菜类,乌塌菜/塌菜/乌菜"
},
{
"id": 200002542,
"name": "菜心/菜苔类",
"level": 3,
"namePath": "蔬菜/豆制品,叶菜类,菜心/菜苔类"
},
{
"id": 200002540,
"name": "马兰头/马兰/红梗菜",
"level": 3,
"namePath": "蔬菜/豆制品,叶菜类,马兰头/马兰/红梗菜"
},
{
"id": 200002531,
"name": "苋菜类",
"level": 3,
"namePath": "蔬菜/豆制品,叶菜类,苋菜类"
},
{
"id": 200002528,
"name": "其他叶菜类",
"level": 3,
"namePath": "蔬菜/豆制品,叶菜类,其他叶菜类"
}
]
JSON,
'expected' => <<<JSON
[
{
"id": "e485b0bb87",
"id_path": ",e485b0bb87,",
"level": 1,
"name": "蔬菜/豆制品",
"name_path": "蔬菜/豆制品",
"parent_id": 0,
"children": [
{
"id": "bf1753faa2",
"id_path": ",e485b0bb87,bf1753faa2,",
"level": 2,
"name": "叶菜类",
"name_path": "蔬菜/豆制品,叶菜类",
"parent_id": "e485b0bb87",
"children": [
{
"id": "b8c8b14630",
"id_path": ",e485b0bb87,bf1753faa2,b8c8b14630,",
"level": 3,
"name": "空心菜类",
"name_path": "蔬菜/豆制品,叶菜类,空心菜类",
"parent_id": "bf1753faa2"
},
{
"id": "78c941ae91",
"id_path": ",e485b0bb87,bf1753faa2,78c941ae91,",
"level": 3,
"name": "紫苏/苏子叶",
"name_path": "蔬菜/豆制品,叶菜类,紫苏/苏子叶",
"parent_id": "bf1753faa2"
},
{
"id": "eccbf542f9",
"id_path": ",e485b0bb87,bf1753faa2,eccbf542f9,",
"level": 3,
"name": "乌塌菜/塌菜/乌菜",
"name_path": "蔬菜/豆制品,叶菜类,乌塌菜/塌菜/乌菜",
"parent_id": "bf1753faa2"
},
{
"id": "3621a8c3dc",
"id_path": ",e485b0bb87,bf1753faa2,3621a8c3dc,",
"level": 3,
"name": "菜心/菜苔类",
"name_path": "蔬菜/豆制品,叶菜类,菜心/菜苔类",
"parent_id": "bf1753faa2"
},
{
"id": "aad334b5a0",
"id_path": ",e485b0bb87,bf1753faa2,aad334b5a0,",
"level": 3,
"name": "马兰头/马兰/红梗菜",
"name_path": "蔬菜/豆制品,叶菜类,马兰头/马兰/红梗菜",
"parent_id": "bf1753faa2"
},
{
"id": "70d40b44f2",
"id_path": ",e485b0bb87,bf1753faa2,70d40b44f2,",
"level": 3,
"name": "苋菜类",
"name_path": "蔬菜/豆制品,叶菜类,苋菜类",
"parent_id": "bf1753faa2"
},
{
"id": "45623d0c88",
"id_path": ",e485b0bb87,bf1753faa2,45623d0c88,",
"level": 3,
"name": "其他叶菜类",
"name_path": "蔬菜/豆制品,叶菜类,其他叶菜类",
"parent_id": "bf1753faa2"
}
]
},
{
"id": "188ae68906",
"id_path": ",e485b0bb87,188ae68906,",
"level": 2,
"name": "葱姜蒜椒/调味菜",
"name_path": "蔬菜/豆制品,葱姜蒜椒/调味菜",
"parent_id": "e485b0bb87",
"children": [
{
"id": "4c64873413",
"id_path": ",e485b0bb87,188ae68906,4c64873413,",
"level": 3,
"name": "香菜类",
"name_path": "蔬菜/豆制品,葱姜蒜椒/调味菜,香菜类",
"parent_id": "188ae68906"
}
]
}
]
}
]
JSON
],
]);
$length_data = [];
for ($i = 0; $i < 5; $i++) {
try {
$length_data[] = random_int(1, 9);
} catch (Exception $e) {
continue;
}
}
test('断言验证码可以正常生成指定长度', function (int $length) use ($utils) {
try {
$code = $utils->generateSecureCode($length);
expect($code)->toHaveLength($length);
} catch (Exception $e) {
expect($e)->toBeNull();
}
})->with($length_data)->group('pure', 'utils');
test('断言可以根据参数构建 URL', function (string $url, array $params, bool $anchorQuery, string $expect) use ($utils) {
$url = $utils->buildUrl(url: $url, moreQueries: $params, anchorQuery: $anchorQuery);
expect($url)->toBe($expect);
})->with([
['baidu.com/list', ['a' => 'b'], false, 'baidu.com/list?a=b'],
['/api/v1/doc/categories/1?order=id', ['sort' => 'desc'], false, '/api/v1/doc/categories/1?order=id&sort=desc'],
['//google.com/search?c=d', ['a' => 'b'], false, '//google.com/search?c=d&a=b'],
[
'https://support.luxcreo.com/#/support?id=123',
['category' => 'abc'],
true,
'https://support.luxcreo.com/#/support?id=123&category=abc',
],
[
'https://support.luxcreo.com/#/support',
['category' => 'abc'],
true,
'https://support.luxcreo.com/#/support?category=abc',
],
[
'ssh://username:password@127.0.0.1/git/resp?id=1#/page?a=b',
['c' => 'd'],
false,
'ssh://username:password@127.0.0.1/git/resp?id=1&c=d#/page?a=b',
],
[
'http://username:password@127.0.0.1/git/resp?id=1#/page?a=b',
['c' => 'd'],
true,
'http://username:password@127.0.0.1/git/resp?id=1#/page?a=b&c=d',
],
])->group('pure', 'utils');
test('断言可以判断是否是数组中的元素', function (
$needle,
array $haystack,
bool $expect,
bool $exceptions = false
) use ($utils) {
try {
expect($utils->inArray($needle, $haystack))->toBe($expect);
} catch (Throwable $e) {
if ($exceptions) {
expect(true)->toBeTrue();
}
}
})->with([
[1, ['1', 2, 3], true, false],
['1', ['1', 2, 3], true, false],
[[1, 2, 3], ['1', 2, 3], false, true],
])->group('pure', 'utils');
test('无极分类', function (string $source, string $expected) {
$tree = [];
foreach (Json::decode($source) as $item) {
$path_array = explode(',', $item['namePath']);
$parent_id = 0;
$name_path = [];
$id_path = ',';
foreach ($path_array as $i => $name) {
$name_path[] = $name;
$level = $i + 1;
$id = substr(md5($level . $name), 0, 10);
$id_path .= $id . ',';
$new_item = [
'id' => $id,
'id_path' => $id_path,
'level' => $level,
'name' => $name,
'name_path' => join(',', $name_path + [$name]),
'parent_id' => $parent_id,
];
$tree[$id] = $new_item;
$parent_id = $id;
}
}
/** @var UtilsService $utils */
$utils = \Hyperf\Support\make(UtilsService::class);
usort($tree, fn($prevent, $current) => $current['level'] <=> $prevent['level']);
$tree = array_column($tree, null, 'id');
$utils->unlimitedSubCategoriesQuicklyWithLevel(list: $tree);
expect($tree)->toBe(Json::decode($expected));
})->with('categories');