Skip to content

Commit 72a6832

Browse files
committed
add more unit for filter
1 parent 8065b9c commit 72a6832

File tree

6 files changed

+87
-13
lines changed

6 files changed

+87
-13
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,13 +739,10 @@ public function addValidator(string $name, \Closure $callback, string $msg = '')
739739
```
740740
// 验证失败
741741
public function isFail()
742-
public function fail() // isFail() 的别名方法
743-
public function failed() // isFail() 的别名方法
744742
public function hasError() // isFail() 的别名方法
745743
746744
// 成功通过验证
747-
public function ok()
748-
public function isOk()
745+
public function isOk()
749746
public function isPassed()
750747
```
751748

src/Filter/FilteringTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Inhere\Validate\Helper;
1212
use InvalidArgumentException;
1313
use function function_exists;
14+
use function is_array;
1415
use function is_object;
1516
use function is_string;
1617
use function method_exists;
@@ -51,6 +52,11 @@ protected function valueFiltering($value, $filters)
5152
{
5253
$filters = is_string($filters) ? Filters::explode($filters, '|') : $filters;
5354

55+
// fix: must ensure is array
56+
if (!is_array($filters)) {
57+
$filters = [$filters];
58+
}
59+
5460
foreach ($filters as $key => $filter) {
5561
// key is a filter. ['myFilter' => ['arg1', 'arg2']]
5662
if (is_string($key)) {

src/Filter/Filtration.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ public function filtering(array $rules = []): array
7979
}
8080

8181
/**
82-
* 对数据应用给的一系列过滤规则
82+
* Apply a series of filtering rules to the input data
8383
*
8484
* @param array $rules
8585
* @param array $data
8686
*
87-
* @return array 返回过滤后的数据
87+
* @return array Return filtered data
8888
* @throws InvalidArgumentException
8989
*/
9090
public function applyRules(array $rules = [], array $data = []): array
@@ -106,8 +106,8 @@ public function applyRules(array $rules = [], array $data = []): array
106106
$fields = is_string($fields) ? Filters::explode($fields) : (array)$fields;
107107

108108
foreach ($fields as $field) {
109-
if (!isset($data[$field])) {
110-
$filtered[$field] = $rule['default'] ?? null;
109+
if (!isset($data[$field]) && isset($rule['default'])) {
110+
$filtered[$field] = $rule['default'];
111111
} else {
112112
$filtered[$field] = $this->valueFiltering($data[$field], $rule[1]);
113113
}
@@ -118,7 +118,7 @@ public function applyRules(array $rules = [], array $data = []): array
118118
}
119119

120120
/**
121-
* value sanitize 直接对给的值进行过滤
121+
* value sanitize Filter the value directly
122122
*
123123
* @param mixed $value
124124
* @param string|array $filters

src/ValidationTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ protected function applyRule($fields, array $rule, array $onlyChecked): void
281281
$this->data[$field] = $value;
282282
}
283283

284-
// Field value validate
284+
// Field name validate
285285
if (is_string($validator)) {
286286
if ($validator === 'safe') {
287287
$this->setSafe($field, $value);

test/Filter/FiltrationTest.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
namespace Inhere\ValidateTest\Filter;
1010

1111
use Inhere\Validate\Filter\Filtration;
12+
use InvalidArgumentException;
1213
use PHPUnit\Framework\TestCase;
14+
use function trim;
1315

1416
/**
1517
* Class FiltrationTest
@@ -48,21 +50,28 @@ public function testUserFilters(): void
4850
$fl->addFilters([
4951
'name1' => function () {
5052
},
51-
'name2' => function () {
53+
'newTrim' => function ($val) {
54+
return trim($val);
5255
},
5356
'' => function () {
5457
},
5558
]);
5659

5760
$this->assertCount(2, $fl->getFilters());
5861

59-
$this->assertNotEmpty($fl->getFilter('name2'));
62+
$this->assertNotEmpty($fl->getFilter('newTrim'));
6063
$this->assertEmpty($fl->getFilter('name3'));
6164

6265
$fl->addFilter('new1', function () {
6366
});
6467
$this->assertNotEmpty($fl->getFilter('new1'));
6568

69+
// use user filter
70+
$filtered = $fl->filtering([
71+
['name', 'newTrim']
72+
]);
73+
$this->assertSame('tom', $filtered['name']);
74+
6675
$fl->delFilter('name1');
6776
$this->assertEmpty($fl->getFilter('name1'));
6877

@@ -72,7 +81,6 @@ public function testUserFilters(): void
7281

7382
public function testFiltering(): void
7483
{
75-
7684
$rules = [
7785
['name', 'string|trim'],
7886
['status', 'trim|int'],
@@ -104,4 +112,29 @@ public function testFiltering(): void
104112
$this->assertEmpty($fl->getData());
105113
$this->assertEmpty($fl->getRules());
106114
}
115+
116+
public function testUseClosure(): void
117+
{
118+
$fl = Filtration::make($this->data);
119+
$fl->setRules([
120+
['name', function($val) {
121+
$this->assertSame(' tom ', $val);
122+
return trim($val);
123+
}]
124+
]);
125+
126+
$cleaned = $fl->filtering();
127+
$this->assertSame('tom', $cleaned['name']);
128+
}
129+
130+
public function testCallNotExist(): void
131+
{
132+
$fl = Filtration::make($this->data);
133+
$fl->setRules([
134+
['name', 'not-exist-filter']
135+
]);
136+
137+
$this->expectException(InvalidArgumentException::class);
138+
$fl->filtering();
139+
}
107140
}

test/RuleValidationTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,4 +604,42 @@ public function testValidatorAlias(): void
604604
$this->assertEquals('arr val must be an array of nature', $v->firstError());
605605
$this->assertEquals('list val must be an array', $v->lastError());
606606
}
607+
608+
public function testIssue13(): void
609+
{
610+
$rule = [
611+
['goods_id', 'list', 'msg' => '商品id数组为空或不合法'],
612+
['goods_id.*', 'each', 'integer', 'msg' => '商品分类id必须是一串数字']
613+
];
614+
615+
$v = Validation::check([
616+
'goods_id' => [
617+
1144181460261978556, 114418146, 1144
618+
]
619+
], $rule);
620+
621+
$this->assertTrue($v->isOk());
622+
$this->assertFalse($v->isFail());
623+
624+
// not array
625+
$v = Validation::check([
626+
'goods_id' => 'string'
627+
], $rule);
628+
$this->assertFalse($v->isOk());
629+
$this->assertSame('商品id数组为空或不合法', $v->firstError());
630+
631+
// not list
632+
$v = Validation::check([
633+
'goods_id' => ['k' => 'v']
634+
], $rule);
635+
$this->assertFalse($v->isOk());
636+
$this->assertSame('商品id数组为空或不合法', $v->firstError());
637+
638+
// value not int
639+
$v = Validation::check([
640+
'goods_id' => ['v']
641+
], $rule);
642+
$this->assertFalse($v->isOk());
643+
$this->assertSame('商品分类id必须是一串数字', $v->firstError());
644+
}
607645
}

0 commit comments

Comments
 (0)