Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit ed1a1b8

Browse files
authored
Fix isFeatureEnabled returns false when multiple strategies are configured (#33)
1 parent a9a1ab6 commit ed1a1b8

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

src/Unleash.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,15 @@ public function isFeatureEnabled(string $name, ...$args): bool
7474
$strategies = Arr::get($feature, 'strategies', []);
7575
$allStrategies = $this->config->get('unleash.strategies', []);
7676

77+
if (count($strategies) === 0) {
78+
return $isEnabled;
79+
}
80+
7781
foreach ($strategies as $strategyData) {
7882
$className = $strategyData['name'];
7983

8084
if (!array_key_exists($className, $allStrategies)) {
81-
return false;
85+
continue;
8286
}
8387

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

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

96-
if (!$strategy->isEnabled($params, $this->request, ...$args)) {
97-
return false;
100+
if ($strategy->isEnabled($params, $this->request, ...$args)) {
101+
return true;
98102
}
99103
}
100104

101-
return $isEnabled;
105+
return false;
102106
}
103107

104108
public function isFeatureDisabled(string $name, ...$args): bool
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace MikeFrancis\LaravelUnleash\Tests\Stubs;
4+
5+
use Illuminate\Http\Request;
6+
use MikeFrancis\LaravelUnleash\Strategies\Contracts\Strategy;
7+
8+
class ImplementedStrategyThatIsDisabled implements Strategy
9+
{
10+
public function isEnabled(array $params, Request $request): bool
11+
{
12+
return false;
13+
}
14+
}

tests/UnleashTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Contracts\Config\Repository as Config;
1111
use Illuminate\Http\Request;
1212
use MikeFrancis\LaravelUnleash\Tests\Stubs\ImplementedStrategy;
13+
use MikeFrancis\LaravelUnleash\Tests\Stubs\ImplementedStrategyThatIsDisabled;
1314
use MikeFrancis\LaravelUnleash\Tests\Stubs\NonImplementedStrategy;
1415
use MikeFrancis\LaravelUnleash\Unleash;
1516
use PHPUnit\Framework\TestCase;
@@ -565,6 +566,69 @@ public function testIsFeatureEnabledWithValidStrategy()
565566
$this->assertTrue($unleash->isFeatureEnabled($featureName));
566567
}
567568

569+
public function testIsFeatureEnabledWithMultipleStrategies()
570+
{
571+
$featureName = 'someFeature';
572+
573+
$this->mockHandler->append(
574+
new Response(
575+
200,
576+
[],
577+
json_encode(
578+
[
579+
'features' => [
580+
[
581+
'name' => $featureName,
582+
'enabled' => true,
583+
'strategies' => [
584+
[
585+
'name' => 'testStrategyThatIsDisabled',
586+
],
587+
[
588+
'name' => 'testStrategy',
589+
]
590+
],
591+
],
592+
],
593+
]
594+
)
595+
)
596+
);
597+
598+
$cache = $this->createMock(Cache::class);
599+
600+
$config = $this->createMock(Config::class);
601+
$config->expects($this->at(0))
602+
->method('get')
603+
->with('unleash.isEnabled')
604+
->willReturn(true);
605+
$config->expects($this->at(1))
606+
->method('get')
607+
->with('unleash.cache.isEnabled')
608+
->willReturn(false);
609+
$config->expects($this->at(2))
610+
->method('get')
611+
->with('unleash.featuresEndpoint')
612+
->willReturn('/api/client/features');
613+
$config->expects($this->at(3))
614+
->method('get')
615+
->with('unleash.strategies')
616+
->willReturn(
617+
[
618+
'testStrategy' => ImplementedStrategy::class,
619+
],
620+
[
621+
'testStrategyThatIsDisabled' => ImplementedStrategyThatIsDisabled::class,
622+
]
623+
);
624+
625+
$request = $this->createMock(Request::class);
626+
627+
$unleash = new Unleash($this->client, $cache, $config, $request);
628+
629+
$this->assertTrue($unleash->isFeatureEnabled($featureName));
630+
}
631+
568632
public function testIsFeatureDisabledWithInvalidStrategy()
569633
{
570634
$featureName = 'someFeature';

0 commit comments

Comments
 (0)