mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 06:55:06 +08:00
82 lines
2.4 KiB
PHP
82 lines
2.4 KiB
PHP
<?php
|
|
/**
|
|
* UtilsServiceTest.php@HDK-Core
|
|
*
|
|
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
|
* Powered by PhpStorm
|
|
* Created on 2023/1/12
|
|
*/
|
|
|
|
use Singularity\HDK\Core\Service\UtilsService;
|
|
|
|
$utils = new UtilsService();
|
|
|
|
$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, $params, $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');
|