forked from tempestphp/tempest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatorTest.php
More file actions
340 lines (268 loc) Β· 11.2 KB
/
Copy pathValidatorTest.php
File metadata and controls
340 lines (268 loc) Β· 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
declare(strict_types=1);
namespace Tempest\Validation\Tests;
use PHPUnit\Framework\TestCase;
use Tempest\Reflection\ClassReflector;
use Tempest\Validation\Exceptions\ValidationFailed;
use Tempest\Validation\FailingRule;
use Tempest\Validation\HasErrorMessage;
use Tempest\Validation\Rule;
use Tempest\Validation\Rules\HasLength;
use Tempest\Validation\Rules\IsBoolean;
use Tempest\Validation\Rules\IsEmail;
use Tempest\Validation\Rules\IsEnum;
use Tempest\Validation\Rules\IsFloat;
use Tempest\Validation\Rules\IsInteger;
use Tempest\Validation\Rules\IsNotNull;
use Tempest\Validation\Rules\IsString;
use Tempest\Validation\Tests\Fixtures\ObjectToBeValidated;
use Tempest\Validation\Tests\Fixtures\ObjectWithBoolProp;
use Tempest\Validation\Tests\Fixtures\ObjectWithEnumProp;
use Tempest\Validation\Tests\Fixtures\ObjectWithFloatProp;
use Tempest\Validation\Tests\Fixtures\ObjectWithIntProp;
use Tempest\Validation\Tests\Fixtures\ObjectWithObjectProperty;
use Tempest\Validation\Tests\Fixtures\ObjectWithSkipValidation;
use Tempest\Validation\Tests\Fixtures\ObjectWithStringProperty;
use Tempest\Validation\Tests\Fixtures\ValidateObjectA;
use Tempest\Validation\Validator;
/**
* @internal
*/
final class ValidatorTest extends TestCase
{
private Validator $validator {
get => new Validator(new NullTranslator());
}
public function test_validate(): void
{
try {
$this->validator->validateObject(new ObjectToBeValidated(name: 'a'));
$this->fail('Expected ValidationFailed to be thrown.');
} catch (ValidationFailed $e) {
$this->assertArrayHasKey('name', $e->failingRules);
$this->assertCount(1, $e->failingRules['name']);
$this->assertInstanceOf(HasLength::class, $e->failingRules['name'][0]->rule);
$this->assertSame('name', $e->failingRules['name'][0]->field);
$this->assertSame('a', $e->failingRules['name'][0]->value);
$this->assertSame(ObjectToBeValidated::class, $e->targetClass);
}
}
public function test_validate_passes_with_valid_object(): void
{
$this->validator->validateObject(new ObjectToBeValidated(name: 'ab'));
$this->expectNotToPerformAssertions();
}
public function test_validate_value_for_property_sets_field_and_value(): void
{
$property = new ClassReflector(ValidateObjectA::class)->getProperty('title');
$failingRules = $this->validator->validateValueForProperty($property, 123);
$this->assertCount(1, $failingRules);
$this->assertSame('title', $failingRules[0]->field);
$this->assertSame(123, $failingRules[0]->value);
}
public function test_validate_value(): void
{
$failingRules = $this->validator->validateValue('a', [new IsEmail()]);
$this->assertCount(1, $failingRules);
}
public function test_closure_fails_with_false_response(): void
{
$failingRules = $this->validator->validateValue('a', fn (mixed $_) => false);
$this->assertCount(1, $failingRules);
}
public function test_closure_fails_with_string_response(): void
{
$failingRules = $this->validator->validateValue('a', fn (mixed $_) => 'I expected b');
$rule = $failingRules[0]->rule;
$this->assertCount(1, $failingRules);
$this->assertInstanceOf(HasErrorMessage::class, $rule);
$this->assertSame('I expected b', $rule->getErrorMessage());
}
public function test_closure_passes_with_null_response(): void
{
$validator = $this->validator;
$validator->validateValue('a', fn (mixed $_) => null);
$this->expectNotToPerformAssertions();
}
public function test_closure_passes_with_true_response(): void
{
$validator = $this->validator;
$validator->validateValue('a', fn (mixed $_) => true);
$this->expectNotToPerformAssertions();
}
public function test_closure_passes(): void
{
$validator = $this->validator;
$validator->validateValue('a', fn (mixed $value) => $value === 'a');
$validator->validateValue('a', fn (mixed $value) => $value === 'a');
$this->expectNotToPerformAssertions();
}
public function test_nested_property_validation(): void
{
$validator = $this->validator;
$class = new ClassReflector(ValidateObjectA::class);
$failingRules = $validator->validateValuesForClass($class, []);
$this->assertCount(7, $failingRules);
$this->assertInstanceOf(IsNotNull::class, $failingRules['b'][0]->rule);
$this->assertInstanceOf(IsString::class, $failingRules['title'][0]->rule);
$failingRules = $validator->validateValuesForClass($class, [
'b' => [
'name' => '',
],
]);
$this->assertArrayNotHasKey('b', $failingRules);
$this->assertCount(1, $failingRules['b.c']);
$this->assertCount(1, $failingRules['b.name']);
$this->assertCount(1, $failingRules['b.age']);
$failingRules = $validator->validateValuesForClass($class, [
'b' => [
'c' => [
'name' => '',
],
],
]);
$this->assertCount(1, $failingRules['b.c.name']);
$failingRules = $validator->validateValuesForClass($class, [
'title' => 'test',
'b' => [
'name' => 'test',
'age' => 1,
'c' => [
'name' => 'test',
'email' => 'brendt@stitcher.io',
],
],
]);
$this->assertEmpty($failingRules);
}
public function test_nested_property_validation_with_dotted_keys(): void
{
$validator = $this->validator;
$class = new ClassReflector(ValidateObjectA::class);
$failingRules = $validator->validateValuesForClass($class, [
'title' => 'test',
'b.name' => 'test',
'b.age' => 1,
'b.c.name' => 'test',
'b.c.email' => 'brendt@stitcher.io',
]);
$this->assertEmpty($failingRules);
$failingRules = $validator->validateValuesForClass($class, [
'title' => 'test',
'b.age' => 1,
]);
$this->assertCount(4, $failingRules);
}
public function test_validation_infers_string_rule_from_property_type(): void
{
$failingRules = $this->validator->validateValuesForClass(ObjectWithStringProperty::class, ['prop' => (object) []]);
$this->assertCount(1, $failingRules['prop']);
$this->assertInstanceOf(IsString::class, $failingRules['prop'][0]->rule);
}
public function test_validation_infers_int_rule_from_property_type(): void
{
$failingRules = $this->validator->validateValuesForClass(ObjectWithIntProp::class, ['prop' => 'a']);
$this->assertCount(1, $failingRules['prop']);
$this->assertInstanceOf(IsInteger::class, $failingRules['prop'][0]->rule);
}
public function test_validation_infers_float_rule_from_property_type(): void
{
$failingRules = $this->validator->validateValuesForClass(ObjectWithFloatProp::class, ['prop' => 'a']);
$this->assertCount(1, $failingRules['prop']);
$this->assertInstanceOf(IsFloat::class, $failingRules['prop'][0]->rule);
}
public function test_validation_infers_bool_rule_from_property_type(): void
{
$failingRules = $this->validator->validateValuesForClass(ObjectWithBoolProp::class, ['prop' => 'a']);
$this->assertCount(1, $failingRules['prop']);
$this->assertInstanceOf(IsBoolean::class, $failingRules['prop'][0]->rule);
}
public function test_validation_infers_enum_rule_from_property_type(): void
{
$failingRules = $this->validator->validateValuesForClass(ObjectWithEnumProp::class, ['prop' => 'a']);
$this->assertCount(1, $failingRules['prop']);
$this->assertInstanceOf(IsEnum::class, $failingRules['prop'][0]->rule);
}
public function test_validation_infers_not_null_from_scalar_property_type(): void
{
$failingRules = $this->validator->validateValuesForClass(ObjectWithStringProperty::class, ['prop' => null]);
$this->assertCount(1, $failingRules['prop']);
$this->assertInstanceOf(IsString::class, $failingRules['prop'][0]->rule);
}
public function test_validation_infers_not_null_from_property_type(): void
{
$failingRules = $this->validator->validateValuesForClass(ObjectWithObjectProperty::class, ['prop' => null]);
$this->assertCount(1, $failingRules['prop']);
$this->assertInstanceOf(IsNotNull::class, $failingRules['prop'][0]->rule);
}
public function test_skip_validation_attribute(): void
{
$failingRules = $this->validator->validateValuesForClass(ObjectWithSkipValidation::class, []);
$this->assertEmpty($failingRules);
}
public function test_validate_values_some_invalid(): void
{
$failingRules = $this->validator->validateValues(
[
'name' => '',
'email' => 'invalid-email',
'age' => 0,
],
[
'name' => [new IsString(), new IsNotNull()],
'email' => [new IsEmail()],
'age' => [new IsInteger(), new IsNotNull()],
],
);
$this->assertCount(1, $failingRules);
$this->assertInstanceOf(IsEmail::class, $failingRules['email'][0]->rule);
}
public function test_validate_values_all_valid(): void
{
$failingRules = $this->validator->validateValues(
[
'name' => '',
'email' => 'foo@bar.baz',
'age' => 0,
],
[
'name' => [new IsString(), new IsNotNull()],
'email' => [new IsEmail()],
'age' => [new IsInteger(), new IsNotNull()],
],
);
$this->assertCount(0, $failingRules);
}
public function test_validator_returns_correct_error_message_when_rule_has_error_message(): void
{
$ruleMessage = $this->validator->getErrorMessage(new IsForTestingHasErrorMessage());
$this->assertSame('This is a test error message.', $ruleMessage);
$failingRuleMessage = $this->validator->getErrorMessage(new FailingRule(rule: new IsForTestingHasErrorMessage()));
$this->assertSame('This is a test error message.', $failingRuleMessage);
}
public function test_validator_returns_correct_error_message(): void
{
$ruleMessage = $this->validator->getErrorMessage(new IsForTestingMessage());
$this->assertSame('validation_error.is_for_testing_message', $ruleMessage);
$failingRuleMessage = $this->validator->getErrorMessage(new FailingRule(rule: new IsForTestingMessage()));
$this->assertSame('validation_error.is_for_testing_message', $failingRuleMessage);
}
}
final readonly class IsForTestingMessage implements Rule
{
public function isValid(mixed $value): bool
{
return false;
}
}
final readonly class IsForTestingHasErrorMessage implements Rule, HasErrorMessage
{
public function isValid(mixed $value): bool
{
return false;
}
public function getErrorMessage(): string
{
return 'This is a test error message.';
}
}