Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Unleash.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ public function isFeatureEnabled(string $name, ...$args): bool
$strategies = Arr::get($feature, 'strategies', []);
$allStrategies = $this->config->get('unleash.strategies', []);

if (count($strategies) === 0) {
return $isEnabled;
}

foreach ($strategies as $strategyData) {
$className = $strategyData['name'];

if (!array_key_exists($className, $allStrategies)) {
return false;
continue;
}

if (is_callable($allStrategies[$className])) {
Expand All @@ -93,12 +97,12 @@ public function isFeatureEnabled(string $name, ...$args): bool

$params = Arr::get($strategyData, 'parameters', []);

if (!$strategy->isEnabled($params, $this->request, ...$args)) {
return false;
if ($strategy->isEnabled($params, $this->request, ...$args)) {
return true;
}
}

return $isEnabled;
return false;
}

public function isFeatureDisabled(string $name, ...$args): bool
Expand Down
14 changes: 14 additions & 0 deletions tests/Stubs/ImplementedStrategyThatIsDisabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace MikeFrancis\LaravelUnleash\Tests\Stubs;

use Illuminate\Http\Request;
use MikeFrancis\LaravelUnleash\Strategies\Contracts\Strategy;

class ImplementedStrategyThatIsDisabled implements Strategy
{
public function isEnabled(array $params, Request $request): bool
{
return false;
}
}
64 changes: 64 additions & 0 deletions tests/UnleashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Http\Request;
use MikeFrancis\LaravelUnleash\Tests\Stubs\ImplementedStrategy;
use MikeFrancis\LaravelUnleash\Tests\Stubs\ImplementedStrategyThatIsDisabled;
use MikeFrancis\LaravelUnleash\Tests\Stubs\NonImplementedStrategy;
use MikeFrancis\LaravelUnleash\Unleash;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -565,6 +566,69 @@ public function testIsFeatureEnabledWithValidStrategy()
$this->assertTrue($unleash->isFeatureEnabled($featureName));
}

public function testIsFeatureEnabledWithMultipleStrategies()
{
$featureName = 'someFeature';

$this->mockHandler->append(
new Response(
200,
[],
json_encode(
[
'features' => [
[
'name' => $featureName,
'enabled' => true,
'strategies' => [
[
'name' => 'testStrategyThatIsDisabled',
],
[
'name' => 'testStrategy',
]
],
],
],
]
)
)
);

$cache = $this->createMock(Cache::class);

$config = $this->createMock(Config::class);
$config->expects($this->at(0))
->method('get')
->with('unleash.isEnabled')
->willReturn(true);
$config->expects($this->at(1))
->method('get')
->with('unleash.cache.isEnabled')
->willReturn(false);
$config->expects($this->at(2))
->method('get')
->with('unleash.featuresEndpoint')
->willReturn('/api/client/features');
$config->expects($this->at(3))
->method('get')
->with('unleash.strategies')
->willReturn(
[
'testStrategy' => ImplementedStrategy::class,
],
[
'testStrategyThatIsDisabled' => ImplementedStrategyThatIsDisabled::class,
]
);

$request = $this->createMock(Request::class);

$unleash = new Unleash($this->client, $cache, $config, $request);

$this->assertTrue($unleash->isFeatureEnabled($featureName));
}

public function testIsFeatureDisabledWithInvalidStrategy()
{
$featureName = 'someFeature';
Expand Down