Skip to content

Commit 13e7ddf

Browse files
committed
add more tests. update some info
1 parent 2426184 commit 13e7ddf

File tree

5 files changed

+52
-30
lines changed

5 files changed

+52
-30
lines changed

src/AbstractValidation.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace Inhere\Validate;
1010

1111
use InvalidArgumentException;
12-
use RuntimeException;
1312

1413
/**
1514
* Class AbstractValidation
@@ -36,7 +35,6 @@ abstract class AbstractValidation implements ValidationInterface
3635
* @param bool $startValidate 立即开始验证
3736
*
3837
* @throws InvalidArgumentException
39-
* @throws RuntimeException
4038
*/
4139
public function __construct(
4240
array $data = [],
@@ -77,7 +75,6 @@ public static function quick(array $data, string $scene = '', bool $startValidat
7775
*
7876
* @return static
7977
* @throws InvalidArgumentException
80-
* @throws RuntimeException
8178
*/
8279
public static function make(
8380
array $data,
@@ -99,7 +96,6 @@ public static function make(
9996
*
10097
* @return static
10198
* @throws InvalidArgumentException
102-
* @throws RuntimeException
10399
*/
104100
public static function makeAndValidate(array $data, array $rules = [], array $translates = [], string $scene = '')
105101
{
@@ -116,7 +112,6 @@ public static function makeAndValidate(array $data, array $rules = [], array $tr
116112
*
117113
* @return static
118114
* @throws InvalidArgumentException
119-
* @throws RuntimeException
120115
*/
121116
public static function check(array $data, array $rules = [], array $translates = [], string $scene = '')
122117
{

src/ValidationInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Inhere\Validate;
88

9-
use RuntimeException;
109
use stdClass;
1110

1211
/**
@@ -39,7 +38,6 @@ public function translates(): array;
3938
* @param bool|null $stopOnError 是否出现错误即停止验证
4039
*
4140
* @return static
42-
* @throws RuntimeException
4341
*/
4442
public function validate(array $onlyChecked = [], bool $stopOnError = null);
4543

src/ValidationTrait.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Inhere\Validate\Traits\ScopedValidatorsTrait;
1717
use Inhere\Validate\Validator\UserValidators;
1818
use InvalidArgumentException;
19-
use RuntimeException;
2019
use stdClass;
2120
use function array_keys;
2221
use function array_merge;
@@ -186,7 +185,6 @@ public function afterValidate(): void
186185
*
187186
* @return static
188187
* @throws InvalidArgumentException
189-
* @throws RuntimeException
190188
*/
191189
public function validate(array $onlyChecked = [], bool $stopOnError = null)
192190
{
@@ -570,7 +568,7 @@ protected function getByWildcard(string $path, $default = null)
570568
$result = [];
571569

572570
foreach ($recently as $item) {
573-
if (isset($item[$field])) {
571+
if (is_array($item) && isset($item[$field])) {
574572
$result[] = $item[$field];
575573
}
576574
}

test/RuleValidationTest.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ public function testRequiredWith(): void
132132
['userId', 'requiredWith', ['status', 'someField']],
133133
]);
134134

135-
// var_dump($v->getErrors());
136-
137135
$this->assertCount(1, $v->getErrors());
138136
$this->assertFalse($v->inError('targetId'));
139137
$this->assertTrue($v->inError('userId'));
@@ -155,8 +153,6 @@ public function testRequiredWithAll(): void
155153
['userId', 'requiredWithAll', ['status', 'someField']],
156154
]);
157155

158-
// var_dump($v->getErrors());
159-
160156
$this->assertCount(1, $v->getErrors());
161157
$this->assertTrue($v->inError('targetId'));
162158
$this->assertFalse($v->inError('userId'));
@@ -178,8 +174,6 @@ public function testRequiredWithout(): void
178174
['userId', 'requiredWithout', ['status', 'someField']],
179175
]);
180176

181-
// var_dump($v->getErrors());
182-
183177
$this->assertCount(1, $v->getErrors());
184178
$this->assertTrue($v->inError('userId'));
185179
$this->assertFalse($v->inError('targetId'));
@@ -201,8 +195,6 @@ public function testRequiredWithoutAll(): void
201195
['userId', 'requiredWithoutAll', ['status', 'someField']],
202196
]);
203197

204-
// var_dump($v->getErrors());
205-
206198
$this->assertCount(1, $v->getErrors());
207199
$this->assertTrue($v->inError('targetId'));
208200
$this->assertFalse($v->inError('userId'));
@@ -388,7 +380,6 @@ public function testValidateJson(): void
388380
['log_data1', 'json', false],
389381
])->validate();
390382

391-
// var_dump($v->getErrors());
392383
$this->assertTrue($v->isOk());
393384
$this->assertFalse($v->failed());
394385

@@ -434,7 +425,6 @@ protected function someRules(): array
434425
function () {
435426
echo " use custom validate to check userId \n";
436427

437-
// var_dump($value, $data);
438428
// echo __LINE__ . "\n";
439429

440430
return false;
@@ -469,7 +459,6 @@ public function testArrayValidate(): void
469459
['options.opt1, options.opt4', 'bool'],
470460
['options.opt1, options.opt4', 'in', [true, false]],
471461
]);
472-
// var_dump($v->getErrors());die;
473462

474463
$this->assertTrue($v->isOk());
475464
$this->assertFalse($v->failed());
@@ -532,7 +521,6 @@ public function testDistinct(): void
532521
['users.*.id', 'distinct'],
533522
]);
534523

535-
// var_dump($v->getErrors());
536524
$this->assertFalse($v->isOk());
537525
$this->assertCount(1, $v->getErrors());
538526
$this->assertTrue($v->inError('tags'));
@@ -555,15 +543,32 @@ public function testEach(): void
555543
['users.*.name', 'each', 'string', 'min' => 5],
556544
]);
557545

558-
// var_dump($v->getErrors());
559546
$this->assertFalse($v->isOk());
560547
$this->assertCount(1, $v->getErrors());
561548
$this->assertTrue($v->inError('users.*.name'));
562549
}
563550

564-
/**
565-
* @covers \Inhere\Validate\RuleValidation::getMessage()
566-
*/
551+
public function testMultiLevelData(): void
552+
{
553+
$v = RuleValidation::check([
554+
'prod' => [
555+
'key0' => 'val0',
556+
[
557+
'attr' => [
558+
'wid' => 1
559+
]
560+
]
561+
]
562+
], [
563+
['prod.*.attr', 'each', 'required'],
564+
// ['prod.*.attr.wid', 'each', 'required'],
565+
['prod.0.attr.wid', 'number'],
566+
]);
567+
568+
$this->assertTrue($v->isOk());
569+
$this->assertNotEmpty($v->getSafeData());
570+
}
571+
567572
public function testGetMessage(): void
568573
{
569574
$v = Validation::check([
@@ -572,7 +577,7 @@ public function testGetMessage(): void
572577
['inTest', 'in', [1, 2]],
573578
]);
574579

575-
$this->assertFalse($v->ok());
580+
$this->assertFalse($v->isOk());
576581
$this->assertEquals('in test must in (1,2)', $v->firstError());
577582
}
578583

@@ -585,7 +590,7 @@ public function testValidatorAlias(): void
585590
['arrTest', 'array'],
586591
]);
587592

588-
$this->assertTrue($v->ok());
593+
$this->assertTrue($v->isOk());
589594

590595
$v = Validation::make([
591596
'arrVal' => 'string',

test/ValidationTraitTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/**
1111
* Class ValidationTraitTest
12+
*
1213
* @package Inhere\ValidateTest
1314
*/
1415
class ValidationTraitTest extends TestCase
@@ -23,10 +24,35 @@ public function testNoDataProperty(): void
2324
// want data property
2425
$this->expectException(InvalidArgumentException::class);
2526
$v->validate();
27+
}
2628

27-
$v = Validation::check(['name' => 'inhere'], [
28-
['name', 'requred']
29+
public function testGetByPath(): void
30+
{
31+
$v = Validation::make([
32+
'prod' => [
33+
'key0' => 'val0',
34+
[
35+
'attr' => [
36+
'wid' => 1
37+
]
38+
]
39+
]
2940
]);
41+
42+
$val = $v->getByPath('prod.key0');
43+
$this->assertSame('val0', $val);
44+
45+
$val = $v->getByPath('prod.0.attr');
46+
$this->assertSame(['wid' => 1], $val);
47+
48+
$val = $v->getByPath('prod.0.attr.wid');
49+
$this->assertSame(1, $val);
50+
51+
$val = $v->getByPath('prod.*.attr');
52+
$this->assertSame([['wid' => 1]], $val);
53+
54+
// $val = $v->getByPath('prod.*.attr.wid');
55+
// $this->assertSame([1], $val);
3056
}
3157

3258
public function testBeforeAndAfter(): void

0 commit comments

Comments
 (0)