1 | <?php |
||||
2 | /** |
||||
3 | * test Validator |
||||
4 | * |
||||
5 | * @package BlueData |
||||
6 | * @subpackage Test |
||||
7 | * @author MichaĆ Adamiak <[email protected]> |
||||
8 | * @copyright bluetree-service |
||||
9 | */ |
||||
10 | namespace Test; |
||||
11 | |||||
12 | use BlueData\Check\Validator; |
||||
13 | use PHPUnit\Framework\TestCase; |
||||
14 | |||||
15 | class ValidatorTest extends TestCase |
||||
16 | { |
||||
17 | public function testRegularExpressions() |
||||
18 | { |
||||
19 | $this->functionTest( |
||||
20 | function ($expression, $type) { |
||||
21 | return Validator::valid($expression, $type); |
||||
22 | } |
||||
23 | ); |
||||
24 | |||||
25 | $this->assertNull(Validator::valid('some_expression', 'none_existing_type')); |
||||
26 | } |
||||
27 | |||||
28 | public function testMail() |
||||
29 | { |
||||
30 | $this->functionTest( |
||||
31 | function ($expression) { |
||||
32 | return Validator::mail($expression); |
||||
33 | }, |
||||
34 | 'mail' |
||||
35 | ); |
||||
36 | } |
||||
37 | |||||
38 | public function testPrice() |
||||
39 | { |
||||
40 | $this->functionTest( |
||||
41 | function ($expression) { |
||||
42 | return Validator::price($expression); |
||||
43 | }, |
||||
44 | 'price' |
||||
45 | ); |
||||
46 | } |
||||
47 | |||||
48 | public function testPostcode() |
||||
49 | { |
||||
50 | $this->functionTest( |
||||
51 | function ($expression) { |
||||
52 | return Validator::postcode($expression); |
||||
53 | }, |
||||
54 | 'postcode' |
||||
55 | ); |
||||
56 | } |
||||
57 | |||||
58 | public function testNip() |
||||
59 | { |
||||
60 | $values = [[ |
||||
61 | 'correct' => [ |
||||
62 | 4878280798, |
||||
63 | '998-993-11-84', |
||||
64 | '998 741 29 94', |
||||
65 | ], |
||||
66 | 'incorrect' => [ |
||||
67 | 4874280798, |
||||
68 | 48782807981, |
||||
69 | 487828079, |
||||
70 | '998-193-11-84', |
||||
71 | '998/993/11/84', |
||||
72 | '998 341 29 94', |
||||
73 | ], |
||||
74 | ]]; |
||||
75 | |||||
76 | $this->check( |
||||
77 | function ($expression) { |
||||
78 | return Validator::nip($expression); |
||||
79 | }, |
||||
80 | $values |
||||
81 | ); |
||||
82 | } |
||||
83 | |||||
84 | public function testRange() |
||||
85 | { |
||||
86 | $this->assertTrue(Validator::range(-1, null, 23)); |
||||
87 | $this->assertTrue(Validator::range(15, 3, 23)); |
||||
88 | $this->assertTrue(Validator::range(23423, 3)); |
||||
89 | $this->assertTrue(Validator::range(0xd3a743f2ab, 0xa0)); |
||||
90 | $this->assertTrue(Validator::range('#aaffff', '#00ffff')); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() '#aaffff' of type string is incompatible with the type integer expected by parameter $value of BlueData\Check\Validator::range() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
91 | $this->assertTrue(Validator::range('#aaffff', '#00ffff', '#bbffff')); |
||||
0 ignored issues
–
show
'#bbffff' of type string is incompatible with the type integer expected by parameter $max of BlueData\Check\Validator::range() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
92 | $this->assertTrue(Validator::range(0555, 0333, 0777)); |
||||
93 | $this->assertTrue(Validator::range(0b1110, 0b0000, 0b1111)); |
||||
94 | |||||
95 | $this->assertFalse(Validator::range(23423, null, 23)); |
||||
96 | $this->assertFalse(Validator::range(2, 3, 23)); |
||||
97 | $this->assertFalse(Validator::range(2, 3)); |
||||
98 | $this->assertFalse(Validator::range(0xd3a743f2ab, 0xffffffffff)); |
||||
99 | $this->assertFalse(Validator::range('#aaffff', '#ffffff')); |
||||
100 | } |
||||
101 | |||||
102 | public function testStringLength() |
||||
103 | { |
||||
104 | $this->assertTrue(Validator::stringLength('asdasdasdĆĆŒÄĆ', null, 23)); |
||||
105 | $this->assertTrue(Validator::stringLength('asdasdasdĆĆŒÄĆ', 3, 23)); |
||||
106 | $this->assertTrue(Validator::stringLength('asdasdasdĆĆŒÄĆ', 3)); |
||||
107 | |||||
108 | $this->assertFalse(Validator::stringLength('asdasdasdĆĆŒÄĆ', null, 5)); |
||||
109 | $this->assertFalse(Validator::stringLength('ÄĆ', 3, 23)); |
||||
110 | $this->assertFalse(Validator::stringLength('ÄĆ', 3)); |
||||
111 | } |
||||
112 | |||||
113 | public function testUnderZero() |
||||
114 | { |
||||
115 | $this->assertTrue(Validator::underZero(-1)); |
||||
116 | $this->assertFalse(Validator::underZero(2)); |
||||
117 | } |
||||
118 | |||||
119 | public function testPesel() |
||||
120 | { |
||||
121 | $this->assertTrue(Validator::pesel(97082402112)); |
||||
122 | $this->assertFalse(Validator::pesel(93789452112)); |
||||
123 | $this->assertFalse(Validator::pesel(9378945211278)); |
||||
124 | } |
||||
125 | |||||
126 | public function testRegon() |
||||
127 | { |
||||
128 | $this->assertTrue(Validator::regon(451239418)); |
||||
129 | $this->assertTrue(Validator::regon('451-239-418')); |
||||
130 | $this->assertTrue(Validator::regon(81420296650613)); |
||||
131 | |||||
132 | $this->assertFalse(Validator::regon(4512394181)); |
||||
133 | $this->assertFalse(Validator::regon(451239419)); |
||||
134 | $this->assertFalse(Validator::regon(45123941)); |
||||
135 | $this->assertFalse(Validator::regon(814202966506136)); |
||||
136 | $this->assertFalse(Validator::regon(8142029665061)); |
||||
137 | } |
||||
138 | |||||
139 | public function testNrb() |
||||
140 | { |
||||
141 | $this->assertTrue(Validator::nrb('42249000057255503346698354')); |
||||
142 | $this->assertTrue(Validator::nrb('42 2490 0005 7255 5033 4669 8354')); |
||||
143 | $this->assertTrue(Validator::nrb('42-2490-0005-7255-5033-4669-8354')); |
||||
144 | |||||
145 | $this->assertFalse(Validator::nrb(42249000057255503346698354)); |
||||
146 | $this->assertFalse(Validator::nrb(4224900005725550334669835)); |
||||
147 | $this->assertFalse(Validator::nrb(422490000572555033466983544)); |
||||
148 | $this->assertFalse(Validator::nrb('42549000057255503346693354')); |
||||
149 | } |
||||
150 | |||||
151 | public function testIban() |
||||
152 | { |
||||
153 | $this->assertTrue(Validator::iban('PL67249076002100555998070117')); |
||||
154 | $this->assertTrue(Validator::iban('PL66249022982522')); |
||||
155 | $this->assertTrue(Validator::iban('PL102490405804037964617707221535')); |
||||
156 | $this->assertTrue(Validator::iban('102490405804037964617707221535')); |
||||
157 | $this->assertTrue(Validator::iban('1024-9040-5804-0379-6461-7707-2215-35')); |
||||
158 | |||||
159 | $this->assertFalse(Validator::iban('PB67249076002100555998070117')); |
||||
160 | $this->assertFalse(Validator::iban('PL66249022982527')); |
||||
161 | $this->assertFalse(Validator::iban('102490405804037964617707221539')); |
||||
162 | } |
||||
163 | |||||
164 | public function testUrl() |
||||
165 | { |
||||
166 | $urlExtended = $this->regularExpressionsProvider()['url_extend']; |
||||
167 | $urlFull = $this->regularExpressionsProvider()['url_full']; |
||||
168 | $url = $this->regularExpressionsProvider()['url']; |
||||
169 | |||||
170 | $this->check( |
||||
171 | function ($expression) { |
||||
172 | return Validator::url($expression, 1); |
||||
173 | }, |
||||
174 | ['url_extend' => $urlExtended] |
||||
175 | ); |
||||
176 | |||||
177 | $this->check( |
||||
178 | function ($expression) { |
||||
179 | return Validator::url($expression, 2); |
||||
180 | }, |
||||
181 | ['url_full' => $urlFull] |
||||
182 | ); |
||||
183 | |||||
184 | $this->check( |
||||
185 | function ($expression) { |
||||
186 | return Validator::url($expression); |
||||
187 | }, |
||||
188 | ['url' => $url] |
||||
189 | ); |
||||
190 | } |
||||
191 | |||||
192 | public function testPhone() |
||||
193 | { |
||||
194 | $this->functionTest( |
||||
195 | function ($expression) { |
||||
196 | return Validator::phone($expression); |
||||
197 | }, |
||||
198 | 'phone' |
||||
199 | ); |
||||
200 | } |
||||
201 | |||||
202 | public function testStep() |
||||
203 | { |
||||
204 | $this->assertTrue(Validator::step(15, 5, 5)); |
||||
205 | $this->assertFalse(Validator::step(12, 5)); |
||||
206 | $this->assertFalse(Validator::step(12, 'a')); |
||||
0 ignored issues
–
show
'a' of type string is incompatible with the type integer|double expected by parameter $step of BlueData\Check\Validator::step() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
207 | } |
||||
208 | |||||
209 | /** |
||||
210 | * @param \Closure $validFunction |
||||
211 | * @param bool|string $type |
||||
212 | */ |
||||
213 | protected function functionTest(\Closure $validFunction, $type = false) |
||||
214 | { |
||||
215 | $testData = []; |
||||
216 | |||||
217 | if ($type) { |
||||
218 | $testData[$type] = $this->regularExpressionsProvider()[$type]; |
||||
219 | } else { |
||||
220 | $testData = $this->regularExpressionsProvider(); |
||||
221 | } |
||||
222 | |||||
223 | $this->check($validFunction, $testData); |
||||
224 | } |
||||
225 | |||||
226 | /** |
||||
227 | * @param \Closure $validFunction |
||||
228 | * @param array $testData |
||||
229 | */ |
||||
230 | protected function check(\Closure $validFunction, array $testData) |
||||
231 | { |
||||
232 | foreach ($testData as $type => $data) { |
||||
233 | foreach ($data['correct'] as $correctExpression) { |
||||
234 | $this->assertTrue($validFunction($correctExpression, $type), "$type: $correctExpression"); |
||||
235 | } |
||||
236 | |||||
237 | foreach ($data['incorrect'] as $incorrectExpression) { |
||||
238 | $this->assertFalse($validFunction($incorrectExpression, $type), "$type: $incorrectExpression"); |
||||
239 | } |
||||
240 | } |
||||
241 | } |
||||
242 | |||||
243 | protected function regularExpressionsProvider() |
||||
244 | { |
||||
245 | $testData = [ |
||||
246 | [ |
||||
247 | 'correct' => [ |
||||
248 | 'asd fgh Ä ĆÄĆș', |
||||
249 | ], |
||||
250 | 'incorrect' => [ |
||||
251 | 'asd fgh Ä ĆÄĆș 23', |
||||
252 | ] |
||||
253 | ], |
||||
254 | [ |
||||
255 | 'correct' => [ |
||||
256 | 'asd fgh Ä ĆÄĆș _,.-', |
||||
257 | ], |
||||
258 | 'incorrect' => [ |
||||
259 | 'asd fgh Ä ĆÄĆș 23_,.', |
||||
260 | ] |
||||
261 | ], |
||||
262 | [ |
||||
263 | 'correct' => [ |
||||
264 | 'asd fghfg Ä ĆŒÄ._,;:-', |
||||
265 | ], |
||||
266 | 'incorrect' => [ |
||||
267 | 'asd fghfg Ä ĆŒÄ 23 ._,;:-', |
||||
268 | ] |
||||
269 | ], |
||||
270 | [ |
||||
271 | 'correct' => [ |
||||
272 | 'sdg sar Ä ĆÄĆș 3242 _ ,\\.;:/!@#$%^&*()+=|{}][<>?`~\'"-', |
||||
273 | ], |
||||
274 | 'incorrect' => [ |
||||
275 | 'sdg sar Ä ĆÄĆș 3242 _ ,\\.;:/!@#$%^&*()+=|{}][<>?`~\'"-' . "\n\t", |
||||
276 | ] |
||||
277 | ], |
||||
278 | [ |
||||
279 | 'correct' => [ |
||||
280 | 23525 |
||||
281 | ], |
||||
282 | 'incorrect' => [ |
||||
283 | '3234a', |
||||
284 | ] |
||||
285 | ], |
||||
286 | [ |
||||
287 | 'correct' => [ |
||||
288 | '234-2423/94', |
||||
289 | ], |
||||
290 | 'incorrect' => [ |
||||
291 | '4-3234a', |
||||
292 | ] |
||||
293 | ], |
||||
294 | [ |
||||
295 | 'correct' => [ |
||||
296 | 'asdas .,_- 23423', |
||||
297 | ], |
||||
298 | 'incorrect' => [ |
||||
299 | '4-asdas .,_- 23423 @', |
||||
300 | ] |
||||
301 | ], |
||||
302 | [ |
||||
303 | 'correct' => [ |
||||
304 | 'asdas .,_- 23423:;', |
||||
305 | ], |
||||
306 | 'incorrect' => [ |
||||
307 | '4-asdas .,_- 234:;23 @', |
||||
308 | ] |
||||
309 | ], |
||||
310 | [ |
||||
311 | 'correct' => [ |
||||
312 | -234234, |
||||
313 | 234234, |
||||
314 | ], |
||||
315 | 'incorrect' => [ |
||||
316 | '4-3234a', |
||||
317 | ] |
||||
318 | ], |
||||
319 | [ |
||||
320 | 'correct' => [ |
||||
321 | -2342.2342, |
||||
322 | 2342.2342, |
||||
323 | '2342,2342', |
||||
324 | '-2342,2342', |
||||
325 | ], |
||||
326 | 'incorrect' => [ |
||||
327 | 234324, |
||||
328 | '2343.24adsf', |
||||
329 | -234324, |
||||
330 | ] |
||||
331 | ], |
||||
332 | [ |
||||
333 | 'correct' => [ |
||||
334 | -2342.2342, |
||||
335 | 2342.2342, |
||||
336 | '2342,2342', |
||||
337 | '-2342,2342', |
||||
338 | 234324, |
||||
339 | -234324, |
||||
340 | ], |
||||
341 | 'incorrect' => [ |
||||
342 | '2343.24adsf', |
||||
343 | ] |
||||
344 | ], |
||||
345 | [ |
||||
346 | 'correct' => [ |
||||
347 | '[email protected]', |
||||
348 | '12te-st@ma_il.com', |
||||
349 | ], |
||||
350 | 'incorrect' => [ |
||||
351 | 'testmail.com', |
||||
352 | 'test@mail', |
||||
353 | '[email protected]', |
||||
354 | ] |
||||
355 | ], |
||||
356 | [ |
||||
357 | 'correct' => [ |
||||
358 | 'http://someweb.com', |
||||
359 | 'http://someweb.com/', |
||||
360 | 'http://some-web_.com', |
||||
361 | 'someweb.com', |
||||
362 | ], |
||||
363 | 'incorrect' => [ |
||||
364 | 'http://someweb.com/subpage', |
||||
365 | 'gesfgsdfg', |
||||
366 | 'https://someweb.com', |
||||
367 | 'http://someweb.com/some-subpage?param1=asdas%d¶m2=asdas+-', |
||||
368 | ] |
||||
369 | ], |
||||
370 | [ |
||||
371 | 'correct' => [ |
||||
372 | 'http://someweb.com', |
||||
373 | 'https://someweb.com', |
||||
374 | 'ftp://someweb.com', |
||||
375 | 'ftps://someweb.com', |
||||
376 | 'http://someweb.com/', |
||||
377 | 'http://some-web_.com', |
||||
378 | 'someweb.com', |
||||
379 | ], |
||||
380 | 'incorrect' => [ |
||||
381 | 'http://someweb.com/subpage', |
||||
382 | 'gesfgsdfg', |
||||
383 | 'http://someweb.com/some-subpage?param1=asdas%d¶m2=asdas+-', |
||||
384 | ] |
||||
385 | ], |
||||
386 | [ |
||||
387 | 'correct' => [ |
||||
388 | 'http://someweb.com', |
||||
389 | 'http://someweb.com/some-subpage', |
||||
390 | 'http://someweb.com/some-subpage?param1=asdas%d¶m2=asdas+-', |
||||
391 | 'https://someweb.com', |
||||
392 | 'ftp://someweb.com', |
||||
393 | 'ftps://someweb.com', |
||||
394 | 'http://someweb.com/', |
||||
395 | 'http://some-web_.com', |
||||
396 | 'someweb.com', |
||||
397 | ], |
||||
398 | 'incorrect' => [ |
||||
399 | 'gesfgsdfg', |
||||
400 | 'http://someweb.com/some-subpage?param1=\asda*', |
||||
401 | ] |
||||
402 | ], |
||||
403 | [ |
||||
404 | 'correct' => [ |
||||
405 | 123123, |
||||
406 | 1231.23, |
||||
407 | '234234,23', |
||||
408 | ], |
||||
409 | 'incorrect' => [ |
||||
410 | '2342f', |
||||
411 | 1231.2334, |
||||
412 | '234234,23324', |
||||
413 | ] |
||||
414 | ], |
||||
415 | [ |
||||
416 | 'correct' => [ |
||||
417 | '42-400', |
||||
418 | ], |
||||
419 | 'incorrect' => [ |
||||
420 | '42400', |
||||
421 | '42-00', |
||||
422 | '422-00', |
||||
423 | ] |
||||
424 | ], |
||||
425 | [ |
||||
426 | 'correct' => [ |
||||
427 | '+48 600 700 800', |
||||
428 | '600-700-000', |
||||
429 | '+48600700800', |
||||
430 | '+48 ( 052 ) 131 231-2312', |
||||
431 | ], |
||||
432 | 'incorrect' => [ |
||||
433 | '34234234d', |
||||
434 | '23423/234234/234234', |
||||
435 | ] |
||||
436 | ], |
||||
437 | [ |
||||
438 | 'correct' => [ |
||||
439 | '12-12-1983', |
||||
440 | ], |
||||
441 | 'incorrect' => [ |
||||
442 | '12/12/1983', |
||||
443 | '2-12-1983', |
||||
444 | '12-2-1983', |
||||
445 | '12-12-83', |
||||
446 | '1983-12-12', |
||||
447 | ] |
||||
448 | ], |
||||
449 | [ |
||||
450 | 'correct' => [ |
||||
451 | '1983-12-12', |
||||
452 | ], |
||||
453 | 'incorrect' => [ |
||||
454 | '1983/12/12', |
||||
455 | '1983-2-12', |
||||
456 | '1983-12-2', |
||||
457 | '12-12-83', |
||||
458 | '12-12-1983', |
||||
459 | ] |
||||
460 | ], |
||||
461 | [ |
||||
462 | 'correct' => [ |
||||
463 | '1983-12', |
||||
464 | ], |
||||
465 | 'incorrect' => [ |
||||
466 | '1983-1', |
||||
467 | '83-12', |
||||
468 | ] |
||||
469 | ], |
||||
470 | [ |
||||
471 | 'correct' => [ |
||||
472 | '2017-06-27 16:42', |
||||
473 | ], |
||||
474 | 'incorrect' => [ |
||||
475 | '17-06-27 16:42', |
||||
476 | '2017-6-27 16:42', |
||||
477 | '2017-06-27', |
||||
478 | '2017-06-27 16:2', |
||||
479 | '2017-06-27 16:42:34', |
||||
480 | ] |
||||
481 | ], |
||||
482 | [ |
||||
483 | 'correct' => [ |
||||
484 | '12/12/1983', |
||||
485 | ], |
||||
486 | 'incorrect' => [ |
||||
487 | '12-12-1983', |
||||
488 | '1983-12-12', |
||||
489 | '12/1/1983', |
||||
490 | '1/12/1983', |
||||
491 | '12/12/83', |
||||
492 | ] |
||||
493 | ], |
||||
494 | [ |
||||
495 | 'correct' => [ |
||||
496 | '12/12/1983 07:23', |
||||
497 | ], |
||||
498 | 'incorrect' => [ |
||||
499 | '12/12/1983 7:23', |
||||
500 | '12/12/1983', |
||||
501 | '12-12-1983', |
||||
502 | '1983-12-12', |
||||
503 | '12/1/1983', |
||||
504 | '1/12/1983', |
||||
505 | '12/12/83', |
||||
506 | ] |
||||
507 | ], |
||||
508 | [ |
||||
509 | 'correct' => [ |
||||
510 | '16:45', |
||||
511 | '16:45:48', |
||||
512 | ], |
||||
513 | 'incorrect' => [ |
||||
514 | '6:45', |
||||
515 | '6:45:48', |
||||
516 | '16:45:8', |
||||
517 | ] |
||||
518 | ], |
||||
519 | [ |
||||
520 | 'correct' => [ |
||||
521 | '#ff0000', |
||||
522 | ], |
||||
523 | 'incorrect' => [ |
||||
524 | 'ff0000', |
||||
525 | '#fff0000', |
||||
526 | '#hf0000', |
||||
527 | ] |
||||
528 | ], |
||||
529 | [ |
||||
530 | 'correct' => [ |
||||
531 | '#fff0000235', |
||||
532 | '#0000', |
||||
533 | ], |
||||
534 | 'incorrect' => [ |
||||
535 | '#fffz0000', |
||||
536 | ] |
||||
537 | ], |
||||
538 | [ |
||||
539 | 'correct' => [ |
||||
540 | '0x235ad', |
||||
541 | ], |
||||
542 | 'incorrect' => [ |
||||
543 | 0x235ad, |
||||
544 | 32345, |
||||
545 | '0235ad', |
||||
546 | '0x235adz', |
||||
547 | 'x235ad', |
||||
548 | ] |
||||
549 | ], |
||||
550 | [ |
||||
551 | 'correct' => [ |
||||
552 | '0755', |
||||
553 | '075512431254213', |
||||
554 | ], |
||||
555 | 'incorrect' => [ |
||||
556 | 755, |
||||
557 | 0755, |
||||
558 | '0755a', |
||||
559 | ] |
||||
560 | ], |
||||
561 | [ |
||||
562 | 'correct' => [ |
||||
563 | 'b01010101110110', |
||||
564 | ], |
||||
565 | 'incorrect' => [ |
||||
566 | '010101011101102', |
||||
567 | 01010101110110, |
||||
568 | ] |
||||
569 | ], |
||||
570 | ]; |
||||
571 | |||||
572 | return array_combine( |
||||
573 | array_keys(Validator::$regularExpressions), |
||||
574 | $testData |
||||
575 | ); |
||||
576 | } |
||||
577 | } |
||||
578 |