Skip to content

Commit 753ec38

Browse files
committed
add more tests code
1 parent 0ed6f45 commit 753ec38

File tree

5 files changed

+94
-22
lines changed

5 files changed

+94
-22
lines changed

src/Traits/ScopedValidatorsTrait.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,6 @@ public function neqFieldValidator($val, string $compareField): bool
437437
return $compareField && ($val !== $this->getByPath($compareField));
438438
}
439439

440-
// TODO: ltField, lteField
441-
442440
/**
443441
* 字段值比较:当前字段值 要小于 给定字段的值
444442
* @param string|int $val
@@ -447,9 +445,13 @@ public function neqFieldValidator($val, string $compareField): bool
447445
*/
448446
public function ltFieldValidator($val, string $compareField): bool
449447
{
450-
$minVal = $this->getByPath($compareField);
448+
$maxVal = $this->getByPath($compareField);
451449

452-
return Validators::gt($val, $minVal);
450+
if ($maxVal === null) {
451+
return false;
452+
}
453+
454+
return Validators::lt($val, $maxVal);
453455
}
454456

455457
/**
@@ -460,9 +462,13 @@ public function ltFieldValidator($val, string $compareField): bool
460462
*/
461463
public function lteFieldValidator($val, string $compareField): bool
462464
{
463-
$minVal = $this->getByPath($compareField);
465+
$maxVal = $this->getByPath($compareField);
464466

465-
return Validators::gte($val, $minVal);
467+
if ($maxVal === null) {
468+
return false;
469+
}
470+
471+
return Validators::lte($val, $maxVal);
466472
}
467473

468474
/**
@@ -475,6 +481,10 @@ public function gtFieldValidator($val, string $compareField): bool
475481
{
476482
$minVal = $this->getByPath($compareField);
477483

484+
if ($minVal === null) {
485+
return false;
486+
}
487+
478488
return Validators::gt($val, $minVal);
479489
}
480490

@@ -488,6 +498,10 @@ public function gteFieldValidator($val, string $compareField): bool
488498
{
489499
$minVal = $this->getByPath($compareField);
490500

501+
if ($minVal === null) {
502+
return false;
503+
}
504+
491505
return Validators::gte($val, $minVal);
492506
}
493507

src/Validator/Messages.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ public static function get(string $key)
121121
return self::$messages[$key] ?? '';
122122
}
123123

124+
/**
125+
* @param string $key
126+
* @param string|array $msg
127+
*/
128+
public static function set(string $key, $msg)
129+
{
130+
if ($key && $msg) {
131+
self::$messages[$key] = $msg;
132+
}
133+
}
134+
124135
/**
125136
* @param string $key
126137
* @return bool
@@ -138,21 +149,6 @@ public static function getDefault(): string
138149
return self::$messages['__default'];
139150
}
140151

141-
/*******************************************************************************
142-
* Error Messages
143-
******************************************************************************/
144-
145-
/**
146-
* @param string $key
147-
* @param string|array $msg
148-
*/
149-
public static function set(string $key, $msg)
150-
{
151-
if ($key && $msg) {
152-
self::$messages[$key] = $msg;
153-
}
154-
}
155-
156152
/**
157153
* @param array $messages
158154
*/

src/Validator/UserValidators.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ final class UserValidators
2626
*/
2727
public static function set(string $name, callable $callback)
2828
{
29-
self::$validators[$name] = $callback;
29+
if ($name = \trim($name)) {
30+
self::$validators[$name] = $callback;
31+
}
3032
}
3133

3234
/**

test/ScopedValidatorsTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2019-01-23
6+
* Time: 01:26
7+
*/
8+
9+
namespace Inhere\ValidateTest;
10+
11+
/**
12+
* Class ScopedValidatorsTest
13+
* @package Inhere\ValidateTest
14+
*/
15+
class ScopedValidatorsTest
16+
{
17+
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2019-01-23
6+
* Time: 01:27
7+
*/
8+
9+
namespace Inhere\ValidateTest\Validator;
10+
11+
use Inhere\Validate\Validator\UserValidators;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Class UserValidatorsTest
16+
* @package Inhere\ValidateTest\Validator
17+
*/
18+
class UserValidatorsTest extends TestCase
19+
{
20+
public function testBasic()
21+
{
22+
UserValidators::removeAll();
23+
UserValidators::setValidators([
24+
'name1' => function() {},
25+
'name2' => function() {},
26+
'' => function() {},
27+
]);
28+
29+
$this->assertCount(2, UserValidators::getValidators());
30+
$this->assertTrue(UserValidators::has('name1'));
31+
$this->assertFalse(UserValidators::has(''));
32+
33+
$this->assertNotEmpty(UserValidators::get('name2'));
34+
$this->assertEmpty(UserValidators::get('name3'));
35+
36+
UserValidators::remove('name1');
37+
$this->assertFalse(UserValidators::has('name1'));
38+
39+
UserValidators::removeAll();
40+
$this->assertCount(0, UserValidators::getValidators());
41+
}
42+
}

0 commit comments

Comments
 (0)