| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Tests\Module; |
| 6 |
|
| 7 |
use DateTimeImmutable; |
| 8 |
use Packetery\Core\CoreHelper; |
| 9 |
use Packetery\Module\FormValidators; |
| 10 |
use Packetery\Nette\Forms\Controls\TextInput; |
| 11 |
use PHPUnit\Framework\TestCase; |
| 12 |
|
| 13 |
class FormValidatorsTest extends TestCase { |
| 14 |
/** |
| 15 |
* @return array<int, array<int, array<int|string|float|bool|null>>> |
| 16 |
*/ |
| 17 |
public static function validatorsDataProvider(): array { |
| 18 |
return [ |
| 19 |
[ |
| 20 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 21 |
[], |
| 22 |
'20:20', |
| 23 |
true, |
| 24 |
], |
| 25 |
[ |
| 26 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 27 |
[], |
| 28 |
'00:00', |
| 29 |
true, |
| 30 |
], |
| 31 |
[ |
| 32 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 33 |
[], |
| 34 |
'000:00', |
| 35 |
false, |
| 36 |
], |
| 37 |
[ |
| 38 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 39 |
[], |
| 40 |
'00:000', |
| 41 |
false, |
| 42 |
], |
| 43 |
[ |
| 44 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 45 |
[], |
| 46 |
'000:000', |
| 47 |
false, |
| 48 |
], |
| 49 |
[ |
| 50 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 51 |
[], |
| 52 |
'01:08', |
| 53 |
true, |
| 54 |
], |
| 55 |
[ |
| 56 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 57 |
[], |
| 58 |
'23:59', |
| 59 |
true, |
| 60 |
], |
| 61 |
[ |
| 62 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 63 |
[], |
| 64 |
'0:00', |
| 65 |
false, |
| 66 |
], |
| 67 |
[ |
| 68 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 69 |
[], |
| 70 |
'30:00', |
| 71 |
false, |
| 72 |
], |
| 73 |
[ |
| 74 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 75 |
[], |
| 76 |
'20:60', |
| 77 |
false, |
| 78 |
], |
| 79 |
[ |
| 80 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 81 |
[], |
| 82 |
':', |
| 83 |
false, |
| 84 |
], |
| 85 |
[ |
| 86 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 87 |
[], |
| 88 |
'00:0', |
| 89 |
false, |
| 90 |
], |
| 91 |
[ |
| 92 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 93 |
[], |
| 94 |
'', |
| 95 |
false, |
| 96 |
], |
| 97 |
[ |
| 98 |
[ FormValidators::class, 'hasClockTimeFormat' ], |
| 99 |
[], |
| 100 |
null, |
| 101 |
false, |
| 102 |
], |
| 103 |
[ |
| 104 |
[ FormValidators::class, 'greaterThan' ], |
| 105 |
[ 100 ], |
| 106 |
200, |
| 107 |
true, |
| 108 |
], |
| 109 |
[ |
| 110 |
[ FormValidators::class, 'greaterThan' ], |
| 111 |
[ 100.1 ], |
| 112 |
100.2, |
| 113 |
true, |
| 114 |
], |
| 115 |
[ |
| 116 |
[ FormValidators::class, 'greaterThan' ], |
| 117 |
[ 100.01 ], |
| 118 |
100.1, |
| 119 |
true, |
| 120 |
], |
| 121 |
[ |
| 122 |
[ FormValidators::class, 'greaterThan' ], |
| 123 |
[ 100.3 ], |
| 124 |
100.29, |
| 125 |
false, |
| 126 |
], |
| 127 |
[ |
| 128 |
[ FormValidators::class, 'greaterThan' ], |
| 129 |
[ 0 ], |
| 130 |
0, |
| 131 |
false, |
| 132 |
], |
| 133 |
[ |
| 134 |
[ FormValidators::class, 'greaterThan' ], |
| 135 |
[ 0.001 ], |
| 136 |
0, |
| 137 |
false, |
| 138 |
], |
| 139 |
[ |
| 140 |
[ FormValidators::class, 'greaterThan' ], |
| 141 |
[ 0.001 ], |
| 142 |
'', |
| 143 |
false, |
| 144 |
], |
| 145 |
[ |
| 146 |
[ FormValidators::class, 'greaterThan' ], |
| 147 |
[ 0.001 ], |
| 148 |
null, |
| 149 |
false, |
| 150 |
], |
| 151 |
[ |
| 152 |
[ FormValidators::class, 'greaterThan' ], |
| 153 |
[ 0 ], |
| 154 |
0.001, |
| 155 |
true, |
| 156 |
], |
| 157 |
[ |
| 158 |
[ FormValidators::class, 'greaterThan' ], |
| 159 |
[ - 100.3 ], |
| 160 |
- 100.29, |
| 161 |
true, |
| 162 |
], |
| 163 |
[ |
| 164 |
[ FormValidators::class, 'dateIsLater' ], |
| 165 |
[ |
| 166 |
DateTimeImmutable::createFromFormat( CoreHelper::DATEPICKER_FORMAT, '2020-01-01' ) |
| 167 |
->modify( '- 1 day' ) |
| 168 |
->format( CoreHelper::DATEPICKER_FORMAT ), |
| 169 |
], |
| 170 |
'2020-01-01', |
| 171 |
true, |
| 172 |
], |
| 173 |
[ |
| 174 |
[ FormValidators::class, 'dateIsLater' ], |
| 175 |
[ '2020-12-31' ], |
| 176 |
'2021-01-01', |
| 177 |
true, |
| 178 |
], |
| 179 |
[ |
| 180 |
[ FormValidators::class, 'dateIsLater' ], |
| 181 |
[ '2020-12-31' ], |
| 182 |
'2020-01-01', |
| 183 |
false, |
| 184 |
], |
| 185 |
[ |
| 186 |
[ FormValidators::class, 'dateIsLater' ], |
| 187 |
[ '2020-01-01' ], |
| 188 |
'2020-01-01', |
| 189 |
false, |
| 190 |
], |
| 191 |
[ |
| 192 |
[ FormValidators::class, 'dateIsLater' ], |
| 193 |
[ '2020-01-02' ], |
| 194 |
'2020-01-01', |
| 195 |
false, |
| 196 |
], |
| 197 |
[ |
| 198 |
[ FormValidators::class, 'dateIsLater' ], |
| 199 |
[ '2020-01-02' ], |
| 200 |
'', |
| 201 |
false, |
| 202 |
], |
| 203 |
[ |
| 204 |
[ FormValidators::class, 'dateIsLater' ], |
| 205 |
[ '2020-01-02' ], |
| 206 |
null, |
| 207 |
false, |
| 208 |
], |
| 209 |
[ |
| 210 |
[ FormValidators::class, 'dateIsInMysqlFormat' ], |
| 211 |
[], |
| 212 |
'2020-01-01', |
| 213 |
true, |
| 214 |
], |
| 215 |
[ |
| 216 |
[ FormValidators::class, 'dateIsInMysqlFormat' ], |
| 217 |
[], |
| 218 |
'2020-12-31', |
| 219 |
true, |
| 220 |
], |
| 221 |
[ |
| 222 |
[ FormValidators::class, 'dateIsInMysqlFormat' ], |
| 223 |
[], |
| 224 |
'9999-01-01', |
| 225 |
true, |
| 226 |
], |
| 227 |
[ |
| 228 |
[ FormValidators::class, 'dateIsInMysqlFormat' ], |
| 229 |
[], |
| 230 |
'2020.01.01', |
| 231 |
false, |
| 232 |
], |
| 233 |
[ |
| 234 |
[ FormValidators::class, 'dateIsInMysqlFormat' ], |
| 235 |
[], |
| 236 |
'01.01.2020', |
| 237 |
false, |
| 238 |
], |
| 239 |
[ |
| 240 |
[ FormValidators::class, 'dateIsInMysqlFormat' ], |
| 241 |
[], |
| 242 |
'2020-22-01', |
| 243 |
false, |
| 244 |
], |
| 245 |
[ |
| 246 |
[ FormValidators::class, 'dateIsInMysqlFormat' ], |
| 247 |
[], |
| 248 |
'2020-11-33', |
| 249 |
false, |
| 250 |
], |
| 251 |
[ |
| 252 |
[ FormValidators::class, 'dateIsInMysqlFormat' ], |
| 253 |
[], |
| 254 |
'2020-11-11 00:00:00', |
| 255 |
false, |
| 256 |
], |
| 257 |
[ |
| 258 |
[ FormValidators::class, 'dateIsInMysqlFormat' ], |
| 259 |
[], |
| 260 |
'', |
| 261 |
false, |
| 262 |
], |
| 263 |
[ |
| 264 |
[ FormValidators::class, 'dateIsInMysqlFormat' ], |
| 265 |
[], |
| 266 |
null, |
| 267 |
false, |
| 268 |
], |
| 269 |
]; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* @param callable(TextInput): bool $validator |
| 274 |
* @param array<string|float|int|null> $validatorArgs |
| 275 |
* |
| 276 |
* @dataProvider validatorsDataProvider |
| 277 |
*/ |
| 278 |
public function testValidators( callable $validator, array $validatorArgs, string|float|int|null $inputValue, bool $expected ): void { |
| 279 |
$input = new TextInput(); |
| 280 |
$input->setValue( $inputValue ); |
| 281 |
|
| 282 |
$result = $validator( $input, ...$validatorArgs ); |
| 283 |
$this->assertSame( $expected, $result ); |
| 284 |
} |
| 285 |
} |
| 286 |
|