Skip to content

Commit 371350e

Browse files
authored
Allows for multiple fakes to occur (#67)
Co-authored-by: peterfox <[email protected]>
1 parent 6223858 commit 371350e

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

src/Facades/Features.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace YlsIdeas\FeatureFlags\Facades;
44

55
use Illuminate\Support\Facades\Facade;
6+
use Illuminate\Support\Testing\Fakes\Fake;
67
use YlsIdeas\FeatureFlags\Contracts\Features as FeaturesContract;
78
use YlsIdeas\FeatureFlags\Support\FeatureFake;
89

@@ -44,7 +45,11 @@ class Features extends Facade
4445
*/
4546
public static function fake(array $flagsToFake = []): FeatureFake
4647
{
47-
static::swap($fake = new FeatureFake(static::getFacadeRoot(), $flagsToFake));
48+
$manager = static::isFake()
49+
? static::getFacadeRoot()->manager
50+
: static::getFacadeRoot();
51+
52+
static::swap($fake = new FeatureFake($manager, $flagsToFake));
4853

4954
return $fake;
5055
}

src/FeatureFlagsServiceProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,13 @@ protected function schedulingMacros()
109109
/** @noRector \Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector */
110110
Event::macro('skipWithoutFeature', fn (string $feature): Event =>
111111
/** @var Event $this */
112-
/** @phpstan-ignore-next-line annoying issue with macros */
113112
$this->skip(fn () => ! Features::accessible($feature)));
114113
}
115114

116115
if (! Event::hasMacro('skipWithFeature')) {
117116
/** @noRector \Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector */
118117
Event::macro('skipWithFeature', fn ($feature): Event =>
119118
/** @var Event $this */
120-
/** @phpstan-ignore-next-line annoying issue with macros */
121119
$this->skip(fn () => Features::accessible($feature)));
122120
}
123121
}

src/Support/FeatureFake.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Facades\Event;
7+
use Illuminate\Support\Testing\Fakes\Fake;
78
use Illuminate\Support\Traits\ForwardsCalls;
89
use PHPUnit\Framework\Assert;
910
use YlsIdeas\FeatureFlags\Contracts\Features;
@@ -14,7 +15,7 @@
1415
/**
1516
* @see \YlsIdeas\FeatureFlags\Tests\Support\FeatureFakeTest
1617
*/
17-
class FeatureFake implements Features
18+
class FeatureFake implements Features, Fake
1819
{
1920
use ForwardsCalls;
2021

@@ -23,7 +24,7 @@ class FeatureFake implements Features
2324
/**
2425
* @param array<string, bool|array> $featureFlags
2526
*/
26-
public function __construct(protected Manager $manager, protected array $featureFlags = [])
27+
public function __construct(public Manager $manager, protected array $featureFlags = [])
2728
{
2829
}
2930

tests/Support/FeatureFakeTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ public function test_it_fires_events_still()
148148
Event::assertDispatched(FeatureAccessed::class);
149149
}
150150

151+
public function test_it_can_switch_a_feature_multiple_times()
152+
{
153+
Features::fake(['my-feature' => true]);
154+
Features::fake(['my-feature' => false]);
155+
156+
$this->assertFalse(Features::accessible('my-feature'));
157+
}
158+
151159
protected function getFake($manager, $features)
152160
{
153161
return new class ($manager, $features) extends FeatureFake {

0 commit comments

Comments
 (0)