Commit 1be7aca8 authored by Emiliano Balbuena's avatar Emiliano Balbuena

(test): Unleash spec tests

1 merge request!2Clean up and strategies
Pipeline #105829834 passed with stage
in 1 minute and 5 seconds
......@@ -2,6 +2,11 @@
namespace spec\Minds\UnleashClient;
use Exception;
use Minds\UnleashClient\Entities\Context;
use Minds\UnleashClient\Entities\Feature;
use Minds\UnleashClient\Entities\Strategy;
use Minds\UnleashClient\StrategyResolver;
use Minds\UnleashClient\Unleash;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
......@@ -9,7 +14,6 @@ use Minds\UnleashClient\Config;
use Minds\UnleashClient\Client;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
use Zend\Cache\Psr\SimpleCache\SimpleCacheDecorator as Cache;
class UnleashSpec extends ObjectBehavior
{
......@@ -25,18 +29,268 @@ class UnleashSpec extends ObjectBehavior
/** @var CacheInterface */
protected $cache;
public function let(Config $config, LoggerInterface $logger, Client $client, CacheInterface $cache)
{
/** @var StrategyResolver */
protected $strategyResolver;
public function let(
Config $config,
LoggerInterface $logger,
Client $client,
CacheInterface $cache,
StrategyResolver $strategyResolver
) {
$this->config = $config;
$this->logger = $logger;
$this->client = $client;
$this->cache = $cache;
$this->strategyResolver = $strategyResolver;
$this->beConstructedWith($config, $logger, $client, $cache);
$this->beConstructedWith(
$config,
$logger,
$client,
$cache,
$strategyResolver
);
}
public function it_is_initializable()
{
$this->shouldHaveType(Unleash::class);
}
public function it_should_check_if_it_is_enabled_from_server(
Feature $feature,
Strategy $strategy,
Context $context
) {
$this->client->getId()
->shouldBeCalled()
->willReturn('phpspec');
$cacheKeyPrefix = Unleash::UNLEASH_CLIENT_CACHE_PREFIX . 'phpspec' . '-';
$this->cache->get($cacheKeyPrefix . Unleash::UNLEASH_CLIENT_CACHE_TIMEOUT_KEY, -1)
->shouldBeCalled()
->willReturn(-1);
$this->client->register()
->shouldBeCalled()
->willReturn(true);
$this->client->getFeatureFlags()
->shouldBeCalled()
->willReturn([$feature]);
$feature->getName()
->shouldBeCalled()
->willReturn('test');
$this->cache->set($cacheKeyPrefix . 'test', $feature)
->shouldBeCalled()
->willReturn(true);
$this->config->getPollingIntervalSeconds()
->shouldBeCalled()
->willReturn(1000);
$this->cache->set(
$cacheKeyPrefix . Unleash::UNLEASH_CLIENT_CACHE_TIMEOUT_KEY,
Argument::type('int') // TODO: Mock time()
)
->shouldBeCalled()
->willReturn(true);
$this->cache->get($cacheKeyPrefix . 'test', null)
->shouldBeCalled()
->willReturn($feature);
$feature->isEnabled()
->shouldBeCalled()
->willReturn(true);
$feature->getStrategies()
->shouldBeCalled()
->willReturn([$strategy]);
$this->strategyResolver->isEnabled(
[$strategy],
$context
)
->shouldBeCalled()
->willReturn(true);
$this
->setContext($context)
->isEnabled('test', false)
->shouldReturn(true);
}
public function it_should_check_if_it_is_enabled_from_cache(
Feature $feature,
Strategy $strategy,
Context $context
) {
$this->client->getId()
->shouldBeCalled()
->willReturn('phpspec');
$cacheKeyPrefix = Unleash::UNLEASH_CLIENT_CACHE_PREFIX . 'phpspec' . '-';
$this->cache->get($cacheKeyPrefix . Unleash::UNLEASH_CLIENT_CACHE_TIMEOUT_KEY, -1)
->shouldBeCalled()
->willReturn(time() + 100000); // TODO: Mock time
$this->client->getFeatureFlags()
->shouldNotBeCalled();
$this->cache->get($cacheKeyPrefix . 'test', null)
->shouldBeCalled()
->willReturn($feature);
$feature->isEnabled()
->shouldBeCalled()
->willReturn(true);
$feature->getStrategies()
->shouldBeCalled()
->willReturn([$strategy]);
$this->strategyResolver->isEnabled(
[$strategy],
$context
)
->shouldBeCalled()
->willReturn(true);
$this
->setContext($context)
->isEnabled('test', false)
->shouldReturn(true);
}
public function it_should_check_if_it_is_not_enabled_on_feature_from_cache(
Feature $feature,
Strategy $strategy,
Context $context
) {
$this->client->getId()
->shouldBeCalled()
->willReturn('phpspec');
$cacheKeyPrefix = Unleash::UNLEASH_CLIENT_CACHE_PREFIX . 'phpspec' . '-';
$this->cache->get($cacheKeyPrefix . Unleash::UNLEASH_CLIENT_CACHE_TIMEOUT_KEY, -1)
->shouldBeCalled()
->willReturn(time() + 100000); // TODO: Mock time
$this->client->getFeatureFlags()
->shouldNotBeCalled();
$this->cache->get($cacheKeyPrefix . 'test', null)
->shouldBeCalled()
->willReturn($feature);
$feature->isEnabled()
->shouldBeCalled()
->willReturn(false);
$this->strategyResolver->isEnabled(Argument::cetera())
->shouldNotBeCalled();
$this
->setContext($context)
->isEnabled('test', false)
->shouldReturn(false);
}
public function it_should_check_if_it_is_not_enabled_on_strategy_from_cache(
Feature $feature,
Strategy $strategy,
Context $context
) {
$this->client->getId()
->shouldBeCalled()
->willReturn('phpspec');
$cacheKeyPrefix = Unleash::UNLEASH_CLIENT_CACHE_PREFIX . 'phpspec' . '-';
$this->cache->get($cacheKeyPrefix . Unleash::UNLEASH_CLIENT_CACHE_TIMEOUT_KEY, -1)
->shouldBeCalled()
->willReturn(time() + 100000); // TODO: Mock time
$this->client->getFeatureFlags()
->shouldNotBeCalled();
$this->cache->get($cacheKeyPrefix . 'test', null)
->shouldBeCalled()
->willReturn($feature);
$feature->isEnabled()
->shouldBeCalled()
->willReturn(true);
$feature->getStrategies()
->shouldBeCalled()
->willReturn([$strategy]);
$this->strategyResolver->isEnabled(
[$strategy],
$context
)
->shouldBeCalled()
->willReturn(false);
$this
->setContext($context)
->isEnabled('test', false)
->shouldReturn(false);
}
public function it_should_return_false_during_is_enabled_if_exception_thrown(
Feature $feature,
Strategy $strategy,
Context $context
) {
$this->client->getId()
->shouldBeCalled()
->willReturn('phpspec');
$cacheKeyPrefix = Unleash::UNLEASH_CLIENT_CACHE_PREFIX . 'phpspec' . '-';
$this->cache->get($cacheKeyPrefix . Unleash::UNLEASH_CLIENT_CACHE_TIMEOUT_KEY, -1)
->shouldBeCalled()
->willReturn(time() + 100000); // TODO: Mock time
$this->client->getFeatureFlags()
->shouldNotBeCalled();
$this->cache->get($cacheKeyPrefix . 'test', null)
->shouldBeCalled()
->willReturn($feature);
$feature->isEnabled()
->shouldBeCalled()
->willReturn(true);
$feature->getStrategies()
->shouldBeCalled()
->willReturn([$strategy]);
$this->strategyResolver->isEnabled(Argument::cetera())
->shouldBeCalled()
->willThrow(new Exception('Failed call'));
$this->logger->debug(Argument::cetera())
->willReturn(null);
$this->logger->error(Argument::cetera())
->shouldBeCalled()
->willReturn(null);
$this
->setContext($context)
->isEnabled('test', false)
->shouldReturn(false);
}
}
......@@ -111,7 +111,7 @@ class Unleash
* @return bool
* @throws InvalidArgumentException
*/
public function isCacheInvalid()
protected function isCacheInvalid()
{
return $this->cache->get($this->buildCacheKey(static::UNLEASH_CLIENT_CACHE_TIMEOUT_KEY), -1) <= time();
}
......@@ -119,7 +119,7 @@ class Unleash
/**
* Fetches the current collection of feature flags from server, and caches it
*/
public function fetch(): void
protected function fetch(): void
{
if (!$this->isClientRegistered) {
$this->logger->debug('Client is not registered');
......
Please register or to comment