Skip to content

Commit 39e484f

Browse files
authored
add more unit tests (#11)
add more unit tests
2 parents ff378a3 + f53f662 commit 39e484f

File tree

3 files changed

+95
-32
lines changed

3 files changed

+95
-32
lines changed

src/ValidationTrait.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ protected function applyRule($fields, array $rule, array $onlyChecked)
246246

247247
$value = $this->getByPath($field, $defValue);
248248

249+
// Field value filtering(有通配符`*`的字段, 不应用过滤器)
250+
if ($filters && null !== $value && !\strpos($field, '.*')) {
251+
$value = $this->valueFiltering($value, $filters);
252+
// save
253+
$this->data[$field] = $value;
254+
}
255+
256+
// Field value validate
249257
if (\is_string($validator)) {
250258
if ($validator === 'safe') {
251259
$this->setSafe($field, $value);
@@ -268,13 +276,6 @@ protected function applyRule($fields, array $rule, array $onlyChecked)
268276
continue;
269277
}
270278

271-
// Field value filtering(有通配符`*`的字段, 不应用过滤器)
272-
if ($filters && !\strpos($field, '.*')) {
273-
$value = $this->valueFiltering($value, $filters);
274-
// save
275-
$this->data[$field] = $value;
276-
}
277-
278279
// Field value verification check
279280
if (!$this->valueValidate($field, $value, $validator, $args, $defMsg) && $this->isStopOnError()) {
280281
break;
@@ -359,7 +360,7 @@ protected function valueValidate(string $field, $value, $validator, array $args,
359360
// if $validator is a custom method of the subclass.
360361
} elseif (\method_exists($this, $method = $validator . 'Validator')) {
361362
$passed = $this->$method($value, ...$args);
362-
// if $validator is a custom validator {@see $_validators}.
363+
// if $validator is a global custom validator {@see UserValidators::$validators}.
363364
} elseif ($callback = UserValidators::get($validator)) {
364365
$args[] = $this->data;
365366
$passed = $callback($value, ...$args);

test/RuleValidationTest.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -244,23 +244,27 @@ public function testCollectRules()
244244

245245
public function testValidatePassed()
246246
{
247-
$data = $this->data;
247+
$data = $this->data;
248+
// change value
248249
$data['userId'] = '456';
249-
$rules = [
250+
251+
$rules = [
250252
// ['tagId,userId,freeTime', 'required'],// set message
251253
['tagId,userId,freeTime', 'number', 'filter' => 'int'],
252254
['tagId', 'size', 'max' => 567, 'min' => 4, 'filter' => 'int'], // 4<= tagId <=567
253255
// ['goods', 'isList'],
254256
['goods.pear', 'max', 60],
257+
['insertTime', 'safe']
255258
];
256-
$v = RuleValidation::make($data, $rules)
257-
->setTranslates([
258-
'goods.pear' => '梨子'
259-
])
260-
->setMessages([
261-
'freeTime.required' => 'freeTime is required!!!!'
262-
])
263-
->validate([], false);
259+
260+
$v = RuleValidation::make($data, $rules);
261+
$v->setTranslates([
262+
'goods.pear' => '梨子'
263+
])
264+
->setMessages([
265+
'freeTime.required' => 'freeTime is required!!!!'
266+
])
267+
->validate([], false);
264268

265269
$this->assertTrue($v->isOk());
266270
$this->assertFalse($v->failed());
@@ -274,25 +278,26 @@ public function testValidateFailed()
274278
{
275279
$rules = $this->someRules();
276280
ob_start();
277-
$v = RuleValidation::make($this->data, $rules)
278-
->setTranslates([
279-
'goods.pear' => '梨子'
280-
])
281-
->setMessages([
282-
'freeTime.required' => 'freeTime is required!!!!'
283-
])
284-
->validate([], false);
281+
$v = RuleValidation::make($this->data, $rules);
282+
$v->setTranslates([
283+
'goods.pear' => '梨子'
284+
])
285+
->setMessages([
286+
'freeTime.required' => 'freeTime is required!!!!'
287+
])
288+
->validate([], false);
289+
285290
$out = ob_get_clean();
286291

287292
$needle = 'use when pre-check';
288-
if (\version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<') ) {
293+
if (\version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) {
289294
$this->assertContains($needle, $out);
290295
} else {
291296
$this->assertStringContainsString($needle, $out);
292297
}
293298

294299
$needle = 'use custom validate';
295-
if (\version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<') ) {
300+
if (\version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) {
296301
$this->assertContains($needle, $out);
297302
} else {
298303
$this->assertStringContainsString($needle, $out);
@@ -478,10 +483,10 @@ public function testRange()
478483
['num', 'range', 'min' => 1, 'max' => 100],
479484
['id', 'range', 'min' => 1, 'max' => 100],
480485
])
481-
->setMessages([
482-
'id.range' => 'range error message',
483-
])
484-
->validate();
486+
->setMessages([
487+
'id.range' => 'range error message',
488+
])
489+
->validate();
485490

486491
$this->assertFalse($v->isOk());
487492
$this->assertCount(1, $v->getErrors());

test/ValidationTraitTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Inhere\ValidateTest;
4+
5+
use Inhere\Validate\Validation;
6+
use Inhere\Validate\ValidationTrait;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* Class ValidationTraitTest
11+
* @package Inhere\ValidateTest
12+
*/
13+
class ValidationTraitTest extends TestCase
14+
{
15+
public function testNoDataProperty()
16+
{
17+
$v = new class {
18+
use ValidationTrait;
19+
};
20+
21+
// want data property
22+
$this->expectException(\InvalidArgumentException::class);
23+
$v->validate();
24+
25+
$v = Validation::check(['name' => 'inhere'], [
26+
['name', 'requred']
27+
]);
28+
}
29+
30+
public function testBeforeAndAfter()
31+
{
32+
$v = Validation::make(['name' => 'inhere'], [
33+
['name', 'string', 'min' => 3, 'filter' => 'trim|upper']
34+
]);
35+
36+
$v->onBeforeValidate(function (Validation $v) {
37+
$this->assertSame('inhere', $v->getRaw('name'));
38+
$this->assertNull($v->getSafe('name'));
39+
40+
return true;
41+
});
42+
43+
$v->onAfterValidate(function (Validation $v) {
44+
$this->assertSame('INHERE', $v->getRaw('name'));
45+
$this->assertSame('INHERE', $v->getSafe('name'));
46+
});
47+
48+
$v->validate();
49+
50+
$this->assertTrue($v->isOk());
51+
$this->assertTrue($v->isValidated());
52+
53+
$v->validate();
54+
55+
$this->assertTrue($v->isValidated());
56+
}
57+
}

0 commit comments

Comments
 (0)