Skip to content

Commit

Permalink
unsupported matcher implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sanzmauro committed Apr 24, 2024
1 parent ffacf6f commit 2841fd1
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/SplitIO/Exception/UnsupportedMatcherException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace SplitIO\Exception;

class UnsupportedMatcherException extends \LogicException
{

}
33 changes: 30 additions & 3 deletions src/SplitIO/Grammar/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
namespace SplitIO\Grammar;

use SplitIO\Exception\InvalidMatcherException;
use SplitIO\Split as SplitApp;
use SplitIO\Grammar\Condition\Combiner\AndCombiner;
use SplitIO\Grammar\Condition\Combiner\CombinerEnum;
use SplitIO\Grammar\Condition\Combiner\Factor\NotFactor;
use SplitIO\Grammar\Condition\ConditionTypeEnum;
use SplitIO\Grammar\Condition\Matcher;
use SplitIO\Grammar\Condition\Partition;
use SplitIO\Grammar\Condition\Matcher\AbstractMatcher;
use SplitIO\Grammar\Condition\ConditionTypeEnum;
use SplitIO\Grammar\Condition\Matcher\Dependency;
use SplitIO\Grammar\Condition\Partition;
use SplitIO\Grammar\Condition\Partition\TreatmentEnum;
use SplitIO\Sdk\Impressions\ImpressionLabel;
use SplitIO\Split as SplitApp;

class Condition
{
Expand Down Expand Up @@ -115,6 +117,31 @@ public function match($key, array $attributes = null, $bucketingKey = null)
return false;
}

public static function getDefaultCondition()
{
return new Condition(array(
'conditionType' => ConditionTypeEnum::WHITELIST,
'matcherGroup' => array(
'combiner' => CombinerEnum::_AND,
'matchers' => array(
array(
'matcherType' => Matcher::ALL_KEYS,
'negate' => false,
'userDefinedSegmentMatcherData' => null,
'whitelistMatcherData' => null
)
)
),
'partitions' => array(
array(
'treatment' => TreatmentEnum::CONTROL,
'size' => 100
)
),
'label' => ImpressionLabel::UNSUPPORTED_MATCHER
));
}

/**
* @return array|null
*/
Expand Down
3 changes: 2 additions & 1 deletion src/SplitIO/Grammar/Condition/Matcher.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace SplitIO\Grammar\Condition;

use SplitIO\Exception\UnsupportedMatcherException;
use SplitIO\Grammar\Condition\Matcher\All;
use SplitIO\Grammar\Condition\Matcher\Between;
use SplitIO\Grammar\Condition\Matcher\EqualTo;
Expand Down Expand Up @@ -131,7 +132,7 @@ public static function factory($matcher)
return new Regex($data, $negate, $attribute);
// @codeCoverageIgnoreStart
default:
return null;
throw new UnsupportedMatcherException("Unable to create matcher for matcher type: ");
}
// @codeCoverageIgnoreEnd
}
Expand Down
15 changes: 11 additions & 4 deletions src/SplitIO/Grammar/Split.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
namespace SplitIO\Grammar;

use SplitIO\Split as SplitApp;
use SplitIO\Component\Common\Di;
use SplitIO\Engine\Hash\HashAlgorithmEnum;
use SplitIO\Split as SplitApp;


class Split
{
Expand Down Expand Up @@ -56,9 +58,14 @@ public function __construct(array $split)
SplitApp::logger()->info("Constructing Feature Flag: ".$this->name);

if (isset($split['conditions']) && is_array($split['conditions'])) {
$this->conditions = array();
foreach ($split['conditions'] as $condition) {
$this->conditions[] = new Condition($condition);
try {
$this->conditions = array();
foreach ($split['conditions'] as $condition) {
$this->conditions[] = new Condition($condition);
}
} catch (\Exception $e) {
Di::getLogger()->debug($e->getMessage());
$this->conditions = array(Condition::getDefaultCondition());
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/SplitIO/Sdk/Impressions/ImpressionLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ class ImpressionLabel
* Label: exception
*/
const EXCEPTION = "exception";

/**
* Condition: unsupported matcher
* Treatment: control
* Label: targeting rule type unsupported by sdk
*/
const UNSUPPORTED_MATCHER = "targeting rule type unsupported by sdk";
}
17 changes: 11 additions & 6 deletions tests/Suite/Matchers/MatchersTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
<?php
namespace SplitIO\Test\Suite\Sdk;

use Monolog\Logger;
use Monolog\Handler\ErrorLogHandler;
use SplitIO\Component\Cache\SegmentCache;
use SplitIO\Component\Cache\SplitCache;
use SplitIO\Grammar\Condition\Matcher;
use SplitIO\Grammar\Condition\Matcher\DataType\DateTime;
use SplitIO\Component\Common\Di;
use \ReflectionMethod;

use SplitIO\Test\Utils;

class MatcherTest extends \PHPUnit\Framework\TestCase
class MatchersTest extends \PHPUnit\Framework\TestCase
{
private function setupSplitApp()
{
Expand Down Expand Up @@ -503,6 +498,16 @@ public function testBooleanMatcher()
$this->assertEquals($meth->invoke($matcher2, 'ff'), false);
}

public function testUnsupportedMatcher()
{
$condition = array(
'matcherType' => 'NEW_MATCHER_TYPE',
);

$this->expectException('\SplitIO\Exception\UnsupportedMatcherException');
Matcher::factory($condition);
}

public static function tearDownAfterClass(): void
{
Utils\Utils::cleanCache();
Expand Down
43 changes: 42 additions & 1 deletion tests/Suite/Sdk/SdkClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ private function validateLastImpression(
$key,
$treatment,
$machineName = 'unknown',
$machineIP = 'unknown'
$machineIP = 'unknown',
$label = ''
) {
$raw = $redisClient->rpop(ImpressionCache::IMPRESSIONS_QUEUE_KEY);
$parsed = json_decode($raw, true);
Expand All @@ -200,6 +201,10 @@ private function validateLastImpression(
$this->assertEquals($parsed['i']['t'], $treatment);
$this->assertEquals($parsed['m']['i'], $machineIP);
$this->assertEquals($parsed['m']['n'], $machineName);

if ($label != '') {
$this->assertEquals($parsed['i']['r'], $label);
}
}

public function testSplitManager()
Expand Down Expand Up @@ -236,6 +241,42 @@ public function testSplitManager()
$this->assertEquals('["set_a","set_b","set_c"]', json_encode($split_views["flagsets_feature"]->getSets()));
}

public function testClientWithUnsupportedMatcher()
{
Di::set(Di::KEY_FACTORY_TRACKER, false);
//Testing version string
$this->assertTrue(is_string(\SplitIO\version()));

$parameters = array(
'scheme' => 'redis',
'host' => REDIS_HOST,
'port' => REDIS_PORT,
'timeout' => 881,
);
$options = array('prefix' => TEST_PREFIX);

$sdkConfig = array(
'log' => array('adapter' => 'stdout'),
'cache' => array('adapter' => 'predis', 'parameters' => $parameters, 'options' => $options)
);

//Initializing the SDK instance.
$splitFactory = \SplitIO\Sdk::factory('asdqwe123456', $sdkConfig);
$splitSdk = $splitFactory->client();
$splitManager = $splitFactory->manager();

//Populating the cache.
Utils\Utils::addSplitsInCache(file_get_contents(__DIR__."/files/splitChanges.json"));
Utils\Utils::addSegmentsInCache(file_get_contents(__DIR__."/files/segmentEmployeesChanges.json"));
Utils\Utils::addSegmentsInCache(file_get_contents(__DIR__."/files/segmentHumanBeignsChanges.json"));

$redisClient = ReflectiveTools::clientFromCachePool(Di::getCache());

//Assertions
$this->assertEquals('control', $splitSdk->getTreatment('user1', 'unsupported_matcher'));
$this->validateLastImpression($redisClient, 'unsupported_matcher', 'user1', 'control', 'unknown', 'unknown', 'targeting rule type unsupported by sdk');
}

public function testClient()
{
Di::set(Di::KEY_FACTORY_TRACKER, false);
Expand Down
64 changes: 64 additions & 0 deletions tests/Suite/Sdk/files/splitChanges.json
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,70 @@
]
}
]
},
{
"orgId": null,
"environment": null,
"trafficTypeId": null,
"trafficTypeName": null,
"name": "unsupported_matcher",
"seed": -1222652054,
"status": "ACTIVE",
"killed": false,
"defaultTreatment": "off",
"sets": ["set_a", "set_b", "set_c"],
"configurations": {
"on": "{\"size\":15,\"test\":20}",
"of": "{\"size\":15,\"defTreatment\":true}"
},
"conditions": [
{
"matcherGroup": {
"combiner": "AND",
"matchers": [
{
"matcherType": "ALL_KEYS",
"negate": false,
"userDefinedSegmentMatcherData": null,
"whitelistMatcherData": null
}
]
},
"partitions": [
{
"treatment": "on",
"size": 100
},
{
"treatment": "off",
"size": 0
}
]
},
{
"matcherGroup": {
"combiner": "AND",
"matchers": [
{
"matcherType": "WRONG_MATCHER",
"negate": false,
"userDefinedSegmentMatcherData": null,
"whitelistMatcherData": {
"whitelist": [
"whitelisted_user"
]
}
}
]
},
"partitions": [
{
"treatment": "on",
"size": 100
}
]
}
]
}
],
"since": -1,
Expand Down

0 comments on commit 2841fd1

Please sign in to comment.