-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathValidator.customControl.phpt
More file actions
122 lines (90 loc) · 2.39 KB
/
Validator.customControl.phpt
File metadata and controls
122 lines (90 loc) · 2.39 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
<?php
/**
* Test: Nette\Forms\Controls\BaseControl
*/
declare(strict_types=1);
use Nette\Forms\Form;
use Nette\Forms\Validator;
use Nette\Http\FileUpload;
use Nette\Utils\AssertionException;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
class CustomControl implements \Nette\Forms\IControl
{
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function setValue($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
public function validate(): void
{
}
public function getErrors(): array
{
return [];
}
public function isOmitted(): bool
{
return false;
}
}
test(function () { // filled, blank
$input = new CustomControl('');
Assert::false(Validator::validateFilled($input));
Assert::true(Validator::validateBlank($input));
$input = new CustomControl(null);
Assert::false(Validator::validateFilled($input));
Assert::true(Validator::validateBlank($input));
$input = new CustomControl([]);
Assert::false(Validator::validateFilled($input));
Assert::true(Validator::validateBlank($input));
$input = new CustomControl(42);
Assert::true(Validator::validateFilled($input));
Assert::false(Validator::validateBlank($input));
});
test(function () { // file upload related validators
$input = new CustomControl(new FileUpload([
'name' => 'foo',
'size' => 1,
'tmp_name' => __FILE__,
'error' => UPLOAD_ERR_OK
]));
Assert::true(Validator::validateFileSize($input, 42));
Assert::true(Validator::validateMimeType($input, ['text/x-php']));
Assert::false(Validator::validateImage($input));
$input = new CustomControl(new FileUpload([
'name' => 'foo',
'size' => 100,
'tmp_name' => __DIR__ . '/files/logo.gif',
'error' => UPLOAD_ERR_OK
]));
Assert::false(Validator::validateFileSize($input, 42));
Assert::false(Validator::validateMimeType($input, ['text/x-php']));
Assert::true(Validator::validateImage($input));
Assert::exception(
function () : void {
Assert::false(Validator::validateFileSize(new CustomControl('foo'), 42));
},
AssertionException::class
);
Assert::exception(
function () : void {
Assert::false(Validator::validateMimeType(new CustomControl('foo'), ['plain/text']));
},
AssertionException::class
);
Assert::exception(
function () : void {
Assert::false(Validator::validateImage(new CustomControl('foo')));
},
AssertionException::class
);
});