|
| 1 | +<?php |
| 2 | + |
| 3 | +use Soap\LaravelWorkflowProcess\GuardEvaluator; |
| 4 | +use Soap\LaravelWorkflowProcess\Tests\Stubs\CheckFlagGuardFunction; |
| 5 | +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
| 6 | + |
| 7 | +it('evaluates a custom closure guard function correctly', function () { |
| 8 | + // Set a test configuration for custom guard functions. |
| 9 | + // Here we define a function named "checkFlag" that returns the value of a 'flag' variable. |
| 10 | + config()->set('workflow.custom_functions', [ |
| 11 | + 'checkFlag' => [ |
| 12 | + 'compiler' => function () { |
| 13 | + // The compiler returns a placeholder expression. |
| 14 | + return 'true'; |
| 15 | + }, |
| 16 | + 'evaluator' => function (array $variables) { |
| 17 | + // Evaluate by returning the 'flag' variable or false if not set. |
| 18 | + return $variables['flag'] ?? false; |
| 19 | + }, |
| 20 | + ], |
| 21 | + ]); |
| 22 | + |
| 23 | + // Instantiate the ExpressionLanguage and GuardEvaluator. |
| 24 | + $expressionLanguage = new ExpressionLanguage; |
| 25 | + $guardEvaluator = new GuardEvaluator($expressionLanguage); |
| 26 | + |
| 27 | + // Evaluate the expression with 'flag' set to true. |
| 28 | + $resultTrue = $guardEvaluator->evaluate('checkFlag()', ['flag' => true]); |
| 29 | + expect($resultTrue)->toBeTrue(); |
| 30 | + |
| 31 | + // Evaluate the expression with 'flag' set to false. |
| 32 | + $resultFalse = $guardEvaluator->evaluate('checkFlag()', ['flag' => false]); |
| 33 | + expect($resultFalse)->toBeFalse(); |
| 34 | +}); |
| 35 | + |
| 36 | +it('evaluates a custom guard function defined as a class correctly', function () { |
| 37 | + // Set the configuration so that the custom function points to our evaluator class. |
| 38 | + config()->set('workflow.custom_functions', [ |
| 39 | + 'checkFlag' => CheckFlagGuardFunction::class, |
| 40 | + ]); |
| 41 | + |
| 42 | + // Instantiate the ExpressionLanguage and GuardEvaluator. |
| 43 | + $expressionLanguage = new ExpressionLanguage; |
| 44 | + $guardEvaluator = new GuardEvaluator($expressionLanguage); |
| 45 | + |
| 46 | + // Evaluate the expression with 'flag' set to true. |
| 47 | + $resultTrue = $guardEvaluator->evaluate('checkFlag()', ['flag' => true]); |
| 48 | + expect($resultTrue)->toBeTrue(); |
| 49 | + |
| 50 | + // Evaluate the expression with 'flag' set to false. |
| 51 | + $resultFalse = $guardEvaluator->evaluate('checkFlag()', ['flag' => false]); |
| 52 | + expect($resultFalse)->toBeFalse(); |
| 53 | +}); |
0 commit comments