From 8406d408def16bf532d074347e9de148fbaef6b2 Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 25 May 2024 11:54:17 +0100 Subject: [PATCH 1/4] 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 { From 90048d898639fb95125a0766597ad00183694cc2 Mon Sep 17 00:00:00 2001 From: peterfox Date: Sat, 25 May 2024 10:54:58 +0000 Subject: [PATCH 2/4] Update Facade Documentation --- src/Facades/Features.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Facades/Features.php b/src/Facades/Features.php index f4e1404..7874510 100755 --- a/src/Facades/Features.php +++ b/src/Facades/Features.php @@ -30,6 +30,7 @@ * @method static \static applyOnExpiredHandler(\YlsIdeas\FeatureFlags\Contracts\ExpiredFeaturesHandler $handler) * @method static \static extend(string $driver, callable $builder) * @method static \YlsIdeas\FeatureFlags\Support\MaintenanceRepository maintenanceMode() + * @method static \YlsIdeas\FeatureFlags\Manager manager() * @method static void assertAccessed(string $feature, int|null $count = null, string $message = '') * @method static void assertNotAccessed(string $feature, string $message = '') * @method static void assertAccessedCount(string $feature, int $count = 0, string $message = '') From 74210209f603d894e3fc89b70837e2f671e378fd Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 25 May 2024 11:58:04 +0100 Subject: [PATCH 3/4] PHPStan fix --- src/FeatureFlagsServiceProvider.php | 2 -- 1 file changed, 2 deletions(-) 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))); } } From 91b24932710074b17548eb6b70b10cff77226410 Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 25 May 2024 12:00:30 +0100 Subject: [PATCH 4/4] Fix facade documentation --- src/Facades/Features.php | 3 +-- src/Support/FeatureFake.php | 7 +------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Facades/Features.php b/src/Facades/Features.php index 7874510..c028444 100755 --- a/src/Facades/Features.php +++ b/src/Facades/Features.php @@ -30,7 +30,6 @@ * @method static \static applyOnExpiredHandler(\YlsIdeas\FeatureFlags\Contracts\ExpiredFeaturesHandler $handler) * @method static \static extend(string $driver, callable $builder) * @method static \YlsIdeas\FeatureFlags\Support\MaintenanceRepository maintenanceMode() - * @method static \YlsIdeas\FeatureFlags\Manager manager() * @method static void assertAccessed(string $feature, int|null $count = null, string $message = '') * @method static void assertNotAccessed(string $feature, string $message = '') * @method static void assertAccessedCount(string $feature, int $count = 0, string $message = '') @@ -47,7 +46,7 @@ class Features extends Facade public static function fake(array $flagsToFake = []): FeatureFake { $manager = static::isFake() - ? static::getFacadeRoot()->manager() + ? static::getFacadeRoot()->manager : static::getFacadeRoot(); static::swap($fake = new FeatureFake($manager, $flagsToFake)); diff --git a/src/Support/FeatureFake.php b/src/Support/FeatureFake.php index 83eeae1..f6b45a5 100644 --- a/src/Support/FeatureFake.php +++ b/src/Support/FeatureFake.php @@ -24,15 +24,10 @@ class FeatureFake implements Features, Fake /** * @param array $featureFlags */ - public function __construct(protected Manager $manager, protected array $featureFlags = []) + public function __construct(public Manager $manager, protected array $featureFlags = []) { } - public function manager(): Manager - { - return $this->manager; - } - public function accessible(string $feature): bool { Event::dispatch(new FeatureAccessing($feature));