mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 05:55:12 +08:00
fix(extend): 修复未传入 extends 不存在缺省值的问题
This commit is contained in:
@@ -24,13 +24,10 @@ class ExtendService
|
|||||||
array|string|null $params = null
|
array|string|null $params = null
|
||||||
): array {
|
): array {
|
||||||
$params ??= $request?->getQueryParams();
|
$params ??= $request?->getQueryParams();
|
||||||
$extends = $params['extends'] ?? null;
|
$extends = explode(',', $params['extends'] ?? '');
|
||||||
if (!empty($extends)) {
|
$extends = array_map('trim', $extends);
|
||||||
$extends = explode(',', $extends);
|
|
||||||
return Context::set(self::class, array_map('trim', $extends));
|
|
||||||
}
|
|
||||||
|
|
||||||
return [];
|
return Context::set(self::class, array_filter($extends));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -57,6 +54,6 @@ class ExtendService
|
|||||||
*/
|
*/
|
||||||
public function hasExtend(string $field): bool
|
public function hasExtend(string $field): bool
|
||||||
{
|
{
|
||||||
return $this->utils->inArray($field, Context::get(self::class));
|
return $this->utils->inArray($field, Context::get(self::class) ?? []);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,10 @@
|
|||||||
|
|
||||||
namespace Singularity\HDK\Test\Core\Unit;
|
namespace Singularity\HDK\Test\Core\Unit;
|
||||||
|
|
||||||
|
use Hyperf\Context\Context;
|
||||||
use Singularity\HDK\Core\Service\ExtendService;
|
use Singularity\HDK\Core\Service\ExtendService;
|
||||||
use Singularity\HDK\Core\Service\UtilsService;
|
use Singularity\HDK\Core\Service\UtilsService;
|
||||||
|
|
||||||
$service = new ExtendService(new UtilsService());
|
|
||||||
|
|
||||||
|
|
||||||
/** @var ExtendService $service */
|
/** @var ExtendService $service */
|
||||||
$service = make(ExtendService::class, ['utils' => new UtilsService()]);
|
$service = make(ExtendService::class, ['utils' => new UtilsService()]);
|
||||||
it('asserts no parameters can be parsed.', function (ExtendService $service, $params) {
|
it('asserts no parameters can be parsed.', function (ExtendService $service, $params) {
|
||||||
@@ -23,7 +21,7 @@ it('asserts no parameters can be parsed.', function (ExtendService $service, $pa
|
|||||||
$params
|
$params
|
||||||
);
|
);
|
||||||
$result = $service->getExtends();
|
$result = $service->getExtends();
|
||||||
expect($result)->toBeArray()->toHaveCount(0)->toBe([]);
|
expect($result)->toBeArray()->toBe([])->toHaveCount(0)->toBe([]);
|
||||||
})->with([
|
})->with([
|
||||||
[$service, null],
|
[$service, null],
|
||||||
[$service, ''],
|
[$service, ''],
|
||||||
@@ -32,6 +30,11 @@ it('asserts no parameters can be parsed.', function (ExtendService $service, $pa
|
|||||||
[$service, ['extends' => null]],
|
[$service, ['extends' => null]],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
it('asserts has no specify extend', function () use ($service) {
|
||||||
|
Context::destroy(ExtendService::class);
|
||||||
|
expect($service->hasExtend('a'))->toBeFalse();
|
||||||
|
})->depends('it asserts query parameters can be parsed.');
|
||||||
|
|
||||||
it('asserts query parameters can be parsed.', function () use ($service) {
|
it('asserts query parameters can be parsed.', function () use ($service) {
|
||||||
$result = $service->parse(
|
$result = $service->parse(
|
||||||
null,
|
null,
|
||||||
@@ -44,19 +47,21 @@ it('asserts query parameters can be parsed.', function () use ($service) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('asserts has extends', function () use ($service) {
|
it('asserts has extends', function () use ($service) {
|
||||||
|
Context::set(ExtendService::class, ['a', 'b']);
|
||||||
expect($service->hasExtends())->toBeTrue();
|
expect($service->hasExtends())->toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('asserts has specify extend', function () use ($service) {
|
it('asserts has specify extend', function () use ($service) {
|
||||||
|
Context::set(ExtendService::class, ['a', 'b']);
|
||||||
expect($service->hasExtend('a'))->toBeTrue()
|
expect($service->hasExtend('a'))->toBeTrue()
|
||||||
->and($service->hasExtend('b'))->toBeTrue()
|
->and($service->hasExtend('b'))->toBeTrue()
|
||||||
->and($service->hasExtend('c'))->toBeFalse();
|
->and($service->hasExtend('c'))->toBeFalse();
|
||||||
})->depends('it asserts query parameters can be parsed.');
|
})->depends('it asserts query parameters can be parsed.');
|
||||||
|
|
||||||
it('asserts parsed extends', function () use ($service) {
|
// it('asserts parsed extends', function () use ($service) {
|
||||||
$extends = $service->getExtends();
|
// $extends = $service->getExtends();
|
||||||
expect($extends)->toBeArray()->toMatchArray([
|
// expect($extends)->toBeArray()->toMatchArray([
|
||||||
'a',
|
// 'a',
|
||||||
'b',
|
// 'b',
|
||||||
]);
|
// ]);
|
||||||
})->depends('it asserts query parameters can be parsed.');
|
// })->depends('it asserts query parameters can be parsed.');
|
||||||
|
|||||||
Reference in New Issue
Block a user