Skip to content

Commit

Permalink
Simplify internal mocking helper
Browse files Browse the repository at this point in the history
Lost this when merging #1651
  • Loading branch information
caendesilva committed Apr 14, 2024
1 parent 71427d8 commit 998ef3d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
23 changes: 8 additions & 15 deletions packages/framework/src/Facades/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Illuminate\Support\Arr;

use function collect;
use function is_array;
use function array_filter;
use function extension_loaded;
use function in_array;
Expand Down Expand Up @@ -145,21 +144,15 @@ public function toArray(): array
]);
}

/**
* @internal This method is not covered by the backward compatibility promise.
*
* @param string|array<string, bool> $feature
*/
public static function mock(string|array $feature, bool $enabled = null): void
/** @internal This method is not covered by the backward compatibility promise. */
public static function mock(string $feature, bool $enabled = null): void
{
foreach (is_array($feature) ? $feature : [$feature => $enabled] as $feature => $enabled) {
if ($enabled === true) {
// Add the feature if it doesn't already exist.
Hyde::features()->features[] = collect(Feature::cases())->firstOrFail(fn (Feature $search): bool => Str::kebab($search->name) === $feature);
} else {
// Remove the feature if it exists.
Hyde::features()->features = array_filter(Hyde::features()->features, fn (Feature $search): bool => Str::kebab($search->name) !== $feature);
}
if ($enabled === true) {
// Add the feature if it doesn't already exist.
Hyde::features()->features[] = collect(Feature::cases())->firstOrFail(fn (Feature $search): bool => Str::kebab($search->name) === $feature);
} else {
// Remove the feature if it exists.
Hyde::features()->features = array_filter(Hyde::features()->features, fn (Feature $search): bool => Str::kebab($search->name) !== $feature);
}
}
}
12 changes: 4 additions & 8 deletions packages/framework/tests/Feature/ConfigurableFeaturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,14 @@ public function testFeaturesCanBeMocked()

public function testMultipleFeaturesCanBeMocked()
{
Features::mock([
'blade-pages' => true,
'darkmode' => true,
]);
Features::mock('blade-pages', true);
Features::mock('darkmode', true);

$this->assertTrue(Features::hasBladePages());
$this->assertTrue(Features::hasDarkmode());

Features::mock([
'blade-pages' => false,
'darkmode' => false,
]);
Features::mock('blade-pages', false);
Features::mock('darkmode', false);

$this->assertFalse(Features::hasBladePages());
$this->assertFalse(Features::hasDarkmode());
Expand Down

0 comments on commit 998ef3d

Please sign in to comment.