Skip to content

Fix handling of booleans in conditions #35

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 9 additions & 20 deletions src/EvaluationCore/EvaluationEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,16 @@ function ($value) {

private function matchesIs(string $propValue, array $filterValues): bool
{
if ($this->containsBooleans($filterValues)) {
$lower = strtolower($propValue);
if ($lower === 'true' || $lower === 'false') {
foreach ($filterValues as $value) {
if (strtolower($value) === $lower) {
return true;
}
}
}
$lowerFilterValues = array_map('strtolower', $filterValues);
$lowerPropValue = strtolower($propValue);
if (in_array('true', $lowerFilterValues) && in_array($lowerPropValue, ['true', '1'])) {
return true;
}

if (in_array('false', $lowerFilterValues) && in_array($lowerPropValue, ['false', '0'])) {
return true;
}

return in_array($propValue, $filterValues);
}

Expand Down Expand Up @@ -350,17 +350,6 @@ private function containsNone(array $filterValues): bool
return in_array('(none)', $filterValues);
}

private function containsBooleans(array $filterValues): bool
{
foreach ($filterValues as $filterValue) {
$lowercaseFilterValue = strtolower($filterValue);
if ($lowercaseFilterValue === 'true' || $lowercaseFilterValue === 'false') {
return true;
}
}
return false;
}

private function parseNumber(string $value): ?int
{
$parsedValue = filter_var($value, FILTER_VALIDATE_INT);
Expand Down
113 changes: 113 additions & 0 deletions tests/EvaluationCore/EvaluationEngineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace AmplitudeExperiment\Test\EvaluationCore;

use AmplitudeExperiment\EvaluationCore\EvaluationEngine;
use PHPUnit\Framework\TestCase;

final class EvaluationEngineTest extends TestCase
{
private EvaluationEngine $evaluation;

protected function setUp() : void
{
parent::setUp();

$this->evaluation = new EvaluationEngine();
}

public function booleanValues() : iterable
{
yield ['true', ['true']];
yield ['true', ['True']];

yield ['True', ['true']];
yield ['True', ['True']];

yield [true, ['true']];
yield [true, ['True']];
yield [true, ['1']];

yield ['1', ['true']];
yield ['1', ['True']];
yield ['1', ['1']];

yield ['false', ['false']];
yield ['false', ['False']];

yield ['False', ['false']];
yield ['False', ['False']];

yield ['0', ['false']];
yield ['0', ['False']];
yield ['0', ['0']];
}

/**
* @dataProvider booleanValues
*/
public function testBooleans($propValue, array $filterValues) : void
{
self::assertSame(
[
'feature1' => [
'key' => 'on',
'value' => 'on',
'metadata' => [
'segmentName' => 'Employee only'
],
],
],
$this->evaluation->evaluate([
'user' => [
'user_properties' => [
'isEmployee' => $propValue,
],
],
], [
[
'key' => 'feature1',
'metadata' => [],
'segments' => [
[
'conditions' => [
[
[
'op' => 'is',
'selector' => ['context', 'user', 'user_properties', 'isEmployee'],
'values' => $filterValues,
],
],
],
'metadata' => [
'segmentName' => 'Employee only',
],
'variant' => 'on',
],
[
'metadata' => [
'segmentName' => 'All Other Users',
],
'variant' => 'off',
],
],
'variants' =>
[
'off' => [
'key' => 'off',
'metadata' => [
'default' => true,
],
],
'on' => [
'key' => 'on',
'value' => 'on',
],
],
],
]),
);
}
}
Loading