diff --git a/src/Middlewares/GuardFeature.php b/src/Middlewares/GuardFeature.php index fb8a9a4..29b835d 100644 --- a/src/Middlewares/GuardFeature.php +++ b/src/Middlewares/GuardFeature.php @@ -3,6 +3,8 @@ namespace YlsIdeas\FeatureFlags\Middlewares; use Closure; +use Illuminate\Contracts\Container\BindingResolutionException; +use Illuminate\Contracts\Translation\Translator; use Illuminate\Foundation\Application; use Illuminate\Http\Request; use YlsIdeas\FeatureFlags\Manager; @@ -15,18 +17,30 @@ class GuardFeature { use StateChecking; - public function __construct(protected Manager $manager, protected Application $application) - { + public function __construct( + protected Manager $manager, + protected Application $application, + protected Translator $translator, + ) { } - public function handle(Request $request, Closure $next, string $feature, string $state = 'on', $abort = 403): mixed - { + /** + * @throws BindingResolutionException + */ + public function handle( + Request $request, + Closure $next, + string $feature, + string $state = 'on', + $abort = 403, + $message = '', + ): mixed { if ( ($this->check($state) ? ! $this->manager->accessible($feature) : $this->manager->accessible($feature)) ) { - $this->application->abort($abort); + $this->application->abort($abort, $this->translator->get($message)); } return $next($request); diff --git a/tests/Middlewares/GuardFeatureTest.php b/tests/Middlewares/GuardFeatureTest.php index 1b764a4..b52053d 100644 --- a/tests/Middlewares/GuardFeatureTest.php +++ b/tests/Middlewares/GuardFeatureTest.php @@ -2,6 +2,7 @@ namespace YlsIdeas\FeatureFlags\Tests\Middlewares; +use Illuminate\Contracts\Translation\Translator; use Illuminate\Foundation\Application; use Illuminate\Http\Request; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; @@ -20,9 +21,10 @@ public function test_it_can_abort_requests_when_features_are_not_accessible(): v $this->expectExceptionObject($exception); $app = \Mockery::mock(Application::class); + $translator = \Mockery::mock(Translator::class); $app->shouldReceive('abort') - ->with(403) + ->with(403, '') ->once() ->andThrow($exception); @@ -33,12 +35,17 @@ public function test_it_can_abort_requests_when_features_are_not_accessible(): v ->once() ->andReturn(false); + $translator->shouldReceive('get') + ->with('') + ->once() + ->andReturn(''); + $request = new Request(); - $middleware = new GuardFeature($manager, $app); + $middleware = new GuardFeature($manager, $app, $translator); $middleware->handle($request, function () { - }, 'my-feature', 'on'); + }, 'my-feature'); } public function test_it_can_abort_requests_when_features_are_not_accessible_and_expecting_to_be_off(): void @@ -47,9 +54,10 @@ public function test_it_can_abort_requests_when_features_are_not_accessible_and_ $this->expectExceptionObject($exception); $app = \Mockery::mock(Application::class); + $translator = \Mockery::mock(Translator::class); $app->shouldReceive('abort') - ->with(403) + ->with(403, '') ->once() ->andThrow($exception); @@ -60,9 +68,14 @@ public function test_it_can_abort_requests_when_features_are_not_accessible_and_ ->once() ->andReturn(true); + $translator->shouldReceive('get') + ->with('') + ->once() + ->andReturn(''); + $request = new Request(); - $middleware = new GuardFeature($manager, $app); + $middleware = new GuardFeature($manager, $app, $translator); $middleware->handle($request, function () { }, 'my-feature', 'off'); @@ -71,9 +84,10 @@ public function test_it_can_abort_requests_when_features_are_not_accessible_and_ public function test_it_continues_the_chain_if_features_are_accessible(): void { $app = \Mockery::mock(Application::class); + $translator = \Mockery::mock(Translator::class); $app->shouldReceive('abort') - ->with(403) + ->with(403, '') ->never(); $manager = \Mockery::mock(Manager::class); @@ -83,9 +97,11 @@ public function test_it_continues_the_chain_if_features_are_accessible(): void ->once() ->andReturn(true); + $translator->shouldNotReceive('get'); + $expectedRequest = new Request(); - $middleware = new GuardFeature($manager, $app); + $middleware = new GuardFeature($manager, $app, $translator); $this->assertTrue($middleware->handle( $expectedRequest, @@ -104,9 +120,10 @@ public function test_it_can_abort_requests_with_a_specified_http_status_code(): $this->expectExceptionObject($exception); $app = \Mockery::mock(Application::class); + $translator = \Mockery::mock(Translator::class); $app->shouldReceive('abort') - ->with(404) + ->with(404, '') ->once() ->andThrow($exception); @@ -117,11 +134,49 @@ public function test_it_can_abort_requests_with_a_specified_http_status_code(): ->once() ->andReturn(false); + $translator->shouldReceive('get') + ->with('') + ->once() + ->andReturn(''); + $request = new Request(); - $middleware = new GuardFeature($manager, $app); + $middleware = new GuardFeature($manager, $app, $translator); $middleware->handle($request, function () { }, 'my-feature', 'on', 404); } + + public function test_it_can_abort_requests_with_a_specified_message(): void + { + $exception = new HttpException(404, 'simple message'); + + $this->expectExceptionObject($exception); + $app = \Mockery::mock(Application::class); + $translator = \Mockery::mock(Translator::class); + + $app->shouldReceive('abort') + ->with(404, 'simple message') + ->once() + ->andThrow($exception); + + $manager = \Mockery::mock(Manager::class); + + $manager->shouldReceive('accessible') + ->with('my-feature') + ->once() + ->andReturn(false); + + $translator->shouldReceive('get') + ->with('simple message') + ->once() + ->andReturn('simple message'); + + $request = new Request(); + + $middleware = new GuardFeature($manager, $app, $translator); + + $middleware->handle($request, function () { + }, 'my-feature', 'on', 404, 'simple message'); + } }