Skip to content

Commit

Permalink
WB-784: test(stateconfigvalidator): add test for invalid guards confi…
Browse files Browse the repository at this point in the history
…guration

Add a test to validate that guards in state transitions are properly configured as an array or string. Throws an exception for invalid configurations to ensure proper validation.
  • Loading branch information
deligoez committed Dec 12, 2024
1 parent db5f470 commit 7e560c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/StateConfigValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private static function validateTransitionBehaviors(
foreach ($behaviors as $behavior => $label) {
if (isset($transitionConfig[$behavior])) {
try {
$transitionConfig[$behavior] = self::normalizeArrayOrString($transitionConfig[$behavior]);
$transitionConfig[$behavior] = self::normalizeArrayOrString(value: $transitionConfig[$behavior]);
} catch (InvalidArgumentException) {
throw new InvalidArgumentException(
message: "State '{$path}' has invalid {$behavior} configuration for event '{$eventName}'. ".
Expand Down
21 changes: 21 additions & 0 deletions tests/StateConfigValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@
exceptionMessage: "State 'state_a' has invalid condition in transition for event 'EVENT'. Each condition must be an array with target/guards/actions."
);
});

test('validates guards configuration in transitions', function (): void {
expect(fn () => MachineDefinition::define([
'id' => 'machine',
'initial' => 'state_a',
'states' => [
'state_a' => [
'on' => [
'EVENT' => [
'target' => 'state_b',
'guards' => true, // Guards should be an array or a string
],
],
],
],
]))->toThrow(
InvalidArgumentException::class,
"State 'state_a' has invalid guards configuration for event 'EVENT'. Guards must be an array or string."
);
});

test('validates actions configuration in transitions', function (): void {
expect(fn () => MachineDefinition::define([
'id' => 'machine',
Expand Down

0 comments on commit 7e560c7

Please sign in to comment.