Skip to content

Commit

Permalink
Fix: Single value constraints (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed May 22, 2023
1 parent 65d80ea commit cf3bf94
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
8 changes: 3 additions & 5 deletions src/ConstraintValidator/DefaultConstraintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
62 changes: 47 additions & 15 deletions tests/ConstraintValidator/ConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -27,7 +33,10 @@ public function testInvalidVersion()
$this->metricsHandler,
new DefaultVariantHandler(new MurmurHashCalculator())
);
}

public function testInvalidVersion()
{
$this->pushResponse([
'version' => 1,
'features' => [
Expand All @@ -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' => [
Expand All @@ -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'));
}
}

0 comments on commit cf3bf94

Please sign in to comment.