Skip to content

Commit c0b0858

Browse files
author
Kenneth Ocastro
committed
Add config for conditionSuccessEvents, add test cases
1 parent 08e820f commit c0b0858

File tree

3 files changed

+563
-0
lines changed

3 files changed

+563
-0
lines changed

src/Rule.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Rule
1212
private ?string $name;
1313
private array $failedConditions = [];
1414
private array $dependencyRules = [];
15+
private array $conditionSuccessEvents = [];
1516

1617
public function __construct(array $options)
1718
{
@@ -28,6 +29,11 @@ public function __construct(array $options)
2829
}
2930
}
3031

32+
public function getConditionSuccessEvents(): array
33+
{
34+
return $this->conditionSuccessEvents;
35+
}
36+
3137
public function getDependencyRules(): array
3238
{
3339
return $this->dependencyRules;
@@ -90,6 +96,9 @@ private function evaluateAll(array $conditions, Facts $facts, array $allRules):
9096
}
9197
}
9298
}
99+
if (isset($condition['event'])) {
100+
$this->conditionSuccessEvents = $condition['event'];
101+
}
93102
return true;
94103
}
95104

@@ -102,6 +111,9 @@ private function evaluateAny(array $conditions, Facts $facts, array $allRules):
102111
$dependencyRule = $allRules[$dependencyRuleName];
103112
$this->dependencyRules[] = $dependencyRule;
104113
if ($dependencyRule->evaluate($facts, $allRules)) {
114+
if (isset($condition['event'])) {
115+
$this->conditionSuccessEvents = $condition['event'];
116+
}
105117
return true;
106118
} else {
107119
$this->failedConditions[] = $condition;
@@ -111,24 +123,36 @@ private function evaluateAny(array $conditions, Facts $facts, array $allRules):
111123
}
112124
} elseif (isset($condition['all'])) {
113125
if ($this->evaluateAll($condition['all'], $facts, $allRules)) {
126+
if (isset($condition['event'])) {
127+
$this->conditionSuccessEvents = $condition['event'];
128+
}
114129
return true;
115130
} else {
116131
$this->failedConditions[] = $condition;
117132
}
118133
} elseif (isset($condition['any'])) {
119134
if ($this->evaluateAny($condition['any'], $facts, $allRules)) {
135+
if (isset($condition['event'])) {
136+
$this->conditionSuccessEvents = $condition['event'];
137+
}
120138
return true;
121139
} else {
122140
$this->failedConditions[] = $condition;
123141
}
124142
} elseif (isset($condition['not'])) {
125143
if (!$this->evaluateCondition($condition['not'], $facts, $allRules)) {
144+
if (isset($condition['event'])) {
145+
$this->conditionSuccessEvents = $condition['event'];
146+
}
126147
return true; // Negate the condition
127148
} else {
128149
$this->failedConditions[] = $condition;
129150
}
130151
} else {
131152
if ($this->evaluateCondition($condition, $facts, $allRules)) {
153+
if (isset($condition['event'])) {
154+
$this->conditionSuccessEvents = $condition['event'];
155+
}
132156
return true;
133157
} else {
134158
$this->failedConditions[] = $condition;
@@ -171,6 +195,10 @@ private function evaluateCondition(array $condition, Facts $facts, array $allRul
171195

172196
public function triggerEvent(Facts $facts): array
173197
{
198+
if (!empty($this->conditionSuccessEvents)) {
199+
return $this->conditionSuccessEvents;
200+
}
201+
174202
return [
175203
'type' => $this->event['type'],
176204
'params' => $this->event['params'],

tests/EngineTest.php

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,265 @@ public function testProfileIsSearchable()
212212
// Step 7: Assert the result matches the expected result
213213
$this->assertEquals($expectedResult, $result);
214214
}
215+
216+
public function testLivenessEncourageSelfieModalCongrats()
217+
{
218+
$rule = json_decode(file_get_contents('tests/data/rule.profile.liveness.encourageSelfieModal.json'), true);
219+
220+
// Helper function to setup and evaluate the engine
221+
$evaluateEngine = function (array $factData) use ($rule) {
222+
$engine = new Engine();
223+
$engine->addRule(new Rule($rule));
224+
$engine->addFact('profile', $factData);
225+
$engine->setTargetRule('rule.profile.liveness.encourageSelfieModal');
226+
return $engine->evaluate();
227+
};
228+
229+
// Test case 1
230+
$result = $evaluateEngine([
231+
'liveness' => [
232+
'hasBadge' => true,
233+
'modalAlreadyShown' => false,
234+
'hasPendingOrApprovedPhotoVerification' => null,
235+
'timeInHoursSinceLastModal' => null,
236+
'dismissCount' => null,
237+
'isLivenessApproved' => null,
238+
'matchCompareFacesAttributeCount' => null,
239+
]
240+
]);
241+
$this->assertEquals('CongratsModal', $result[0]['type']);
242+
243+
// Test case 2
244+
$result2 = $evaluateEngine([
245+
'liveness' => [
246+
'hasBadge' => true,
247+
'modalAlreadyShown' => true,
248+
'hasPendingOrApprovedPhotoVerification' => false,
249+
'timeInHoursSinceLastModal' => 24,
250+
'dismissCount' => 1,
251+
'isLivenessApproved' => true,
252+
'matchCompareFacesAttributeCount' => 2,
253+
]
254+
]);
255+
$this->assertEquals(false, $result2[0]['params']['value']);
256+
257+
// Test case 3
258+
$result3 = $evaluateEngine([
259+
'liveness' => [
260+
'hasBadge' => false,
261+
'modalAlreadyShown' => false,
262+
'hasPendingOrApprovedPhotoVerification' => null,
263+
'timeInHoursSinceLastModal' => 24,
264+
'dismissCount' => 1,
265+
'isLivenessApproved' => null,
266+
'matchCompareFacesAttributeCount' => 0,
267+
]
268+
]);
269+
$this->assertEquals(false, $result3[0]['params']['value']);
270+
}
271+
272+
public function testLivenessEncourageSelfieModalVoluntary()
273+
{
274+
$rule = json_decode(file_get_contents('tests/data/rule.profile.liveness.encourageSelfieModal.json'), true);
275+
276+
// Helper function to setup and evaluate the engine
277+
$evaluateEngine = function (array $factData) use ($rule) {
278+
$engine = new Engine();
279+
$engine->addRule(new Rule($rule));
280+
$engine->addFact('profile', $factData);
281+
$engine->setTargetRule('rule.profile.liveness.encourageSelfieModal');
282+
return $engine->evaluate();
283+
};
284+
285+
// Test case 1
286+
$result = $evaluateEngine([
287+
'liveness' => [
288+
'isLivenessApproved' => false,
289+
'hasPendingOrApprovedPhotoVerification' => false,
290+
'hasBadge' => false,
291+
'modalAlreadyShown' => false,
292+
'timeInHoursSinceLastModal' => 24,
293+
'dismissCount' => 1,
294+
'matchCompareFacesAttributeCount' => 0,
295+
]
296+
]);
297+
$this->assertEquals('VoluntaryModal', $result[0]['type']);
298+
$this->assertEquals('Encourage Selfie Modal shown for VoluntaryModal (Within Dismissal Limit)', $result[0]['params']['message']);
299+
300+
// Test case 2
301+
$result2 = $evaluateEngine([
302+
'liveness' => [
303+
'isLivenessApproved' => false,
304+
'hasPendingOrApprovedPhotoVerification' => false,
305+
'hasBadge' => false,
306+
'modalAlreadyShown' => false,
307+
'timeInHoursSinceLastModal' => 1,
308+
'dismissCount' => 1,
309+
'matchCompareFacesAttributeCount' => 0,
310+
]
311+
]);
312+
$this->assertEquals(false, $result2[0]['params']['value']);
313+
314+
// Test case 3
315+
$result3 = $evaluateEngine([
316+
'liveness' => [
317+
'isLivenessApproved' => false,
318+
'hasPendingOrApprovedPhotoVerification' => false,
319+
'hasBadge' => false,
320+
'modalAlreadyShown' => false,
321+
'timeInHoursSinceLastModal' => 169,
322+
'dismissCount' => 3,
323+
'matchCompareFacesAttributeCount' => 0,
324+
]
325+
]);
326+
$this->assertEquals('VoluntaryModal', $result3[0]['type']);
327+
$this->assertEquals('Encourage Selfie Modal shown for VoluntaryModal (After 7-Day Cooldown)', $result3[0]['params']['message']);
328+
}
329+
330+
public function testLivenessEncourageSelfieModalVerificationJustGotBetter()
331+
{
332+
$rule = json_decode(file_get_contents('tests/data/rule.profile.liveness.encourageSelfieModal.json'), true);
333+
334+
// Helper function to setup and evaluate the engine
335+
$evaluateEngine = function (array $factData) use ($rule) {
336+
$engine = new Engine();
337+
$engine->addRule(new Rule($rule));
338+
$engine->addFact('profile', $factData);
339+
$engine->setTargetRule('rule.profile.liveness.encourageSelfieModal');
340+
return $engine->evaluate();
341+
};
342+
343+
// Test case 1
344+
$result = $evaluateEngine([
345+
'liveness' => [
346+
'isLivenessApproved' => false,
347+
'hasPendingOrApprovedPhotoVerification' => true,
348+
'hasBadge' => false,
349+
'modalAlreadyShown' => false,
350+
'timeInHoursSinceLastModal' => 24,
351+
'dismissCount' => 1,
352+
'matchCompareFacesAttributeCount' => 0,
353+
]
354+
]);
355+
$this->assertEquals('VerificationJustGotBetterModal', $result[0]['type']);
356+
$this->assertEquals('Encourage Selfie Modal shown for VerificationJustGotBetterModal (Within Dismissal Limit)', $result[0]['params']['message']);
357+
358+
// Test case 2
359+
$result2 = $evaluateEngine([
360+
'liveness' => [
361+
'hasBadge' => false,
362+
'hasPendingOrApprovedPhotoVerification' => true,
363+
'modalAlreadyShown' => false,
364+
'timeInHoursSinceLastModal' => 24,
365+
'dismissCount' => 4,
366+
'isLivenessApproved' => false,
367+
'matchCompareFacesAttributeCount' => 0,
368+
]
369+
]);
370+
$this->assertEquals(false, $result2[0]['params']['value']);
371+
372+
// Test case 3
373+
$result3 = $evaluateEngine([
374+
'liveness' => [
375+
'hasBadge' => false,
376+
'hasPendingOrApprovedPhotoVerification' => true,
377+
'modalAlreadyShown' => false,
378+
'timeInHoursSinceLastModal' => 0,
379+
'dismissCount' => 0,
380+
'isLivenessApproved' => false,
381+
'matchCompareFacesAttributeCount' => 0,
382+
]
383+
]);
384+
$this->assertEquals(false, $result3[0]['params']['value']);
385+
386+
// Test case 4
387+
$result4 = $evaluateEngine([
388+
'liveness' => [
389+
'isLivenessApproved' => false,
390+
'hasPendingOrApprovedPhotoVerification' => true,
391+
'hasBadge' => false,
392+
'modalAlreadyShown' => false,
393+
'timeInHoursSinceLastModal' => 13,
394+
'dismissCount' => 0,
395+
'matchCompareFacesAttributeCount' => 0,
396+
]
397+
]);
398+
$this->assertEquals('VerificationJustGotBetterModal', $result4[0]['type']);
399+
$this->assertEquals('Encourage Selfie Modal shown for VerificationJustGotBetterModal (Within Dismissal Limit)', $result4[0]['params']['message']);
400+
401+
// Test case 5
402+
$result = $evaluateEngine([
403+
'liveness' => [
404+
'isLivenessApproved' => false,
405+
'hasBadge' => false,
406+
'hasPendingOrApprovedPhotoVerification' => true,
407+
'modalAlreadyShown' => false,
408+
'timeInHoursSinceLastModal' => 168,
409+
'dismissCount' => 3,
410+
'matchCompareFacesAttributeCount' => 0,
411+
]
412+
]);
413+
$this->assertEquals('VerificationJustGotBetterModal', $result[0]['type']);
414+
$this->assertEquals('Encourage Selfie Modal shown for VerificationJustGotBetterModal (After 7-Day Cooldown)', $result[0]['params']['message']);
415+
}
416+
417+
public function testLivenessEncourageSelfieModalFailedCompareFaces()
418+
{
419+
$rule = json_decode(file_get_contents('tests/data/rule.profile.liveness.encourageSelfieModal.json'), true);
420+
421+
// Helper function to setup and evaluate the engine
422+
$evaluateEngine = function (array $factData) use ($rule) {
423+
$engine = new Engine();
424+
$engine->addRule(new Rule($rule));
425+
$engine->addFact('profile', $factData);
426+
$engine->setTargetRule('rule.profile.liveness.encourageSelfieModal');
427+
return $engine->evaluate();
428+
};
429+
430+
// Test case 1
431+
$result = $evaluateEngine([
432+
'liveness' => [
433+
'isLivenessApproved' => true,
434+
'hasBadge' => false,
435+
'hasPendingOrApprovedPhotoVerification' => true,
436+
'modalAlreadyShown' => false,
437+
'timeInHoursSinceLastModal' => 24,
438+
'dismissCount' => 1,
439+
440+
'matchCompareFacesAttributeCount' => 0,
441+
]
442+
]);
443+
$this->assertEquals('AlmostSelfieVerifiedModal', $result[0]['type']);
444+
$this->assertEquals('Encourage Selfie Modal shown for AlmostSelfieVerifiedModal (Within Dismissal Limit)', $result[0]['params']['message']);
445+
446+
// Test case 2
447+
$result2 = $evaluateEngine([
448+
'liveness' => [
449+
'isLivenessApproved' => true,
450+
'hasBadge' => false,
451+
'hasPendingOrApprovedPhotoVerification' => false,
452+
'modalAlreadyShown' => false,
453+
'timeInHoursSinceLastModal' => 169,
454+
'dismissCount' => 4,
455+
456+
'matchCompareFacesAttributeCount' => 0,
457+
]
458+
]);
459+
$this->assertEquals('AlmostSelfieVerifiedModal', $result2[0]['type']);
460+
$this->assertEquals('Encourage Selfie Modal shown for AlmostSelfieVerifiedModal (After 7-Day Cooldown)', $result2[0]['params']['message']);
461+
462+
// Test case 4
463+
$result3 = $evaluateEngine([
464+
'liveness' => [
465+
'hasBadge' => false,
466+
'hasPendingOrApprovedPhotoVerification' => false,
467+
'modalAlreadyShown' => true,
468+
'timeInHoursSinceLastModal' => 169,
469+
'dismissCount' => 1,
470+
'isLivenessApproved' => true,
471+
'matchCompareFacesAttributeCount' => 1,
472+
]
473+
]);
474+
$this->assertEquals(false, $result3[0]['params']['value']);
475+
}
215476
}

0 commit comments

Comments
 (0)