Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows for multiple fakes to occur #67

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Facades/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 0 additions & 2 deletions src/FeatureFlagsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ 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)));
}

if (! Event::hasMacro('skipWithFeature')) {
/** @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)));
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Support/FeatureFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +15,7 @@
/**
* @see \YlsIdeas\FeatureFlags\Tests\Support\FeatureFakeTest
*/
class FeatureFake implements Features
class FeatureFake implements Features, Fake
{
use ForwardsCalls;

Expand All @@ -23,7 +24,7 @@ class FeatureFake implements Features
/**
* @param array<string, bool|array> $featureFlags
*/
public function __construct(protected Manager $manager, protected array $featureFlags = [])
public function __construct(public Manager $manager, protected array $featureFlags = [])
{
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Support/FeatureFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading