Skip to content

Commit

Permalink
Add tests for invalid version value and invalid operator, don't throw…
Browse files Browse the repository at this point in the history
… on invalid operator (#63)
  • Loading branch information
RikudouSage authored Mar 22, 2022
1 parent d54e7fa commit a763504
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 19 deletions.
3 changes: 1 addition & 2 deletions src/ConstraintValidator/DefaultConstraintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Unleash\Client\ConstraintValidator;

use LogicException;
use Unleash\Client\Configuration\Context;
use Unleash\Client\ConstraintValidator\Operator\Date\DateAfterOperatorValidator;
use Unleash\Client\ConstraintValidator\Operator\Date\DateBeforeOperatorValidator;
Expand Down Expand Up @@ -106,7 +105,7 @@ private function getValidationCallback(string $operator): callable
ConstraintOperator::VERSION_GREATER_THAN => new VersionGreaterThanOperatorValidator(),
ConstraintOperator::VERSION_LOWER_THAN => new VersionLowerThanOperatorValidator(),

default => throw new LogicException("Unsupported operator: {$operator}"),
default => fn () => false,
};
}
}
34 changes: 34 additions & 0 deletions tests/AbstractHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@
use PHPUnit\Framework\TestCase;
use Unleash\Client\Client\DefaultRegistrationService;
use Unleash\Client\Client\RegistrationService;
use Unleash\Client\Configuration\Context;
use Unleash\Client\Configuration\UnleashConfiguration;
use Unleash\Client\DTO\DefaultVariant;
use Unleash\Client\DTO\Feature;
use Unleash\Client\DTO\Variant;
use Unleash\Client\Metrics\MetricsHandler;
use Unleash\Client\Repository\DefaultUnleashRepository;
use Unleash\Client\Tests\Traits\FakeCacheImplementationTrait;
use Unleash\Client\Variant\VariantHandler;

abstract class AbstractHttpClientTest extends TestCase
{
Expand Down Expand Up @@ -49,6 +55,16 @@ abstract class AbstractHttpClientTest extends TestCase
*/
protected $registrationService;

/**
* @var MetricsHandler
*/
protected $metricsHandler;

/**
* @var VariantHandler
*/
protected $variantHandler;

protected function setUp(): void
{
$this->mockHandler = new MockHandler();
Expand All @@ -73,6 +89,24 @@ protected function setUp(): void
(new UnleashConfiguration('', '', ''))
->setCache($this->getCache())
);

$this->metricsHandler = new class implements MetricsHandler {
public function handleMetrics(Feature $feature, bool $successful, Variant $variant = null): void
{
}
};

$this->variantHandler = new class implements VariantHandler {
public function getDefaultVariant(): Variant
{
return new DefaultVariant('test', false);
}

public function selectVariant(Feature $feature, Context $context): ?Variant
{
return null;
}
};
}

protected function tearDown(): void
Expand Down
97 changes: 97 additions & 0 deletions tests/ConstraintValidator/ConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Unleash\Client\Tests\ConstraintValidator;

use Unleash\Client\Configuration\UnleashConfiguration;
use Unleash\Client\Configuration\UnleashContext;
use Unleash\Client\DefaultUnleash;
use Unleash\Client\Stickiness\MurmurHashCalculator;
use Unleash\Client\Strategy\DefaultStrategyHandler;
use Unleash\Client\Tests\AbstractHttpClientTest;
use Unleash\Client\Tests\Traits\FakeCacheImplementationTrait;
use Unleash\Client\Variant\DefaultVariantHandler;

final class ConstraintTest extends AbstractHttpClientTest
{
use FakeCacheImplementationTrait;

public function testInvalidVersion()
{
$instance = new DefaultUnleash(
[new DefaultStrategyHandler()],
$this->repository,
$this->registrationService,
(new UnleashConfiguration('', '', ''))
->setAutoRegistrationEnabled(false)
->setCache($this->getCache()),
$this->metricsHandler,
new DefaultVariantHandler(new MurmurHashCalculator()),
);

$this->pushResponse([
'version' => 1,
'features' => [
[
'name' => 'test',
'description' => '',
'enabled' => true,
'strategies' => [
[
'name' => 'default',
'constraints' => [
[
'contextName' => 'version',
'operator' => 'SEMVER_EQ',
'value' => 'version55',
],
],
],
],
],
],
]);

$context = (new UnleashContext())->setCustomProperty('version', '1.5.5');

self::assertFalse($instance->isEnabled('test', $context));
}

public function testInvalidOperator()
{
$instance = new DefaultUnleash(
[new DefaultStrategyHandler()],
$this->repository,
$this->registrationService,
(new UnleashConfiguration('', '', ''))
->setAutoRegistrationEnabled(false)
->setCache($this->getCache()),
$this->metricsHandler,
new DefaultVariantHandler(new MurmurHashCalculator()),
);

$this->pushResponse([
'version' => 1,
'features' => [
[
'name' => 'test',
'description' => '',
'enabled' => true,
'strategies' => [
[
'name' => 'default',
'constraints' => [
[
'contextName' => 'version',
'operator' => 'invalid_operator',
'value' => 'version55',
],
],
],
],
],
],
]);

self::assertFalse($instance->isEnabled('test'));
}
}
17 changes: 0 additions & 17 deletions tests/DefaultUnleashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
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;
Expand All @@ -26,21 +24,6 @@ final class DefaultUnleashTest extends AbstractHttpClientTest
{
use FakeCacheImplementationTrait;

/**
* @var MetricsHandler
*/
private $metricsHandler;

protected function setUp(): void
{
parent::setUp();
$this->metricsHandler = new class implements MetricsHandler {
public function handleMetrics(Feature $feature, bool $successful, Variant $variant = null): void
{
}
};
}

public function testIsEnabled()
{
$instance = $this->getInstance(new DefaultStrategyHandler());
Expand Down

0 comments on commit a763504

Please sign in to comment.