Skip to content

Commit 6b17ce6

Browse files
eceltovkrulis-martin
authored andcommitted
added validator tests
1 parent b58aa5e commit 6b17ce6

1 file changed

Lines changed: 247 additions & 0 deletions

File tree

tests/Validation/Validators.phpt

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
<?php
2+
3+
use App\Helpers\MetaFormats\FormatDefinitions\UserFormat;
4+
use App\Helpers\MetaFormats\Validators\BaseValidator;
5+
use App\Helpers\MetaFormats\Validators\VArray;
6+
use App\Helpers\MetaFormats\Validators\VBool;
7+
use App\Helpers\MetaFormats\Validators\VDouble;
8+
use App\Helpers\MetaFormats\Validators\VInt;
9+
use App\Helpers\MetaFormats\Validators\VMixed;
10+
use App\Helpers\MetaFormats\Validators\VObject;
11+
use App\Helpers\MetaFormats\Validators\VString;
12+
use App\Helpers\MetaFormats\Validators\VTimestamp;
13+
use App\Helpers\MetaFormats\Validators\VUuid;
14+
use Tester\Assert;
15+
16+
$container = require_once __DIR__ . "/../bootstrap.php";
17+
18+
/**
19+
* @testCase
20+
*/
21+
class TestValidators extends Tester\TestCase
22+
{
23+
/** @var Nette\DI\Container */
24+
protected $container;
25+
26+
public function __construct()
27+
{
28+
global $container;
29+
$this->container = $container;
30+
}
31+
32+
private static function getAssertionFailedMessage(
33+
BaseValidator $validator,
34+
mixed $value,
35+
bool $expectedValid,
36+
bool $strict
37+
): string {
38+
$classTokens = explode("\\", get_class($validator));
39+
$class = $classTokens[array_key_last($classTokens)];
40+
$strictString = $strict ? "strict" : "permissive";
41+
$expectedString = $expectedValid ? "valid" : "invalid";
42+
$valueString = json_encode($value);
43+
return "Asserts that the value <$valueString> using $strictString validator <$class> is $expectedString";
44+
}
45+
46+
private static function assertAllValid(BaseValidator $validator, array $values, bool $strict)
47+
{
48+
foreach ($values as $value) {
49+
$failMessage = self::getAssertionFailedMessage($validator, $value, true, $strict);
50+
Assert::true($validator->validate($value), $failMessage);
51+
}
52+
}
53+
54+
private static function assertAllInvalid(BaseValidator $validator, array $values, bool $strict)
55+
{
56+
foreach ($values as $value) {
57+
$failMessage = self::getAssertionFailedMessage($validator, $value, false, $strict);
58+
Assert::false($validator->validate($value), $failMessage);
59+
}
60+
}
61+
62+
/**
63+
* Test a validator against a set of input values. The strictness mode is set automatically by the method.
64+
* @param App\Helpers\MetaFormats\Validators\BaseValidator $validator The validator to be tested.
65+
* @param array $strictValid Valid values in the strict mode.
66+
* @param array $strictInvalid Invalid values in the strict mode.
67+
* @param array $permissiveValid Valid values in the permissive mode.
68+
* @param array $permissiveInvalid Invalid values in the permissive mode.
69+
*/
70+
private static function validatorTester(
71+
BaseValidator $validator,
72+
array $strictValid,
73+
array $strictInvalid,
74+
array $permissiveValid,
75+
array $permissiveInvalid
76+
): void {
77+
// test strict
78+
$validator->setStrict(true);
79+
self::assertAllValid($validator, $strictValid, true);
80+
self::assertAllInvalid($validator, $strictInvalid, true);
81+
// all invalid values in the permissive mode have to be invalid in the strict mode
82+
self::assertAllInvalid($validator, $permissiveInvalid, true);
83+
84+
// test permissive
85+
$validator->setStrict(false);
86+
self::assertAllValid($validator, $permissiveValid, false);
87+
self::assertAllInvalid($validator, $permissiveInvalid, false);
88+
// all valid values in the strict mode have to be valid in the permissive mode
89+
self::assertAllValid($validator, $strictValid, false);
90+
}
91+
92+
public function testVBool()
93+
{
94+
$validator = new VBool();
95+
$strictValid = [true, false];
96+
$strictInvalid = [0, 1, -1, [], "0", "1", "true", "false", "", "text"];
97+
$permissiveValid = [true, false, 0, 1, "0", "1", "true", "false"];
98+
$permissiveInvalid = [-1, [], "", "text"];
99+
self::validatorTester($validator, $strictValid, $strictInvalid, $permissiveValid, $permissiveInvalid);
100+
}
101+
102+
public function testVInt()
103+
{
104+
$validator = new VInt();
105+
$strictValid = [0, 1, -1];
106+
$strictInvalid = [0.0, 2.5, "0", "1", "-1", "0.0", "", false, []];
107+
$permissiveValid = [0, 1, -1, 0.0, "0", "1", "-1", "0.0"];
108+
$permissiveInvalid = ["", 2.5, false, []];
109+
self::validatorTester($validator, $strictValid, $strictInvalid, $permissiveValid, $permissiveInvalid);
110+
}
111+
112+
public function testVTimestamp()
113+
{
114+
// timestamps are just ints (unix timestamps, timestamps can be negative)
115+
$validator = new VTimestamp();
116+
$strictValid = [0, 1, -1];
117+
$strictInvalid = [0.0, 2.5, "0", "1", "-1", "0.0", "", false, []];
118+
$permissiveValid = [0, 1, -1, 0.0, "0", "1", "-1", "0.0"];
119+
$permissiveInvalid = ["", 2.5, false, []];
120+
self::validatorTester($validator, $strictValid, $strictInvalid, $permissiveValid, $permissiveInvalid);
121+
}
122+
123+
public function testVDouble()
124+
{
125+
$validator = new VDouble();
126+
$strictValid = [0, 1, -1, 0.0, 2.5];
127+
$strictInvalid = ["0", "1", "-1", "0.0", "2.5", "", false, []];
128+
$permissiveValid = [0, 1, -1, 0.0, 2.5, "0", "1", "-1", "0.0", "2.5"];
129+
$permissiveInvalid = ["", false, []];
130+
self::validatorTester($validator, $strictValid, $strictInvalid, $permissiveValid, $permissiveInvalid);
131+
}
132+
133+
public function testVArrayShallow()
134+
{
135+
// no nested validators, strictness has no effect
136+
$validator = new VArray();
137+
$valid = [[], [[]], [0], [[], 0]];
138+
$invalid = ["[]", 0, false, ""];
139+
self::validatorTester($validator, $valid, $invalid, $valid, $invalid);
140+
}
141+
142+
public function testVArrayNested()
143+
{
144+
// nested array validator, strictness has no effect
145+
$validator = new VArray(new VArray());
146+
$valid = [[[]], []]; // an array without any nested arrays is still valid (it just has 0 elements)
147+
$invalid = [[0], [[], 0], "[]", 0, false, ""];
148+
self::validatorTester($validator, $valid, $invalid, $valid, $invalid);
149+
}
150+
151+
public function testVArrayNestedInt()
152+
{
153+
// nested int validator, strictness affects int validation
154+
$validator = new VArray(new VInt());
155+
$strictValid = [[], [0]];
156+
$strictInvalid = [["0"], [0.0], [[]], [[], 0], "[]", 0, false, ""];
157+
$permissiveValid = [[], [0], ["0"], [0.0]];
158+
$permissiveInvalid = [[[]], [[], 0], "[]", 0, false, ""];
159+
self::validatorTester($validator, $strictValid, $strictInvalid, $permissiveValid, $permissiveInvalid);
160+
}
161+
162+
public function testVArrayDoublyNestedInt()
163+
{
164+
// doubly nested int validator, strictness affects int validation through the middle array validator
165+
$validator = new VArray(new VArray(new VInt()));
166+
$strictValid = [[], [[]], [[0]]];
167+
$strictInvalid = [[0], [["0"]], [[0.0]], [[], 0], "[]", 0, false, ""];
168+
$permissiveValid = [[], [[]], [[0]], [["0"]], [[0.0]]];
169+
$permissiveInvalid = [[0], [[], 0], "[]", 0, false, ""];
170+
self::validatorTester($validator, $strictValid, $strictInvalid, $permissiveValid, $permissiveInvalid);
171+
}
172+
173+
public function testVStringBasic()
174+
{
175+
// strictness does not affect strings
176+
$validator = new VString();
177+
$valid = ["", "text"];
178+
$invalid = [0, false, []];
179+
self::validatorTester($validator, $valid, $invalid, $valid, $invalid);
180+
}
181+
182+
public function testVStringLength()
183+
{
184+
// strictness does not affect strings
185+
$validator = new VString(minLength: 2);
186+
$valid = ["ab", "text"];
187+
$invalid = ["", "a", 0, false, []];
188+
self::validatorTester($validator, $valid, $invalid, $valid, $invalid);
189+
190+
$validator = new VString(maxLength: 2);
191+
$valid = ["", "a", "ab"];
192+
$invalid = ["abc", "text", 0, false, []];
193+
self::validatorTester($validator, $valid, $invalid, $valid, $invalid);
194+
195+
$validator = new VString(minLength: 2, maxLength: 3);
196+
$valid = ["ab", "abc"];
197+
$invalid = ["", "a", "text", 0, false, []];
198+
self::validatorTester($validator, $valid, $invalid, $valid, $invalid);
199+
}
200+
201+
public function testVStringRegex()
202+
{
203+
// strictness does not affect strings
204+
$validator = new VString(regex: "/^A[0-9a-f]{2}$/");
205+
$valid = ["A2c", "Add", "A00"];
206+
$invalid = ["2c", "a2c", "A2g", "A2cc", "", 0, false, []];
207+
self::validatorTester($validator, $valid, $invalid, $valid, $invalid);
208+
}
209+
210+
public function testVStringComplex()
211+
{
212+
// strictness does not affect strings
213+
$validator = new VString(minLength: 1, maxLength: 2, regex: "/^[0-9a-f]*$/");
214+
$valid = ["a", "aa", "0a"];
215+
$invalid = ["", "g", "aaa", 0, false, []];
216+
self::validatorTester($validator, $valid, $invalid, $valid, $invalid);
217+
}
218+
219+
public function testVUuid()
220+
{
221+
// strictness does not affect strings
222+
$validator = new VUuid();
223+
$valid = ["10000000-2000-4000-8000-160000000000"];
224+
$invalid = ["g0000000-2000-4000-8000-160000000000", "010000000-2000-4000-8000-160000000000", 0, false, []];
225+
self::validatorTester($validator, $valid, $invalid, $valid, $invalid);
226+
}
227+
228+
public function testVMixed()
229+
{
230+
// accepts everything
231+
$validator = new VMixed();
232+
$valid = [0, 1.2, -1, "", false, [], new VMixed()];
233+
$invalid = [];
234+
self::validatorTester($validator, $valid, $invalid, $valid, $invalid);
235+
}
236+
237+
public function testVObject()
238+
{
239+
// accepts all formats (content is not validated, that is done with the checkedAssign method)
240+
$validator = new VObject(UserFormat::class);
241+
$valid = [new UserFormat()];
242+
$invalid = [0, 1.2, -1, "", false, [], new VMixed()];
243+
self::validatorTester($validator, $valid, $invalid, $valid, $invalid);
244+
}
245+
}
246+
247+
(new TestValidators())->run();

0 commit comments

Comments
 (0)