Skip to content

Commit

Permalink
Implement a middleware message (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfox committed Jan 14, 2023
1 parent 08a47dd commit 29506de
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 14 deletions.
24 changes: 19 additions & 5 deletions src/Middlewares/GuardFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
73 changes: 64 additions & 9 deletions tests/Middlewares/GuardFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);

Expand All @@ -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');
}
}

0 comments on commit 29506de

Please sign in to comment.