-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add application hostname strategy (#24)
- Loading branch information
1 parent
48ae6cb
commit 3603a39
Showing
9 changed files
with
205 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Unleash\Client\Strategy; | ||
|
||
use Unleash\Client\Configuration\Context; | ||
use Unleash\Client\DTO\Strategy; | ||
|
||
final class ApplicationHostnameStrategyHandler extends AbstractStrategyHandler | ||
{ | ||
public function getStrategyName(): string | ||
{ | ||
return 'applicationHostname'; | ||
} | ||
|
||
public function isEnabled(Strategy $strategy, Context $context): bool | ||
{ | ||
if (!$hostnames = $this->findParameter('hostNames', $strategy)) { | ||
return false; | ||
} | ||
|
||
$hostnames = array_map('trim', explode(',', $hostnames)); | ||
$enabled = in_array($context->getHostname(), $hostnames, true); | ||
|
||
if (!$enabled) { | ||
return false; | ||
} | ||
|
||
if (!$this->validateConstraints($strategy, $context)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace Unleash\Client\Tests\Strategy; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Unleash\Client\Configuration\UnleashContext; | ||
use Unleash\Client\DTO\DefaultConstraint; | ||
use Unleash\Client\DTO\DefaultStrategy; | ||
use Unleash\Client\Enum\ConstraintOperator; | ||
use Unleash\Client\Strategy\ApplicationHostnameStrategyHandler; | ||
|
||
final class ApplicationHostnameStrategyHandlerTest extends TestCase | ||
{ | ||
/** | ||
* @var ApplicationHostnameStrategyHandler | ||
*/ | ||
private $instance; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->instance = new ApplicationHostnameStrategyHandler(); | ||
} | ||
|
||
public function testSupports() | ||
{ | ||
self::assertFalse($this->instance->supports(new DefaultStrategy('default', []))); | ||
self::assertFalse($this->instance->supports(new DefaultStrategy('flexibleRollout', []))); | ||
self::assertFalse($this->instance->supports(new DefaultStrategy('remoteAddress', []))); | ||
self::assertFalse($this->instance->supports(new DefaultStrategy('userWithId', []))); | ||
self::assertFalse($this->instance->supports(new DefaultStrategy('nonexistent', []))); | ||
self::assertTrue($this->instance->supports(new DefaultStrategy('applicationHostname', []))); | ||
} | ||
|
||
public function testIsEnabled() | ||
{ | ||
self::assertFalse($this->instance->isEnabled( | ||
new DefaultStrategy('applicationHostname', []), | ||
new UnleashContext() | ||
)); | ||
|
||
self::assertFalse($this->instance->isEnabled( | ||
new DefaultStrategy('applicationHostname', [ | ||
'hostNames' => 'test1,test2', | ||
]), | ||
new UnleashContext() | ||
)); | ||
|
||
self::assertTrue($this->instance->isEnabled( | ||
new DefaultStrategy('applicationHostname', [ | ||
'hostNames' => 'test1,test2', | ||
]), | ||
(new UnleashContext())->setHostname('test1') | ||
)); | ||
self::assertTrue($this->instance->isEnabled( | ||
new DefaultStrategy('applicationHostname', [ | ||
'hostNames' => 'test1,test2', | ||
]), | ||
(new UnleashContext())->setHostname('test2') | ||
)); | ||
|
||
self::assertFalse($this->instance->isEnabled( | ||
new DefaultStrategy('applicationHostname', [ | ||
'hostNames' => 'test1,test2', | ||
]), | ||
(new UnleashContext())->setHostname('test3') | ||
)); | ||
|
||
$currentHostname = (new UnleashContext())->getHostname(); | ||
self::assertTrue( | ||
$this->instance->isEnabled( | ||
new DefaultStrategy('applicationHostname', [ | ||
'hostNames' => $currentHostname, | ||
]), | ||
new UnleashContext() | ||
) | ||
); | ||
|
||
self::assertFalse($this->instance->isEnabled( | ||
new DefaultStrategy('applicationHostname', [ | ||
'hostNames' => 'test1,test2', | ||
], [ | ||
new DefaultConstraint('something', ConstraintOperator::IN, ['test']), | ||
]), | ||
(new UnleashContext())->setHostname('test2') | ||
)); | ||
self::assertTrue($this->instance->isEnabled( | ||
new DefaultStrategy('applicationHostname', [ | ||
'hostNames' => 'test1,test2', | ||
], [ | ||
new DefaultConstraint('something', ConstraintOperator::IN, ['test']), | ||
]), | ||
(new UnleashContext())->setHostname('test2')->setCustomProperty('something', 'test') | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters