Skip to content
Projects
Groups
Snippets
Help
Sign in / Register
Toggle navigation
U
unleash-client-php
Project overview
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
0
Merge Requests
2
CI / CD
Security & Compliance
Packages
Wiki
Snippets
Members
Collapse sidebar
Close sidebar
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Minds
unleash-client-php
Commits
1be7aca8
Commit
1be7aca8
authored
21 hours ago
by
Emiliano Balbuena
Browse files
Options
Download
(test): Unleash spec tests
parent
523eb864
goal/clean-up-unleash-client
1 merge request
!2
Clean up and strategies
Pipeline
#105829834
passed with stage
in 1 minute and 5 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
260 additions
and
6 deletions
+260
-6
spec/Minds/UnleashClient/UnleashSpec.php
View file @
1be7aca8
...
...
@@ -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
);
}
}
This diff is collapsed.
src/Unleash.php
View file @
1be7aca8
...
...
@@ -111,7 +111,7 @@ class Unleash
* @return bool
* @throws InvalidArgumentException
*/
p
ublic
function
isCacheInvalid
()
p
rotected
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
*/
p
ublic
function
fetch
()
:
void
p
rotected
function
fetch
()
:
void
{
if
(
!
$this
->
isClientRegistered
)
{
$this
->
logger
->
debug
(
'Client is not registered'
);
...
...
This diff is collapsed.
Please
register
or
sign in
to comment