Skip to content

Commit 04e2a37

Browse files
committed
feat(condition): add tests for empty and not empty condition handling with constants
1 parent af27204 commit 04e2a37

4 files changed

Lines changed: 122 additions & 10 deletions

File tree

src/Kernel/RuleEngine/PHPSandbox/PHPSandboxRuleEngineClient.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ public function getConditionItemCode(ConditionItem $conditionItem): string
9494
$eq = '==';
9595
$notEq = '!=';
9696
}
97+
98+
// Handle Empty and NotEmpty comparisons specially for constant values
99+
if (in_array($conditionItem->getCompareType(), [CompareType::Empty, CompareType::NotEmpty, CompareType::Valuable, CompareType::NoValuable], true)
100+
&& ! (str_starts_with($left, '($') || str_starts_with($left, '$'))) {
101+
// Determine if the constant value is empty
102+
$isEmpty = ($left === null || $left === 'null' || $left === '' || $left === '0' || $left === 'false' || $left === '[]');
103+
104+
return match ($conditionItem->getCompareType()) {
105+
CompareType::Empty, CompareType::Valuable => $isEmpty ? 'true' : 'false',
106+
CompareType::NotEmpty, CompareType::NoValuable => $isEmpty ? 'false' : 'true',
107+
default => 'false',
108+
};
109+
}
110+
97111
return match ($conditionItem->getCompareType()) {
98112
CompareType::Equals => "{$left} {$eq} {$right}",
99113
CompareType::NoEquals => "{$left} {$notEq} {$right}",

tests/Structure/Api/ApiSendTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
namespace Dtyq\FlowExprEngine\Test\Structure\Api;
99

10-
use Dtyq\FlowExprEngine\Structure\Api\ApiRequestOptions;
11-
use Dtyq\FlowExprEngine\Structure\Api\ApiSend;
12-
use Dtyq\FlowExprEngine\Structure\Api\Safe\DefenseAgainstSSRFOptions;
1310
use Dtyq\FlowExprEngine\Test\BaseTestCase;
1411

1512
/**

tests/Structure/Api/ApiTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@
77

88
namespace Dtyq\FlowExprEngine\Test\Structure\Api;
99

10-
use Dtyq\FlowExprEngine\Builder\ApiBuilder;
11-
use Dtyq\FlowExprEngine\ComponentFactory;
12-
use Dtyq\FlowExprEngine\Structure\Api\Api;
13-
use Dtyq\FlowExprEngine\Structure\Api\Safe\DefenseAgainstSSRFOptions;
14-
use Dtyq\FlowExprEngine\Structure\Api\StandardIO\Request as StandardRequest;
15-
use Dtyq\FlowExprEngine\Structure\Api\StandardIO\Response as StandardResponse;
16-
use Dtyq\FlowExprEngine\Structure\StructureType;
1710
use Dtyq\FlowExprEngine\Test\BaseTestCase;
1811

1912
/**

tests/Structure/Condition/ConditionTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,114 @@ public function testNumberCompatible()
104104
$this->assertTrue($condition->getResult(['518002981345849344' => ['content' => 1]]));
105105
}
106106

107+
public function testErrorInput()
108+
{
109+
$conditionArray = json_decode(
110+
<<<'JSON'
111+
{
112+
"ops": "AND",
113+
"children": [
114+
{
115+
"type": "compare",
116+
"left_operands": {
117+
"type": "const",
118+
"const_value": [
119+
{
120+
"type": "input",
121+
"uniqueId": "727422814327410688",
122+
"value": "1"
123+
}
124+
],
125+
"expression_value": []
126+
},
127+
"condition": "not_empty",
128+
"right_operands": {
129+
"type": "const",
130+
"const_value": [],
131+
"expression_value": []
132+
}
133+
}
134+
]
135+
}
136+
JSON,
137+
true
138+
);
139+
$condition = $this->builder->build($conditionArray);
140+
$this->assertEquals('((true))', $condition->getCode());
141+
$this->assertTrue($condition->getResult());
142+
}
143+
144+
public function testEmptyConditionWithConstant()
145+
{
146+
// Test with non-empty constant
147+
$conditionArray = json_decode(
148+
<<<'JSON'
149+
{
150+
"ops": "AND",
151+
"children": [
152+
{
153+
"type": "compare",
154+
"left_operands": {
155+
"type": "const",
156+
"const_value": [
157+
{
158+
"type": "input",
159+
"value": "1"
160+
}
161+
],
162+
"expression_value": []
163+
},
164+
"condition": "empty",
165+
"right_operands": {
166+
"type": "const",
167+
"const_value": [],
168+
"expression_value": []
169+
}
170+
}
171+
]
172+
}
173+
JSON,
174+
true
175+
);
176+
$condition = $this->builder->build($conditionArray);
177+
$this->assertEquals('((false))', $condition->getCode());
178+
$this->assertFalse($condition->getResult());
179+
180+
// Test with empty constant
181+
$conditionArray = json_decode(
182+
<<<'JSON'
183+
{
184+
"ops": "AND",
185+
"children": [
186+
{
187+
"type": "compare",
188+
"left_operands": {
189+
"type": "const",
190+
"const_value": [
191+
{
192+
"type": "input",
193+
"value": ""
194+
}
195+
],
196+
"expression_value": []
197+
},
198+
"condition": "empty",
199+
"right_operands": {
200+
"type": "const",
201+
"const_value": [],
202+
"expression_value": []
203+
}
204+
}
205+
]
206+
}
207+
JSON,
208+
true
209+
);
210+
$condition = $this->builder->build($conditionArray);
211+
$this->assertEquals('((false))', $condition->getCode());
212+
$this->assertFalse($condition->getResult());
213+
}
214+
107215
public function testNumberCondition()
108216
{
109217
$conditionArray = json_decode(

0 commit comments

Comments
 (0)