From 39aeb29780a70563c927f28c058b3fbf11df254a Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Tue, 21 Dec 2021 14:52:03 -0300 Subject: [PATCH] Updated tests and removed redundant closures. --- tests/Http/Middleware/ScoreMiddlewareTest.php | 23 +---- .../Middleware/UsesRoutesWithMiddleware.php | 95 +++++-------------- 2 files changed, 29 insertions(+), 89 deletions(-) diff --git a/tests/Http/Middleware/ScoreMiddlewareTest.php b/tests/Http/Middleware/ScoreMiddlewareTest.php index 984c12d..0152d85 100644 --- a/tests/Http/Middleware/ScoreMiddlewareTest.php +++ b/tests/Http/Middleware/ScoreMiddlewareTest.php @@ -37,9 +37,7 @@ function (): void { public function test_fakes_response_if_authenticated_in_guard(): void { - $this->app['router']->post('v3/guarded', function (ReCaptchaResponse $response) { - return $response; - })->middleware(ReCaptcha::score()->except('web')->toString()); + $this->app['router']->post('v3/guarded', [__CLASS__, 'returnSameResponse'])->middleware(ReCaptcha::score()->except('web')->toString()); $this->actingAs(User::make(), 'web'); @@ -164,9 +162,7 @@ public function test_uses_custom_input(): void $this->fulfilledResponse(['success' => true, 'score' => 0.7, 'foo' => 'bar']) ); - $this->app['router']->post('test', function (ReCaptchaResponse $response) { - return $response; - })->middleware('recaptcha.score:null,null,foo'); + $this->app['router']->post('test', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,null,foo'); $this->post('test', ['foo' => 'token']) ->assertOk() @@ -343,9 +339,7 @@ public function test_no_error_if_no_action(): void $this->fulfilledResponse(['success' => true, 'action' => 'foo', 'apk_package_name' => null]) ); - $this->app['router']->post('test', function (ReCaptchaResponse $response) { - return $response; - })->middleware('recaptcha.score:null,null'); + $this->app['router']->post('test', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,null'); $this->post('test', [Captchavel::INPUT => 'token'])->assertOk(); } @@ -360,9 +354,7 @@ public function test_no_error_if_action_same(): void $this->fulfilledResponse(['success' => true, 'action' => 'foo', 'apk_package_name' => null]) ); - $this->app['router']->post('test', function (ReCaptchaResponse $response) { - return $response; - })->middleware('recaptcha.score:null,foo'); + $this->app['router']->post('test', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,foo'); $this->post('test', [Captchavel::INPUT => 'token'])->assertOk(); } @@ -382,12 +374,7 @@ public function test_exception_if_action_not_equal(): void ) ); - $this->app['router']->post( - 'test', - function (ReCaptchaResponse $response) { - return $response; - } - )->middleware('recaptcha.score:null,bar'); + $this->app['router']->post('test', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,bar'); $this->post('test', [Captchavel::INPUT => 'token']) ->assertSessionHasErrors(Captchavel::INPUT, trans('captchavel::validation.match')) diff --git a/tests/Http/Middleware/UsesRoutesWithMiddleware.php b/tests/Http/Middleware/UsesRoutesWithMiddleware.php index 1bd89ac..3844aef 100644 --- a/tests/Http/Middleware/UsesRoutesWithMiddleware.php +++ b/tests/Http/Middleware/UsesRoutesWithMiddleware.php @@ -3,6 +3,7 @@ namespace Tests\Http\Middleware; use DarkGhostHunter\Captchavel\Http\ReCaptchaResponse; +use function app; trait UsesRoutesWithMiddleware { @@ -10,78 +11,30 @@ protected function createsRoutes() { config(['captchavel.enable' => true]); - $this->app['router']->post('v3/default', function (ReCaptchaResponse $response) { - return $response; - })->middleware('recaptcha.score'); - - $this->app['router']->post('v3/threshold_1', function (ReCaptchaResponse $response) { - return $response; - })->middleware('recaptcha.score:1.0'); - - $this->app['router']->post('v3/threshold_0', function (ReCaptchaResponse $response) { - return $response; - })->middleware('recaptcha.score:0'); - - $this->app['router']->post('v3/action_foo', function (ReCaptchaResponse $response) { - return $response; - })->middleware('recaptcha.score:null,foo'); - - $this->app['router']->post('v3/input_bar', function (ReCaptchaResponse $response) { - return $response; - })->middleware('recaptcha.score:null,null,bar'); - - $this->app['router']->post('v2/checkbox', function () { - if (app()->has(ReCaptchaResponse::class)) { - return app(ReCaptchaResponse::class); - } - })->middleware('recaptcha:checkbox'); - - $this->app['router']->post('v2/checkbox/input_bar', function () { - if (app()->has(ReCaptchaResponse::class)) { - return app(ReCaptchaResponse::class); - } - })->middleware('recaptcha:checkbox,null,bar'); - - $this->app['router']->post('v2/checkbox/remember', function () { - if (app()->has(ReCaptchaResponse::class)) { - return app(ReCaptchaResponse::class); - } - })->middleware('recaptcha:checkbox,10'); - - $this->app['router']->post('v2/invisible', function () { - if (app()->has(ReCaptchaResponse::class)) { - return app(ReCaptchaResponse::class); - } - })->middleware('recaptcha:invisible'); - - $this->app['router']->post('v2/invisible/input_bar', function () { - if (app()->has(ReCaptchaResponse::class)) { - return app(ReCaptchaResponse::class); - } - })->middleware('recaptcha:invisible,null,bar'); - - $this->app['router']->post('v2/invisible/remember', function () { - if (app()->has(ReCaptchaResponse::class)) { - return app(ReCaptchaResponse::class); - } - })->middleware('recaptcha:invisible,10'); - - $this->app['router']->post('v2/android', function () { - if (app()->has(ReCaptchaResponse::class)) { - return app(ReCaptchaResponse::class); - } - })->middleware('recaptcha:android'); + $this->app['router']->post('v3/default', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score'); + $this->app['router']->post('v3/threshold_1', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:1.0'); + $this->app['router']->post('v3/threshold_0', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:0'); + $this->app['router']->post('v3/action_foo', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,foo'); + $this->app['router']->post('v3/input_bar', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,null,bar'); + + $this->app['router']->post('v2/checkbox', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:checkbox'); + $this->app['router']->post('v2/checkbox/input_bar', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:checkbox,null,bar'); + $this->app['router']->post('v2/checkbox/remember', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:checkbox,10'); + $this->app['router']->post('v2/invisible', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:invisible'); + $this->app['router']->post('v2/invisible/input_bar', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:invisible,null,bar'); + $this->app['router']->post('v2/invisible/remember', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:invisible,10'); + $this->app['router']->post('v2/android', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:android'); + $this->app['router']->post('v2/android/input_bar', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:android,null,bar'); + $this->app['router']->post('v2/android/remember', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:android,10'); + } - $this->app['router']->post('v2/android/input_bar', function () { - if (app()->has(ReCaptchaResponse::class)) { - return app(ReCaptchaResponse::class); - } - })->middleware('recaptcha:android,null,bar'); + public static function returnSameResponse(ReCaptchaResponse $response): ReCaptchaResponse + { + return $response; + } - $this->app['router']->post('v2/android/remember', function () { - if (app()->has(ReCaptchaResponse::class)) { - return app(ReCaptchaResponse::class); - } - })->middleware('recaptcha:android,10'); + public static function returnResponseIfExists(): ?ReCaptchaResponse + { + return app()->has(ReCaptchaResponse::class) ? app(ReCaptchaResponse::class) : null; } }