Skip to content

Commit 8065b9c

Browse files
committed
add more unit tests
1 parent 123cf01 commit 8065b9c

File tree

5 files changed

+123
-4
lines changed

5 files changed

+123
-4
lines changed

docs/Valid.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Inhere\Validate;
4+
5+
/**
6+
* Class Valid - Simple Data Validator
7+
* @package Inhere\Validate
8+
*/
9+
class Valid
10+
{
11+
/**
12+
* @var array
13+
*/
14+
protected $data = [];
15+
16+
/**
17+
* @param array $data
18+
*
19+
* @return Valid
20+
*/
21+
public static function new(array $data): self
22+
{
23+
return new static($data);
24+
}
25+
26+
/**
27+
* Validator constructor.
28+
*
29+
* @param array $data
30+
*/
31+
public function __construct(array $data)
32+
{
33+
$this->data = $data;
34+
}
35+
36+
public function getInt(string $field, $min = null, $max = null, $default = null): int
37+
{
38+
39+
}
40+
41+
/**
42+
* @return array
43+
*/
44+
public function getData(): array
45+
{
46+
return $this->data;
47+
}
48+
49+
/**
50+
* @param array $data
51+
*/
52+
public function setData(array $data): void
53+
{
54+
$this->data = $data;
55+
}
56+
}

src/Traits/MultipleRulesTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ protected function collectRules(): ?Generator
5555
foreach ($this->getRules() as $rule) {
5656
// check field
5757
if (!isset($rule[0]) || !$rule[0]) {
58-
throw new InvalidArgumentException('Please setting the field(string) to wait validate! position: rule[0].');
58+
throw new InvalidArgumentException('Please setting the field(string) to wait validate! position: rule[0]');
5959
}
6060

6161
// check validators
6262
if (!isset($rule[1]) || !$rule[1]) {
63-
throw new InvalidArgumentException('The field validators must be is a validator name(s) string! position: rule[1].');
63+
throw new InvalidArgumentException('The field validators must be is a validator name(s) string! position: rule[1]');
6464
}
6565

6666
// an rule for special scene.

src/ValidationTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,12 @@ protected function collectRules(): ?Generator
462462
foreach ($this->getRules() as $rule) {
463463
// check fields
464464
if (!isset($rule[0]) || !$rule[0]) {
465-
throw new InvalidArgumentException('Please setting the fields(string|array) to wait validate! position: rule[0].');
465+
throw new InvalidArgumentException('Please setting the fields(string|array) to wait validate! position: rule[0]');
466466
}
467467

468468
// check validator
469469
if (!isset($rule[1]) || !$rule[1]) {
470-
throw new InvalidArgumentException('The rule validator is must be setting! position: rule[1].');
470+
throw new InvalidArgumentException('The rule validator is must be setting! position: rule[1]');
471471
}
472472

473473
// only use to special scene.

test/FieldValidationTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
use Inhere\Validate\FieldValidation;
66
use Inhere\ValidateTest\Sample\FieldSample;
77
use PHPUnit\Framework\TestCase;
8+
use Throwable;
89

10+
/**
11+
* Class FieldValidationTest
12+
* @package Inhere\ValidateTest
13+
*/
914
class FieldValidationTest extends TestCase
1015
{
1116
public $data = [
@@ -26,6 +31,27 @@ class FieldValidationTest extends TestCase
2631
],
2732
];
2833

34+
public function testRuleCollectError(): void
35+
{
36+
$rv = FieldValidation::make(['name' => 'inhere'], [
37+
[]
38+
]);
39+
try {
40+
$rv->validate();
41+
} catch (Throwable $e) {
42+
$this->assertSame('Please setting the field(string) to wait validate! position: rule[0]', $e->getMessage());
43+
}
44+
45+
$rv = FieldValidation::make(['name' => 'inhere'], [
46+
['name']
47+
]);
48+
try {
49+
$rv->validate();
50+
} catch (Throwable $e) {
51+
$this->assertSame('The field validators must be is a validator name(s) string! position: rule[1]', $e->getMessage());
52+
}
53+
}
54+
2955
public function testValidateField(): void
3056
{
3157
$rules = [
@@ -79,6 +105,24 @@ public function testValidateField(): void
79105
);
80106
}
81107

108+
public function testOnScene(): void
109+
{
110+
$data = [
111+
'user' => 'inhere',
112+
'pwd' => '123456',
113+
'code' => '1234',
114+
];
115+
116+
$v = FieldValidation::make($data, [
117+
['user', 'required|string', 'on' => 's1'],
118+
['code', 'required|int', 'filter' => 'int', 'on' => 's2'],
119+
]);
120+
121+
$v->atScene('s1')->validate();
122+
123+
$this->assertCount(1, $v->getUsedRules());
124+
}
125+
82126
public function testScenarios(): void
83127
{
84128
$data = [

test/RuleValidationTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Inhere\Validate\Validation;
77
use PHPUnit\Framework\TestCase;
88
use PHPUnit\Runner\Version;
9+
use Throwable;
910
use function version_compare;
1011

1112
/**
@@ -28,6 +29,24 @@ public function testBasic(): void
2829
$this->assertTrue($v->has('key1'));
2930

3031
$this->assertFalse($v->hasRule());
32+
33+
$rv = RuleValidation::make(['name' => 'inhere'], [
34+
[]
35+
]);
36+
try {
37+
$rv->validate();
38+
} catch (Throwable $e) {
39+
$this->assertSame('Please setting the fields(string|array) to wait validate! position: rule[0]', $e->getMessage());
40+
}
41+
42+
$rv = RuleValidation::make(['name' => 'inhere'], [
43+
['name']
44+
]);
45+
try {
46+
$rv->validate();
47+
} catch (Throwable $e) {
48+
$this->assertSame('The rule validator is must be setting! position: rule[1]', $e->getMessage());
49+
}
3150
}
3251

3352
public function testRequired(): void

0 commit comments

Comments
 (0)