From 8406d408def16bf532d074347e9de148fbaef6b2 Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 25 May 2024 11:54:17 +0100 Subject: [PATCH] Allows for multiple fakes to occur --- src/Facades/Features.php | 7 ++++++- src/Support/FeatureFake.php | 8 +++++++- tests/Support/FeatureFakeTest.php | 8 ++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Facades/Features.php b/src/Facades/Features.php index 031ae45..f4e1404 100755 --- a/src/Facades/Features.php +++ b/src/Facades/Features.php @@ -3,6 +3,7 @@ namespace YlsIdeas\FeatureFlags\Facades; use Illuminate\Support\Facades\Facade; +use Illuminate\Support\Testing\Fakes\Fake; use YlsIdeas\FeatureFlags\Contracts\Features as FeaturesContract; use YlsIdeas\FeatureFlags\Support\FeatureFake; @@ -44,7 +45,11 @@ class Features extends Facade */ public static function fake(array $flagsToFake = []): FeatureFake { - static::swap($fake = new FeatureFake(static::getFacadeRoot(), $flagsToFake)); + $manager = static::isFake() + ? static::getFacadeRoot()->manager() + : static::getFacadeRoot(); + + static::swap($fake = new FeatureFake($manager, $flagsToFake)); return $fake; } diff --git a/src/Support/FeatureFake.php b/src/Support/FeatureFake.php index 31b6f18..83eeae1 100644 --- a/src/Support/FeatureFake.php +++ b/src/Support/FeatureFake.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Testing\Fakes\Fake; use Illuminate\Support\Traits\ForwardsCalls; use PHPUnit\Framework\Assert; use YlsIdeas\FeatureFlags\Contracts\Features; @@ -14,7 +15,7 @@ /** * @see \YlsIdeas\FeatureFlags\Tests\Support\FeatureFakeTest */ -class FeatureFake implements Features +class FeatureFake implements Features, Fake { use ForwardsCalls; @@ -27,6 +28,11 @@ public function __construct(protected Manager $manager, protected array $feature { } + public function manager(): Manager + { + return $this->manager; + } + public function accessible(string $feature): bool { Event::dispatch(new FeatureAccessing($feature)); diff --git a/tests/Support/FeatureFakeTest.php b/tests/Support/FeatureFakeTest.php index c1ccde2..5c99648 100644 --- a/tests/Support/FeatureFakeTest.php +++ b/tests/Support/FeatureFakeTest.php @@ -148,6 +148,14 @@ public function test_it_fires_events_still() Event::assertDispatched(FeatureAccessed::class); } + public function test_it_can_switch_a_feature_multiple_times() + { + Features::fake(['my-feature' => true]); + Features::fake(['my-feature' => false]); + + $this->assertFalse(Features::accessible('my-feature')); + } + protected function getFake($manager, $features) { return new class ($manager, $features) extends FeatureFake {