From 621f30bb704f383db1e3c50d41faff7bcaf58f6a Mon Sep 17 00:00:00 2001 From: yaozm Date: Tue, 17 Oct 2023 14:22:40 +0800 Subject: [PATCH] refactor(src): optimize LoginCaptchaServiceProvider - Removed unnecessary code - Moved view publishing logic to a separate method - Added condition to check if running in console before publishing views --- src/LoginCaptchaServiceProvider.php | 16 ++++++++--- src/Support/helpers.php | 6 +--- tests/LoginCaptchaServiceProviderrTest.php | 32 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 tests/LoginCaptchaServiceProviderrTest.php diff --git a/src/LoginCaptchaServiceProvider.php b/src/LoginCaptchaServiceProvider.php index 179004f..46d1f21 100644 --- a/src/LoginCaptchaServiceProvider.php +++ b/src/LoginCaptchaServiceProvider.php @@ -47,13 +47,10 @@ public function init(): void parent::init(); $this->setupConfig() + ->publishView() ->loadMigrations() ->extendValidator() ->bootingCaptcha(); - - $this->publishes([ - $this->getViewPath() => resource_path(sprintf('views/vendor/%s', $this->getName())), - ]); } public function provides(): array @@ -84,6 +81,17 @@ protected function setupConfig(): self return $this; } + protected function publishView(): self + { + if ($this->app->runningInConsole()) { + $this->publishes([ + $this->getViewPath() => resource_path(sprintf('views/vendor/%s', $this->getName())), + ]); + } + + return $this; + } + /** * 初始化配置. */ diff --git a/src/Support/helpers.php b/src/Support/helpers.php index 4f1f20e..0efc1e9 100644 --- a/src/Support/helpers.php +++ b/src/Support/helpers.php @@ -34,11 +34,7 @@ function login_captcha_check(string $value): bool */ function login_captcha_url(?string $routeName = null): string { - if (null === $routeName) { - return admin_route('captcha.generate', ['random' => Str::random()]); - } - - return admin_route($routeName, ['random' => Str::random()]); + return admin_route($routeName ?? 'captcha.generate', ['random' => Str::random()]); } } diff --git a/tests/LoginCaptchaServiceProviderrTest.php b/tests/LoginCaptchaServiceProviderrTest.php new file mode 100644 index 0000000..4615020 --- /dev/null +++ b/tests/LoginCaptchaServiceProviderrTest.php @@ -0,0 +1,32 @@ + + * + * This source file is subject to the MIT license that is bundled. + */ + +namespace Guanguans\DcatLoginCaptcha\Tests; + +use Guanguans\DcatLoginCaptcha\LoginCaptchaServiceProvider; +use Guanguans\DcatLoginCaptcha\Setting; +use Illuminate\Support\Facades\Validator; + +it('can return provides', function (): void { + expect(new LoginCaptchaServiceProvider(app())) + ->provides()->toBeArray(); +})->group(__DIR__, __FILE__); + +it('can return setting form', function (): void { + expect(new LoginCaptchaServiceProvider(app())) + ->settingForm()->toBeInstanceOf(Setting::class); +})->group(__DIR__, __FILE__); + +it('can check dcat_login_captcha rule', function (): void { + expect(Validator::make(['captcha' => 'foo'], ['captcha' => 'dcat_login_captcha'])) + ->fails()->toBeTrue(); +})->group(__DIR__, __FILE__);