Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Single value constraints #152

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
}
}