From 0a10a961e8eaf7e92fb460f772c1a9e8504670a5 Mon Sep 17 00:00:00 2001 From: Dominik Chrastecky Date: Tue, 22 Mar 2022 15:28:09 +0100 Subject: [PATCH] Add tests for invalid version value and invalid operator, don't throw on invalid operator --- .../DefaultConstraintValidator.php | 3 +- tests/AbstractHttpClientTest.php | 34 +++++++ tests/ConstraintValidator/ConstraintTest.php | 97 +++++++++++++++++++ tests/DefaultUnleashTest.php | 17 ---- 4 files changed, 132 insertions(+), 19 deletions(-) create mode 100644 tests/ConstraintValidator/ConstraintTest.php diff --git a/src/ConstraintValidator/DefaultConstraintValidator.php b/src/ConstraintValidator/DefaultConstraintValidator.php index 517abb61..91baadd6 100644 --- a/src/ConstraintValidator/DefaultConstraintValidator.php +++ b/src/ConstraintValidator/DefaultConstraintValidator.php @@ -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; @@ -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, }; } } diff --git a/tests/AbstractHttpClientTest.php b/tests/AbstractHttpClientTest.php index 46988ad7..47118401 100755 --- a/tests/AbstractHttpClientTest.php +++ b/tests/AbstractHttpClientTest.php @@ -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 { @@ -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(); @@ -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 diff --git a/tests/ConstraintValidator/ConstraintTest.php b/tests/ConstraintValidator/ConstraintTest.php new file mode 100644 index 00000000..b7353a71 --- /dev/null +++ b/tests/ConstraintValidator/ConstraintTest.php @@ -0,0 +1,97 @@ +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')); + } +} diff --git a/tests/DefaultUnleashTest.php b/tests/DefaultUnleashTest.php index a2237709..612d69c2 100755 --- a/tests/DefaultUnleashTest.php +++ b/tests/DefaultUnleashTest.php @@ -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; @@ -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());