Skip to content
Commits on Source (2)
bin/
vendor/
.phpunit.result.cache
bin
.php_cs.cache
.idea
.vscode
\ No newline at end of file
......@@ -20,6 +20,12 @@ before_script:
stages:
- test
lint:
stage: test
script:
- bin/php-cs-fixer fix --allow-risky=yes --verbose --dry-run
test:
stage: test
script:
- php composer.phar test
- php composer.phar test -- -f pretty
\ No newline at end of file
<?php
$finder = PhpCsFixer\Finder::create()
->exclude(['vendor','lib','classes'])
->in(__DIR__);
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'strict_param' => true,
'array_syntax' => ['syntax' => 'short'],
'no_blank_lines_after_class_opening' => true,
])
->setFinder($finder);
......@@ -2,16 +2,26 @@
"name": "minds/unleash-client-php",
"type": "library",
"description": "PHP client for Unleash",
"keywords": ["unleash", "client", "minds"],
"keywords": [
"unleash",
"client",
"minds"
],
"homepage": "https://gitlab.com/minds/unleash-client-php",
"license": "MIT",
"authors": [
{
"name": "Brian Hatchet",
"email": "brian@minds.com",
"homepage": "http://www.minds.com/brianhatchet",
"role": "Developer"
}
{
"name": "Brian Hatchet",
"email": "brian@minds.com",
"homepage": "http://www.minds.com/brianhatchet",
"role": "Developer"
},
{
"name": "Emiliano Balbuena",
"email": "emiliano@minds.com",
"homepage": "http://www.minds.com/edgebal",
"role": "Developer"
}
],
"minimum-stability": "dev",
"config": {
......@@ -23,17 +33,19 @@
"monolog/monolog": "^2.0@dev",
"psr/simple-cache": "^1.0@dev",
"zendframework/zend-cache": "^2.9@dev",
"zendframework/zend-serializer": "^2.9@dev"
"zendframework/zend-serializer": "^2.9@dev",
"lastguest/murmurhash": "dev-master"
},
"require-dev": {
"bossa/phpspec2-expect": "^3.0",
"phpspec/phpspec": "^4.0",
"phpspec/prophecy": "~1.0"
"bossa/phpspec2-expect": "^3.0",
"phpspec/phpspec": "^4.0",
"phpspec/prophecy": "~1.0",
"friendsofphp/php-cs-fixer": "^2.17@dev"
},
"autoload": {
"files": [
"src/Client.php",
"src/Unleash.php"
"src/Client.php",
"src/Unleash.php"
],
"psr-4": {
"Minds\\UnleashClient\\": "src/",
......
This diff is collapsed.
<?php
require('vendor/autoload.php');
use Minds\UnleashClient\Config;
use Minds\UnleashClient\Entities\Context;
use Minds\UnleashClient\Logger;
use Minds\UnleashClient\Unleash;
function main() : void
{
$logger = new Logger();
$logger->debug('Unleash client demo');
$config = new Config(
"https://gitlab.com/api/v4/feature_flags/unleash/14894840/",
"F2qZp9PyWKXDas9mkEsH",
"test",
300,
15
);
$context = new Context();
$context
->setUserId('1000')
->setSessionId('asdasdqweqwe123123')
->setRemoteAddress('127.0.0.1')
->setHostName('www.minds.com');
$unleash = new Unleash($config, $logger);
$unleash
->setContext($context);
$enabled = $unleash->isEnabled('test', false);
$logger->info("'test' flag evaluates to {$enabled}");
$enabled = $unleash->isEnabled('test-fiftypercent', false);
$logger->info("'test-fiftypercent' flag evaluates to {$enabled}");
$enabled = $unleash->isEnabled('test-selective', false);
$logger->info("'test-selective' flag evaluates to {$enabled}");
$enabled = $unleash->isEnabled('test_on_test', false);
$logger->info("'test_on_test' flag evaluates to {$enabled}");
}
main();
<?php
require('vendor/autoload.php');
use Minds\UnleashClient\Helpers\NormalizedValue;
use Minds\UnleashClient\Logger;
function main()
{
$logger = new Logger();
$logger->debug('Unleash normalizer stats');
$normalizedValue = new NormalizedValue();
$stats = array_fill(1, 100, 0);
$min = 1;
$max = 999999;
$groupId = 'default';
for ($i = $min; $i <= $max; $i++) {
$id = $normalizedValue->build("$i", $groupId, 100, 1);
$stats[(int) $id]++;
}
var_export($stats);
}
main();
......@@ -8,6 +8,7 @@ use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use GuzzleHttp\Client as HttpClient;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
class ClientSpec extends ObjectBehavior
{
......@@ -16,22 +17,33 @@ class ClientSpec extends ObjectBehavior
private const TEST_INSTANCE_ID = "test_instance_id";
private const TEST_POLLING_INTERVAL_SECONDS = 15;
private const TEST_METRICS_INTERVAL_SECONDS = 20;
/** @var Config; */
private $config;
/** @var LoggerInterface */
private $logger;
/** @var HttpClient */
private $httpClient;
public function let(Config $config, HttpClient $httpClient)
{
public function let(
Config $config,
LoggerInterface $logger,
HttpClient $httpClient
) {
$this->config = $config;
$this->logger = $logger;
$this->httpClient = $httpClient;
$this->config->getApiUrl()->willReturn(ClientSpec::TEST_URL);
$this->config->getApplicationName()->willReturn(ClientSpec::TEST_APPLICATION_NAME);
$this->config->getInstanceId()->willReturn(ClientSpec::TEST_INSTANCE_ID);
$this->config->getPollingIntervalSeconds()->willReturn(ClientSpec::TEST_POLLING_INTERVAL_SECONDS);
$this->config->getMetricsIntervalSeconds()->willReturn(ClientSpec::TEST_METRICS_INTERVAL_SECONDS);
$this->config->getVersion()->willReturn(Config::VERSION);
$this->beConstructedWith($this->config, $this->httpClient);
$this->beConstructedWith($this->config, $this->logger, $this->httpClient);
}
public function it_is_initializable()
......
......@@ -59,7 +59,7 @@ class ConfigSpec extends ObjectBehavior
$applicationName = "new_application_name";
$pollingIntervalSeconds = 100;
$metricsIntervalSeconds = 200;
$this->getApiUrl()->shouldEqual(ConfigSpec::TEST_URL);
$this->getInstanceId()->shouldEqual(ConfigSpec::TEST_INSTANCE_ID);
$this->getApplicationName()->shouldEqual(ConfigSpec::TEST_APPLICATION_NAME);
......
......@@ -3,40 +3,61 @@
namespace spec\Minds\UnleashClient\Entities;
use Minds\UnleashClient\Entities\Context;
use Minds\UnleashClient\Config;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class ContextSpec extends ObjectBehavior
{
private const TEST_APP_NAME = "test_app_name";
private const TEST_USER_ID = "test_user_id";
private const TEST_SESSION_ID = "test_session_id";
private const TEST_REMOTE_ADDRESS = "test_remote_address";
private const TEST_ENVIRONMENT = "test_environment";
public function it_is_initializable()
{
$this->shouldHaveType(Context::class);
}
/** @var Config */
private $config;
public function it_should_set_and_get_user_id()
{
$this
->getUserId()
->shouldReturn(null);
$this
->setUserId('phpspec')
->getUserId()
->shouldReturn('phpspec');
}
public function let(Config $config)
public function it_should_set_and_get_session_id()
{
$this->config = $config;
$this->config->getApplicationName()
->willReturn(ContextSpec::TEST_APP_NAME);
$this->beConstructedWith(
$this->config,
ContextSpec::TEST_USER_ID,
ContextSpec::TEST_SESSION_ID,
ContextSpec::TEST_REMOTE_ADDRESS,
ContextSpec::TEST_ENVIRONMENT
);
$this
->getSessionId()
->shouldReturn(null);
$this
->setSessionId('phpspec')
->getSessionId()
->shouldReturn('phpspec');
}
public function it_should_set_and_get_remote_address()
{
$this
->getRemoteAddress()
->shouldReturn(null);
$this
->setRemoteAddress('127.0.0.1')
->getRemoteAddress()
->shouldReturn('127.0.0.1');
}
public function it_is_initializable()
public function it_should_set_and_get_host_name()
{
$this->shouldHaveType(Context::class);
$this
->getHostName()
->shouldReturn(null);
$this
->setHostName('phpspec.test')
->getHostName()
->shouldReturn('phpspec.test');
}
}
<?php
namespace spec\Minds\UnleashClient\Entities;
use Minds\UnleashClient\Entities\Feature;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class FeatureSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType(Feature::class);
}
public function it_should_set_and_get_name()
{
$this
->getName()
->shouldReturn('');
$this
->setName('phpspec')
->getName()
->shouldReturn('phpspec');
}
public function it_should_set_and_get_description()
{
$this
->getDescription()
->shouldReturn('');
$this
->setDescription('phpspec')
->getDescription()
->shouldReturn('phpspec');
}
public function it_should_set_and_get_enabled_flag()
{
$this
->isEnabled()
->shouldReturn(false);
$this
->setEnabled(true)
->isEnabled()
->shouldReturn(true);
$this
->setEnabled(false)
->isEnabled()
->shouldReturn(false);
}
}
<?php
namespace spec\Minds\UnleashClient\Entities;
use Minds\UnleashClient\Entities\Strategy;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class StrategySpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType(Strategy::class);
}
public function it_should_set_and_get_name()
{
$this
->getName()
->shouldReturn('');
$this
->setName('phpspec')
->getName()
->shouldReturn('phpspec');
}
public function it_should_set_and_get_parameters()
{
$this
->getParameters()
->shouldReturn([]);
$this
->setParameters([
'phpspec' => 1
])
->getParameters()
->shouldReturn([
'phpspec' => 1
]);
}
}
<?php
namespace spec\Minds\UnleashClient\Factories;
use Minds\UnleashClient\Entities\Feature;
use Minds\UnleashClient\Entities\Strategy;
use Minds\UnleashClient\Factories\FeatureFactory;
use Minds\UnleashClient\Factories\StrategyFactory;
use PhpSpec\Exception\Example\FailureException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class FeatureFactorySpec extends ObjectBehavior
{
/** @var StrategyFactory */
protected $strategyFactory;
public function let(
StrategyFactory $strategyFactory
) {
$this->strategyFactory = $strategyFactory;
$this->beConstructedWith($strategyFactory);
}
public function it_is_initializable()
{
$this->shouldHaveType(FeatureFactory::class);
}
public function it_should_build(
Strategy $strategy1,
Strategy $strategy2
) {
$this->strategyFactory->build(['strategy.1'])
->shouldBeCalled()
->willReturn($strategy1);
$this->strategyFactory->build(['strategy.2'])
->shouldBeCalled()
->willReturn($strategy2);
$this
->build([
'name' => 'phpspec',
'description' => 'a phpspec feature',
'enabled' => true,
'strategies' => [
['strategy.1'],
['strategy.2']
]
])
->shouldBeAFeature([
'name' => 'phpspec',
'description' => 'a phpspec feature',
'enabled' => true,
'strategies' => [
$strategy1,
$strategy2
]
]);
}
public function getMatchers(): array
{
return [
'beAFeature' => function ($subject, $data) {
if (!($subject instanceof Feature)) {
throw new FailureException(sprintf("Subject should be a %s instance", Feature::class));
}
if ($subject->getName() !== $data['name']) {
throw new FailureException('Unexpected subject getName() value');
}
if ($subject->getDescription() !== $data['description']) {
throw new FailureException('Unexpected subject getDescription() value');
}
if ($subject->isEnabled() !== $data['enabled']) {
throw new FailureException('Unexpected subject isEnabled() value');
}
if ($subject->getStrategies() !== $data['strategies']) {
throw new FailureException('Unexpected subject getStrategies() value');
}
return true;
}
];
}
}
<?php
namespace spec\Minds\UnleashClient\Factories;
use Minds\UnleashClient\Entities\Strategy;
use Minds\UnleashClient\Factories\StrategyAlgorithmFactory;
use Minds\UnleashClient\StrategyAlgorithms\DefaultStrategyAlgorithm;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;
class StrategyAlgorithmFactorySpec extends ObjectBehavior
{
/** @var LoggerInterface */
protected $logger;
public function let(
LoggerInterface $logger
) {
$this->logger = $logger;
$this->beConstructedWith($logger);
}
public function it_is_initializable()
{
$this->shouldHaveType(StrategyAlgorithmFactory::class);
}
public function it_should_build(
Strategy $strategy
) {
$strategy->getName()
->shouldBeCalled()
->willReturn('default');
$this
->build($strategy)
->shouldBeAnInstanceOf(DefaultStrategyAlgorithm::class);
}
public function it_should_return_null_if_class_does_not_exist(
Strategy $strategy
) {
$strategy->getName()
->shouldBeCalled()
->willReturn('php~notexisting~class');
$this->logger->warning(Argument::cetera())
->shouldBeCalled();
$this
->build($strategy)
->shouldReturn(null);
}
}
<?php
namespace spec\Minds\UnleashClient\Factories;
use Minds\UnleashClient\Entities\Strategy;
use Minds\UnleashClient\Factories\StrategyFactory;
use PhpSpec\Exception\Example\FailureException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class StrategyFactorySpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType(StrategyFactory::class);
}
public function it_should_build()
{
$this
->build([
'name' => 'phpspec',
'parameters' => [
'test' => 1,
]
])
->shouldBeAStrategy([
'name' => 'phpspec',
'parameters' => [
'test' => 1,
]
]);
}
public function getMatchers(): array
{
return [
'beAStrategy' => function ($subject, $data) {
if (!($subject instanceof Strategy)) {
throw new FailureException(sprintf("Subject should be a %s instance", Strategy::class));
}
if ($subject->getName() !== $data['name']) {
throw new FailureException('Unexpected subject getName() value');
}
if ($subject->getParameters() !== $data['parameters']) {
throw new FailureException('Unexpected subject getParameters() value');
}
return true;
}
];
}
}
<?php
namespace spec\Minds\UnleashClient\Helpers;
use Minds\UnleashClient\Helpers\NormalizedValue;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class NormalizedValueSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType(NormalizedValue::class);
}
public function it_should_build()
{
$this
->build('', 'test', 100)
->shouldReturn(0);
$this
->build('phpspec', 'test', 100)
->shouldReturn(96);
$this
->build('minds', 'test', 100)
->shouldReturn(89);
}
}
<?php
namespace spec\Minds\UnleashClient\StrategyAlgorithms;
use Minds\UnleashClient\Entities\Context;
use Minds\UnleashClient\Entities\Strategy;
use Minds\UnleashClient\StrategyAlgorithms\ApplicationHostnameStrategyAlgorithm;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;
class ApplicationHostnameStrategyAlgorithmSpec extends ObjectBehavior
{
/** @var LoggerInterface */
protected $logger;
public function let(
LoggerInterface $logger
) {
$this->logger = $logger;
$this->beConstructedWith($logger);
}
public function it_is_initializable()
{
$this->shouldHaveType(ApplicationHostnameStrategyAlgorithm::class);
}
public function it_should_check_it_is_enabled(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'hostNames' => 'foo.bar, phpspec.test, minds.com'
]);
$context->getHostName()
->shouldBeCalled()
->willReturn('phpspec.test');
$this
->isEnabled($strategy, $context)
->shouldReturn(true);
}
public function it_should_check_it_is_not_enabled(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'hostNames' => 'foo.bar, phpspec.test, minds.com'
]);
$context->getHostName()
->shouldBeCalled()
->willReturn('notawhitelisteddomain.com');
$this
->isEnabled($strategy, $context)
->shouldReturn(false);
}
}
<?php
namespace spec\Minds\UnleashClient\StrategyAlgorithms;
use Minds\UnleashClient\Entities\Context;
use Minds\UnleashClient\Entities\Strategy;
use Minds\UnleashClient\StrategyAlgorithms\DefaultStrategyAlgorithm;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;
class DefaultStrategyAlgorithmSpec extends ObjectBehavior
{
/** @var LoggerInterface */
protected $logger;
public function let(
LoggerInterface $logger
) {
$this->logger = $logger;
$this->beConstructedWith($logger);
}
public function it_is_initializable()
{
$this->shouldHaveType(DefaultStrategyAlgorithm::class);
}
public function it_should_check_it_is_enabled(
Strategy $strategy,
Context $context
) {
$this
->isEnabled($strategy, $context)
->shouldReturn(true);
}
}
<?php
namespace spec\Minds\UnleashClient\StrategyAlgorithms;
use Minds\UnleashClient\Entities\Context;
use Minds\UnleashClient\Entities\Strategy;
use Minds\UnleashClient\Helpers\NormalizedValue;
use Minds\UnleashClient\StrategyAlgorithms\FlexibleRolloutStrategyAlgorithm;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;
class FlexibleRolloutStrategyAlgorithmSpec extends ObjectBehavior
{
/** @var LoggerInterface */
protected $logger;
/** @var NormalizedValue */
protected $normalizedValue;
public function let(
LoggerInterface $logger,
NormalizedValue $normalizedValue
) {
$this->logger = $logger;
$this->normalizedValue = $normalizedValue;
$this->beConstructedWith($logger, $normalizedValue);
}
public function it_is_initializable()
{
$this->shouldHaveType(FlexibleRolloutStrategyAlgorithm::class);
}
public function it_should_check_it_is_enabled_with_user_id_stickiness(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'rollout' => 20,
'stickiness' => 'userId',
'groupId' => 'test'
]);
$context->getUserId()
->shouldBeCalled()
->willReturn('1000');
$context->getSessionId()
->willReturn(null);
$this->normalizedValue->random(999999, 1)
->willReturn(99);
$this->normalizedValue->build('1000', 'test')
->shouldBeCalled()
->willReturn(10);
$this
->isEnabled($strategy, $context)
->shouldReturn(true);
}
public function it_should_check_it_is_not_enabled_with_user_id_stickiness(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'rollout' => 20,
'stickiness' => 'userId',
'groupId' => 'test'
]);
$context->getUserId()
->shouldBeCalled()
->willReturn('1000');
$context->getSessionId()
->willReturn(null);
$this->normalizedValue->random(999999, 1)
->willReturn(99);
$this->normalizedValue->build('1000', 'test')
->shouldBeCalled()
->willReturn(90);
$this
->isEnabled($strategy, $context)
->shouldReturn(false);
}
public function it_should_check_it_is_enabled_with_session_id_stickiness(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'rollout' => 20,
'stickiness' => 'sessionId',
'groupId' => 'test'
]);
$context->getUserId()
->willReturn(null);
$context->getSessionId()
->shouldBeCalled()
->willReturn('phpspec~123123');
$this->normalizedValue->random(999999, 1)
->willReturn(99);
$this->normalizedValue->build('phpspec~123123', 'test')
->shouldBeCalled()
->willReturn(10);
$this
->isEnabled($strategy, $context)
->shouldReturn(true);
}
public function it_should_check_it_is_not_enabled_with_session_id_stickiness(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'rollout' => 20,
'stickiness' => 'sessionId',
'groupId' => 'test'
]);
$context->getUserId()
->willReturn(null);
$context->getSessionId()
->shouldBeCalled()
->willReturn('phpspec~123123');
$this->normalizedValue->random(999999, 1)
->willReturn(99);
$this->normalizedValue->build('phpspec~123123', 'test')
->shouldBeCalled()
->willReturn(90);
$this
->isEnabled($strategy, $context)
->shouldReturn(false);
}
public function it_should_check_it_is_enabled_with_random_stickiness(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'rollout' => 20,
'stickiness' => 'random',
'groupId' => 'test'
]);
$context->getUserId()
->willReturn(null);
$context->getSessionId()
->willReturn(null);
$this->normalizedValue->random(999999, 1)
->willReturn(99);
$this->normalizedValue->random(999999, 1)
->shouldBeCalled()
->willReturn(99);
$this->normalizedValue->build('99', 'test')
->shouldBeCalled()
->willReturn(10);
$this
->isEnabled($strategy, $context)
->shouldReturn(true);
}
public function it_should_check_it_is_not_enabled_with_random_stickiness(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'rollout' => 20,
'stickiness' => 'random',
'groupId' => 'test'
]);
$context->getUserId()
->willReturn(null);
$context->getSessionId()
->willReturn(null);
$this->normalizedValue->random(999999, 1)
->shouldBeCalled()
->willReturn(99);
$this->normalizedValue->build('99', 'test')
->shouldBeCalled()
->willReturn(90);
$this
->isEnabled($strategy, $context)
->shouldReturn(false);
}
public function it_should_check_it_is_enabled_with_default_stickiness_using_user_id(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'rollout' => 20,
'stickiness' => 'default',
'groupId' => 'test'
]);
$context->getUserId()
->shouldBeCalled()
->willReturn('1000');
$context->getSessionId()
->willReturn(null);
$this->normalizedValue->random(999999, 1)
->willReturn(99);
$this->normalizedValue->build('1000', 'test')
->shouldBeCalled()
->willReturn(10);
$this
->isEnabled($strategy, $context)
->shouldReturn(true);
}
public function it_should_check_it_is_enabled_with_default_stickiness_using_session_id(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'rollout' => 20,
'stickiness' => 'default',
'groupId' => 'test'
]);
$context->getUserId()
->willReturn(null);
$context->getSessionId()
->shouldBeCalled()
->willReturn('phpspec~123123');
$this->normalizedValue->random(999999, 1)
->willReturn(99);
$this->normalizedValue->build('phpspec~123123', 'test')
->shouldBeCalled()
->willReturn(10);
$this
->isEnabled($strategy, $context)
->shouldReturn(true);
}
public function it_should_check_it_is_enabled_with_default_stickiness_using_random(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'rollout' => 20,
'stickiness' => 'default',
'groupId' => 'test'
]);
$context->getUserId()
->willReturn(null);
$context->getSessionId()
->willReturn(null);
$this->normalizedValue->random(999999, 1)
->shouldBeCalled()
->willReturn(99);
$this->normalizedValue->build('99', 'test')
->shouldBeCalled()
->willReturn(10);
$this
->isEnabled($strategy, $context)
->shouldReturn(true);
}
}
<?php
namespace spec\Minds\UnleashClient\StrategyAlgorithms;
use Minds\UnleashClient\Entities\Context;
use Minds\UnleashClient\Entities\Strategy;
use Minds\UnleashClient\Helpers\NormalizedValue;
use Minds\UnleashClient\StrategyAlgorithms\GradualRolloutRandomStrategyAlgorithm;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;
class GradualRolloutRandomStrategyAlgorithmSpec extends ObjectBehavior
{
/** @var LoggerInterface */
protected $logger;
/** @var NormalizedValue */
protected $normalizedValue;
public function let(
LoggerInterface $logger,
NormalizedValue $normalizedValue
) {
$this->logger = $logger;
$this->normalizedValue = $normalizedValue;
$this->beConstructedWith($logger, $normalizedValue);
}
public function it_is_initializable()
{
$this->shouldHaveType(GradualRolloutRandomStrategyAlgorithm::class);
}
public function it_should_check_it_is_enabled(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'percentage' => 20
]);
$this->normalizedValue->random()
->shouldBeCalled()
->willReturn(10);
$this
->isEnabled($strategy, $context)
->shouldReturn(true);
}
public function it_should_check_it_is_not_enabled(
Strategy $strategy,
Context $context
) {
$strategy->getParameters()
->shouldBeCalled()
->willReturn([
'percentage' => 20
]);
$this->normalizedValue->random()
->shouldBeCalled()
->willReturn(90);
$this
->isEnabled($strategy, $context)
->shouldReturn(false);
}
}