Skip to content

Commit

Permalink
Allows for multiple fakes to occur
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfox committed May 25, 2024
1 parent 6223858 commit 8406d40
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
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
8 changes: 7 additions & 1 deletion 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 @@ -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));
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

0 comments on commit 8406d40

Please sign in to comment.