Skip to content

Commit 209a3d9

Browse files
committed
test(policy): use data providers in PolicySpec validation tests
Signed-off-by: Vitor Mattos <[email protected]>
1 parent 6aa5f3b commit 209a3d9

1 file changed

Lines changed: 34 additions & 7 deletions

File tree

tests/php/Unit/Service/Policy/Model/PolicySpecTest.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,56 @@ public function testValidationUsesProvidedContext(): void {
7676
$spec->validateValue('parallel', new PolicyContext());
7777
}
7878

79-
public function testValidationAllowsAnyValueWhenAllowedValuesIsEmpty(): void {
79+
/**
80+
* @dataProvider provideUnconstrainedValues
81+
*/
82+
public function testValidationAllowsAnyValueWhenAllowedValuesIsEmpty(mixed $value): void {
8083
$spec = new PolicySpec(
8184
key: 'signature_text_template',
8285
defaultSystemValue: '',
8386
allowedValues: [],
8487
);
8588

86-
$spec->validateValue('any free text', new PolicyContext());
87-
$spec->validateValue(12.5, new PolicyContext());
89+
$spec->validateValue($value, new PolicyContext());
8890

8991
$this->addToAssertionCount(1);
9092
}
9193

92-
public function testValidationStillRejectsValueWhenAllowedValuesIsDefined(): void {
94+
/**
95+
* @dataProvider provideConstrainedValidationCases
96+
*/
97+
public function testValidationAgainstDefinedAllowedValues(string $value, bool $shouldThrow): void {
9398
$spec = new PolicySpec(
9499
key: 'signature_render_mode',
95100
defaultSystemValue: 'default',
96101
allowedValues: ['default', 'graphic', 'text'],
97102
);
98103

99-
$spec->validateValue('graphic', new PolicyContext());
104+
if ($shouldThrow) {
105+
$this->expectException(\InvalidArgumentException::class);
106+
}
100107

101-
$this->expectException(\InvalidArgumentException::class);
102-
$spec->validateValue('unsupported_mode', new PolicyContext());
108+
$spec->validateValue($value, new PolicyContext());
109+
110+
if (!$shouldThrow) {
111+
$this->assertTrue(true);
112+
}
113+
}
114+
115+
/** @return array<string, array{0: mixed}> */
116+
public static function provideUnconstrainedValues(): array {
117+
return [
118+
'text value' => ['any free text'],
119+
'float value' => [12.5],
120+
'boolean value' => [true],
121+
];
122+
}
123+
124+
/** @return array<string, array{0: string, 1: bool}> */
125+
public static function provideConstrainedValidationCases(): array {
126+
return [
127+
'allowed value' => ['graphic', false],
128+
'disallowed value' => ['unsupported_mode', true],
129+
];
103130
}
104131
}

0 commit comments

Comments
 (0)