diff --git a/src/ConstraintValidator/DefaultConstraintValidator.php b/src/ConstraintValidator/DefaultConstraintValidator.php index d91a5f72..3456d081 100644 --- a/src/ConstraintValidator/DefaultConstraintValidator.php +++ b/src/ConstraintValidator/DefaultConstraintValidator.php @@ -30,11 +30,9 @@ public function validateConstraint(Constraint $constraint, Context $context): bo $currentValue = $context->findContextValue($field) ?? ''; $callback = $this->getValidationCallback($constraint->getOperator()); - $valueToPass = $constraint->getValues() ?? ( - method_exists($constraint, 'getSingleValue') - ? $constraint->getSingleValue() - : null - ); + $valueToPass = method_exists($constraint, 'getSingleValue') && $constraint->getSingleValue() !== null + ? $constraint->getSingleValue() + : $constraint->getValues(); $isCaseInsensitive = method_exists($constraint, 'isCaseInsensitive') && $constraint->isCaseInsensitive(); if ($isCaseInsensitive) { diff --git a/tests/ConstraintValidator/ConstraintTest.php b/tests/ConstraintValidator/ConstraintTest.php index b1aabc66..1e30ecb4 100644 --- a/tests/ConstraintValidator/ConstraintTest.php +++ b/tests/ConstraintValidator/ConstraintTest.php @@ -2,9 +2,11 @@ namespace Unleash\Client\Tests\ConstraintValidator; +use DateTimeImmutable; use Unleash\Client\Configuration\UnleashConfiguration; use Unleash\Client\Configuration\UnleashContext; use Unleash\Client\DefaultUnleash; +use Unleash\Client\Enum\ConstraintOperator; use Unleash\Client\Stickiness\MurmurHashCalculator; use Unleash\Client\Strategy\DefaultStrategyHandler; use Unleash\Client\Tests\AbstractHttpClientTest; @@ -15,9 +17,13 @@ final class ConstraintTest extends AbstractHttpClientTest { use FakeCacheImplementationTrait; - public function testInvalidVersion() + private DefaultUnleash $instance; + + protected function setUp(): void { - $instance = new DefaultUnleash( + parent::setUp(); + + $this->instance = new DefaultUnleash( [new DefaultStrategyHandler()], $this->repository, $this->registrationService, @@ -27,7 +33,10 @@ public function testInvalidVersion() $this->metricsHandler, new DefaultVariantHandler(new MurmurHashCalculator()) ); + } + public function testInvalidVersion() + { $this->pushResponse([ 'version' => 1, 'features' => [ @@ -53,22 +62,11 @@ public function testInvalidVersion() $context = (new UnleashContext())->setCustomProperty('version', '1.5.5'); - self::assertFalse($instance->isEnabled('test', $context)); + self::assertFalse($this->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' => [ @@ -92,6 +90,40 @@ public function testInvalidOperator() ], ]); - self::assertFalse($instance->isEnabled('test')); + self::assertFalse($this->instance->isEnabled('test')); + } + + /** + * @see https://github.com/Unleash/unleash-client-php/issues/151 + */ + public function testDateBeforeGetValues() + { + $this->pushResponse([ + 'version' => 1, + 'features' => [ + [ + 'name' => 'test', + 'description' => '', + 'enabled' => true, + 'strategies' => [ + [ + 'name' => 'default', + 'constraints' => [ + [ + 'contextName' => 'currentTime', + 'operator' => ConstraintOperator::DATE_BEFORE, + 'value' => (new DateTimeImmutable('+1 day'))->format('c'), + 'values' => [], + 'inverted' => false, + 'caseInsensitive' => false, + ], + ], + ], + ], + ], + ], + ]); + + self::assertTrue($this->instance->isEnabled('test')); } }