Skip to content

Commit 6439140

Browse files
committed
add more unit tests
1 parent 8921e41 commit 6439140

File tree

2 files changed

+106
-21
lines changed

2 files changed

+106
-21
lines changed

src/Validators.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ public static function ipv6($val): bool
681681

682682
/**
683683
* mac Address
684-
* @param string $input
684+
* @param string|mixed $input
685685
* @return bool
686686
*/
687687
public static function macAddress($input): bool
@@ -955,7 +955,7 @@ public static function in($val, $dict, $strict = false): bool
955955
}
956956

957957
/**
958-
* @param mixed $val
958+
* @param mixed $val
959959
* @param array|string $dict
960960
* @param bool $strict
961961
* @return bool
@@ -981,7 +981,9 @@ public static function notIn($val, $dict, $strict = false): bool
981981
*/
982982
public static function startWith($val, $start, $strict = true): bool
983983
{
984-
$start = (string)$start;
984+
if (($start = (string)$start) === '') {
985+
return false;
986+
}
985987

986988
if (\is_string($val)) {
987989
return ($strict ? \strpos($val, $start) : \stripos($val, $start)) === 0;
@@ -1009,10 +1011,10 @@ public static function endWith($val, $end, $strict = true): bool
10091011

10101012
if (\is_string($val)) {
10111013
$last = \substr($val, -Helper::strlen($end));
1012-
}
1013-
1014-
if (\is_array($val)) {
1014+
} elseif (\is_array($val)) {
10151015
$last = \array_pop($val);
1016+
} else {
1017+
return false;
10161018
}
10171019

10181020
return $strict ? $last === $end : (string)$last === $end;
@@ -1045,11 +1047,7 @@ public static function dateEquals($val, $date): bool
10451047
return false;
10461048
}
10471049

1048-
if (!$date) {
1049-
return false;
1050-
}
1051-
1052-
return $time === \strtotime($date);
1050+
return $date ? $time === \strtotime((string)$date) : false;
10531051
}
10541052

10551053
/**
@@ -1087,11 +1085,11 @@ public static function beforeDate($val, $beforeDate = '', $symbol = '<'): bool
10871085

10881086
$beforeTime = $beforeDate ? \strtotime($beforeDate) : \time();
10891087

1090-
if ($symbol === '>') {
1091-
return $beforeTime < $valueTime;
1088+
if ($symbol === '<') {
1089+
return $valueTime < $beforeTime;
10921090
}
10931091

1094-
return $beforeTime <= $valueTime;
1092+
return $valueTime <= $beforeTime;
10951093
}
10961094

10971095
/**
@@ -1230,16 +1228,24 @@ public static function negativePrice($price): bool
12301228

12311229
/**
12321230
* Check for a float number validity
1233-
* @param float $float Float number to validate
1231+
* @param float|mixed $float Float number to validate
12341232
* @return bool Validity is ok or not
12351233
*/
12361234
public static function isFloat($float): bool
12371235
{
1236+
if (!\is_scalar($float)) {
1237+
return false;
1238+
}
1239+
12381240
return (string)((float)$float) === (string)$float;
12391241
}
12401242

12411243
public static function isUnsignedFloat($float): bool
12421244
{
1245+
if (!\is_scalar($float)) {
1246+
return false;
1247+
}
1248+
12431249
return (string)((float)$float) === (string)$float && $float >= 0;
12441250
}
12451251

test/ValidatorsTest.php

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ public function testIntList()
344344
$this->assertFalse(Validators::intList(['a' => 'v']));
345345
$this->assertFalse(Validators::intList(['value', 'a' => 'v']));
346346
$this->assertFalse(Validators::intList([2 => '343', 45]));
347+
$this->assertFalse(Validators::intList([45, 2 => '343']));
347348

348349
$this->assertTrue(Validators::intList(['343', 45]));
349350
$this->assertTrue(Validators::intList([565, 3234, -56]));
@@ -416,6 +417,22 @@ public function testDistinct()
416417
$this->assertTrue(Validators::distinct(['a', 'b', 'c']));
417418
}
418419

420+
public function testInANDNotIn()
421+
{
422+
$samples = [
423+
[true, 1, [1, 2, 3], false],
424+
[true, 1, [1, 2, 3], true],
425+
[true, '1', [1, 2, 3], false],
426+
[false, '1', [1, 2, 3], true],
427+
[true, '1', '1,2,3', true],
428+
];
429+
430+
foreach ($samples as list($want, $val, $dict, $strict)) {
431+
$this->assertSame($want, Validators::in($val, $dict, $strict));
432+
$this->assertSame(!$want, Validators::notIn($val, $dict, $strict));
433+
}
434+
}
435+
419436
public function testJson()
420437
{
421438
$this->assertFalse(Validators::json('test'));
@@ -446,7 +463,9 @@ public function testContains()
446463

447464
public function testStartWith()
448465
{
466+
$this->assertFalse(Validators::startWith(null, 'ell'));
449467
$this->assertFalse(Validators::startWith('hello, world', 'ell'));
468+
$this->assertFalse(Validators::startWith('hello, world', ''));
450469

451470
$this->assertTrue(Validators::startWith('hello, world', 'hell'));
452471
$this->assertTrue(Validators::startWith(['hello', 'world'], 'hello'));
@@ -460,21 +479,32 @@ public function testEndWith()
460479
$this->assertTrue(Validators::endWith(['hello', 'world'], 'world'));
461480
}
462481

463-
public function testDate()
482+
public function testDateCheck()
464483
{
484+
// date
465485
$this->assertFalse(Validators::date('hello'));
466-
467486
$this->assertTrue(Validators::date(170526));
468487
$this->assertTrue(Validators::date('20170526'));
469-
}
470488

471-
public function testDateCheck()
472-
{
489+
// dateEquals
490+
$this->assertTrue(Validators::dateEquals('20170526', '20170526'));
491+
$this->assertTrue(Validators::dateEquals('2017-05-26', '20170526'));
492+
$this->assertFalse(Validators::dateEquals('20170525', '20170526'));
493+
473494
// dateFormat
474495
$this->assertFalse(Validators::dateFormat('hello'));
475496
$this->assertFalse(Validators::dateFormat('170526', 'ymd'));
476497
$this->assertTrue(Validators::dateFormat('20170526', 'Ymd'));
477498

499+
// beforeDate
500+
$this->assertTrue(Validators::beforeDate('20170524', '20170526'));
501+
$this->assertFalse(Validators::beforeDate('20170526', '20170526'));
502+
503+
// beforeOrEqualDate
504+
$this->assertTrue(Validators::beforeOrEqualDate('20170524', '20170526'));
505+
$this->assertTrue(Validators::beforeOrEqualDate('20170526', '20170526'));
506+
$this->assertFalse(Validators::beforeOrEqualDate('20170527', '20170526'));
507+
478508
// afterDate
479509
$this->assertTrue(Validators::afterDate('20170526', '20170524'));
480510
$this->assertFalse(Validators::afterDate('20170526', '20170526'));
@@ -485,6 +515,13 @@ public function testDateCheck()
485515
$this->assertTrue(Validators::afterOrEqualDate('20170526', '20170526'));
486516
$this->assertTrue(Validators::afterOrEqualDate('20170526', '20170524'));
487517
$this->assertFalse(Validators::afterOrEqualDate('20170524', '20170526'));
518+
519+
// isDate
520+
$this->assertTrue(Validators::isDate('2017-05-26'));
521+
$this->assertFalse(Validators::isDate('20170526'));
522+
// isDateFormat
523+
$this->assertTrue(Validators::isDateFormat('2017-05-26'));
524+
$this->assertFalse(Validators::isDateFormat('20170526'));
488525
}
489526

490527
public function testPhone()
@@ -502,6 +539,48 @@ public function testPostCode()
502539
public function testPrice()
503540
{
504541
$this->assertTrue(Validators::price('610.45'));
505-
$this->assertFalse(Validators::price('-20170526'));
542+
$this->assertFalse(Validators::price('-201.26'));
543+
$this->assertFalse(Validators::price('abc'));
544+
545+
$this->assertTrue(Validators::negativePrice('610.45'));
546+
$this->assertTrue(Validators::negativePrice('-201.26'));
547+
$this->assertFalse(Validators::negativePrice('abc'));
548+
}
549+
550+
public function testOther()
551+
{
552+
// isFloat
553+
$this->assertFalse(Validators::isFloat([]));
554+
$this->assertFalse(Validators::isFloat('abc'));
555+
$this->assertTrue(Validators::isFloat('23.34'));
556+
$this->assertTrue(Validators::isFloat('-23.34'));
557+
558+
// isUnsignedFloat
559+
$this->assertTrue(Validators::isUnsignedFloat('23.34'));
560+
$this->assertFalse(Validators::isUnsignedFloat('-23.34'));
561+
562+
// isInt
563+
$this->assertTrue(Validators::isInt('23'));
564+
$this->assertTrue(Validators::isInt('-23'));
565+
$this->assertFalse(Validators::isInt('-23.34'));
566+
567+
// isUnsignedInt
568+
$this->assertTrue(Validators::isUnsignedInt('23'));
569+
$this->assertFalse(Validators::isUnsignedInt('-23'));
570+
$this->assertFalse(Validators::isUnsignedInt('-23.34'));
571+
572+
// macAddress
573+
$this->assertTrue(Validators::macAddress('01:23:45:67:89:ab'));
574+
$this->assertFalse(Validators::macAddress([]));
575+
$this->assertFalse(Validators::macAddress(null));
576+
$this->assertFalse(Validators::macAddress('123 abc'));
577+
578+
// md5
579+
$this->assertFalse(Validators::md5('123 abc'));
580+
$this->assertFalse(Validators::md5(true));
581+
582+
// sha1
583+
$this->assertFalse(Validators::sha1(true));
584+
$this->assertFalse(Validators::sha1('123 abc'));
506585
}
507586
}

0 commit comments

Comments
 (0)