Skip to content

Commit

Permalink
Add tests for iterators (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Aug 18, 2021
1 parent f21af33 commit 48ae6cb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/Client/DefaultRegistrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public function register(iterable $strategyHandlers): bool
return true;
}
if (!is_array($strategyHandlers)) {
// @codeCoverageIgnoreStart
$strategyHandlers = iterator_to_array($strategyHandlers);
// @codeCoverageIgnoreEnd
}
$request = $this->requestFactory
->createRequest('POST', $this->configuration->getUrl() . 'client/register')
Expand Down
2 changes: 0 additions & 2 deletions src/DefaultUnleash.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public function isEnabled(string $featureName, ?Context $context = null, bool $d

$strategies = $feature->getStrategies();
if (!is_countable($strategies)) {
// @codeCoverageIgnoreStart
$strategies = iterator_to_array($strategies);
// @codeCoverageIgnoreEnd
}
if (!count($strategies)) {
$this->metricsHandler->handleMetrics($feature, true);
Expand Down
6 changes: 5 additions & 1 deletion tests/Client/DefaultRegistrationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Unleash\Client\Tests\Client;

use ArrayIterator;
use GuzzleHttp\Psr7\HttpFactory;
use Unleash\Client\Client\DefaultRegistrationService;
use Unleash\Client\Configuration\UnleashConfiguration;
Expand All @@ -27,10 +28,13 @@ public function testRegister()
$configuration
);

$this->pushResponse([], 1, 202);
$this->pushResponse([], 2, 202);
self::assertTrue($instance->register([
new DefaultStrategyHandler(),
]));
self::assertTrue($instance->register(new ArrayIterator([
new DefaultStrategyHandler(),
])));

$this->pushResponse([
'type' => 'password',
Expand Down
43 changes: 43 additions & 0 deletions tests/DefaultUnleashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

namespace Unleash\Client\Tests;

use ArrayIterator;
use LimitIterator;
use Unleash\Client\Configuration\UnleashConfiguration;
use Unleash\Client\Configuration\UnleashContext;
use Unleash\Client\DefaultUnleash;
use Unleash\Client\DTO\DefaultFeature;
use Unleash\Client\DTO\DefaultStrategy;
use Unleash\Client\DTO\Feature;
use Unleash\Client\DTO\Variant;
use Unleash\Client\Metrics\MetricsHandler;
use Unleash\Client\Repository\UnleashRepository;
use Unleash\Client\Stickiness\MurmurHashCalculator;
use Unleash\Client\Strategy\DefaultStrategyHandler;
use Unleash\Client\Strategy\GradualRolloutStrategyHandler;
Expand Down Expand Up @@ -452,6 +457,44 @@ public function testRegister()
self::assertCount(3, $this->requestHistory);
}

public function testIterators()
{
$repository = new class implements UnleashRepository {
private $cache = [];

public function findFeature(string $featureName): ?Feature
{
if (!isset($this->cache[$featureName])) {
$this->cache[$featureName] = new DefaultFeature(
$featureName,
true,
new LimitIterator(new ArrayIterator([new DefaultStrategy('default')])),
);
}

return $this->cache[$featureName];
}

public function getFeatures(): iterable
{
return [];
}
};

$instance = new DefaultUnleash(
new ArrayIterator([new DefaultStrategyHandler()]),
$repository,
$this->registrationService,
(new UnleashConfiguration('', '', ''))
->setAutoRegistrationEnabled(false)
->setCache($this->getCache()),
$this->metricsHandler,
new DefaultVariantHandler(new MurmurHashCalculator())
);
self::assertTrue($instance->isEnabled('someFeature'));
self::assertTrue($instance->isEnabled('someFeature'));
}

private function getInstance(StrategyHandler ...$handlers): DefaultUnleash
{
return new DefaultUnleash(
Expand Down

0 comments on commit 48ae6cb

Please sign in to comment.