-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_basic.php
More file actions
170 lines (147 loc) · 5.42 KB
/
test_basic.php
File metadata and controls
170 lines (147 loc) · 5.42 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
<?php
declare(strict_types=1);
require_once __DIR__ . '/autoload.php';
use Signalforge\Validation\MessageFormatter;
use Signalforge\Validation\Validator;
echo "Running basic validation tests...\n\n";
$passed = 0;
$failed = 0;
function test(string $name, bool $condition): void {
global $passed, $failed;
if ($condition) {
echo "[PASS] $name\n";
$passed++;
} else {
echo "[FAIL] $name\n";
$failed++;
}
}
// Test 1: Basic validation passes
$validator = new Validator([
'email' => ['required', 'email'],
'name' => ['required', 'string', ['min', 2]],
]);
$result = $validator->validate([
'email' => 'user@example.com',
'name' => 'John',
]);
test('Basic validation passes', $result->valid());
// Test 2: Required field validation
$result = $validator->validate([
'email' => '',
'name' => 'John',
]);
test('Required field fails on empty', !$result->valid() && $result->hasError('email'));
// Test 3: Email validation
$validator = new Validator(['email' => ['required', 'email']]);
$result = $validator->validate(['email' => 'not-an-email']);
test('Email validation fails on invalid email', !$result->valid());
// Test 4: Min length validation
$validator = new Validator(['name' => ['required', 'string', ['min', 5]]]);
$result = $validator->validate(['name' => 'Jo']);
test('Min length validation fails', !$result->valid());
// Test 5: Nullable field
$validator = new Validator(['bio' => ['nullable', 'string', ['min', 10]]]);
$result = $validator->validate(['bio' => null]);
test('Nullable field allows null', $result->valid());
// Test 6: Array validation
$validator = new Validator([
'tags' => ['required', 'array', ['min', 1]],
]);
$result = $validator->validate(['tags' => ['php', 'validation']]);
test('Array validation passes', $result->valid());
// Test 7: Wildcard expansion
$validator = new Validator([
'items' => ['required', 'array'],
'items.*.name' => ['required', 'string'],
]);
$result = $validator->validate([
'items' => [
['name' => 'First'],
['name' => 'Second'],
],
]);
test('Wildcard validation passes', $result->valid());
// Test 8: Wildcard with failure
$result = $validator->validate([
'items' => [
['name' => 'First'],
['name' => ''],
],
]);
test('Wildcard validation fails correctly', !$result->valid() && $result->hasError('items.1.name'));
// Test 9: In rule
$validator = new Validator(['role' => ['required', ['in', ['admin', 'user']]]]);
$result = $validator->validate(['role' => 'admin']);
test('In rule passes for valid value', $result->valid());
$result = $validator->validate(['role' => 'superadmin']);
test('In rule fails for invalid value', !$result->valid());
// Test 10: Confirmed rule
$validator = new Validator(['password' => ['required', 'confirmed']]);
$result = $validator->validate([
'password' => 'secret',
'password_confirmation' => 'secret',
]);
test('Confirmed rule passes when matching', $result->valid());
$result = $validator->validate([
'password' => 'secret',
'password_confirmation' => 'different',
]);
test('Confirmed rule fails when not matching', !$result->valid());
// Test 11: OIB validation
$validator = new Validator(['oib' => ['required', 'oib']]);
$result = $validator->validate(['oib' => '12345678903']);
test('OIB validation with valid OIB', $result->valid());
$result = $validator->validate(['oib' => '12345678901']);
test('OIB validation fails with invalid checksum', !$result->valid());
// Test 12: Conditional validation
$validator = new Validator([
'type' => ['required'],
'company_name' => [
['when', ['type', '=', 'business'], [
'required',
'string',
]],
],
]);
$result = $validator->validate(['type' => 'business', 'company_name' => 'Acme']);
test('Conditional validation passes when condition met', $result->valid());
$result = $validator->validate(['type' => 'business']);
test('Conditional validation fails when required and missing', !$result->valid());
$result = $validator->validate(['type' => 'personal']);
test('Conditional validation passes when condition not met', $result->valid());
// Test 13: Between rule
$validator = new Validator(['age' => ['required', 'integer', ['between', 18, 65]]]);
$result = $validator->validate(['age' => 25]);
test('Between rule passes for value in range', $result->valid());
$result = $validator->validate(['age' => 10]);
test('Between rule fails for value below range', !$result->valid());
// Test 14: Validated data
$validator = new Validator([
'name' => ['required', 'string'],
'email' => ['required', 'email'],
]);
$result = $validator->validate([
'name' => 'John',
'email' => 'john@example.com',
'extra' => 'not validated',
]);
$validated = $result->validated();
test('Validated data contains validated fields', isset($validated['name']) && isset($validated['email']));
test('Validated data excludes extra fields', !isset($validated['extra']));
// Test 15: Message formatter
$formatter = new MessageFormatter([
'en' => [
'validation.required' => 'The {field} field is required.',
],
]);
$validator = new Validator(['name' => ['required']]);
$result = $validator->validate([]);
$messages = $formatter->format($result, 'en');
test('Message formatter works', str_contains($messages['name'][0], 'required'));
echo "\n";
echo "=================================\n";
echo "Passed: $passed\n";
echo "Failed: $failed\n";
echo "=================================\n";
exit($failed > 0 ? 1 : 0);