diff --git a/src/Facades/Features.php b/src/Facades/Features.php index 031ae45..c028444 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/FeatureFlagsServiceProvider.php b/src/FeatureFlagsServiceProvider.php index a64548b..3cb7aea 100755 --- a/src/FeatureFlagsServiceProvider.php +++ b/src/FeatureFlagsServiceProvider.php @@ -109,7 +109,6 @@ protected function schedulingMacros() /** @noRector \Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector */ Event::macro('skipWithoutFeature', fn (string $feature): Event => /** @var Event $this */ - /** @phpstan-ignore-next-line annoying issue with macros */ $this->skip(fn () => ! Features::accessible($feature))); } @@ -117,7 +116,6 @@ protected function schedulingMacros() /** @noRector \Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector */ Event::macro('skipWithFeature', fn ($feature): Event => /** @var Event $this */ - /** @phpstan-ignore-next-line annoying issue with macros */ $this->skip(fn () => Features::accessible($feature))); } } diff --git a/src/Support/FeatureFake.php b/src/Support/FeatureFake.php index 31b6f18..f6b45a5 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; @@ -23,7 +24,7 @@ class FeatureFake implements Features /** * @param array $featureFlags */ - public function __construct(protected Manager $manager, protected array $featureFlags = []) + public function __construct(public Manager $manager, protected array $featureFlags = []) { } 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 {