Skip to content

Commit 9ac09cd

Browse files
committed
update some unit tests
1 parent e75ec76 commit 9ac09cd

File tree

3 files changed

+94
-63
lines changed

3 files changed

+94
-63
lines changed

src/Helper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public static function call($cb, ...$args)
282282
// className::method
283283
if (strpos($cb, '::') > 0) {
284284
$cb = explode('::', $cb, 2);
285-
// function
285+
// function
286286
} elseif (function_exists($cb)) {
287287
return $cb(...$args);
288288
}

src/ValidationTrait.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -636,32 +636,34 @@ protected function getByWildcard(string $path, $default = null, array $data = []
636636

637637
$result = [];
638638

639-
// eg: "companies.*.departments.*.employees.*.name" => "departments.*.employees.*.name"
639+
// eg: "companies.*.departments.*.employees.*.name" => $subPath: "departments.*.employees.*.name"
640640
if (strpos($subPath, '.*') > 0) {
641641
foreach ($recently as $item) {
642642
if (is_array($item)) {
643-
// $result[] = $this->getByWildcard($subPath, $this->_arrayNotKeyValue, $item);
644643
$result[] = $this->getByWildcard($subPath, $this->_arrayNotKeyValue, $item);
645644
}
646645
}
647646

648647
// return $result;
648+
// expand all sub-values one dimensional array. eg: [[1, 2], [3, 4]] => [1, 2, 3, 4]
649649
return array_merge(...$result);
650650
}
651651

652-
// eg: "companies.0.departments.*.employees.0.manage" => "employees.0.manage"
652+
// eg: "companies.0.departments.*.employees.0.manage" => $subPath: "employees.0.manage"
653653
if (strpos($subPath, '.') > 0) {
654654
foreach ($recently as $item) {
655655
if (is_array($item)) {
656656
$result[] = Helper::getValueOfArray($item, $subPath, $this->_arrayNotKeyValue);
657657
}
658658
}
659-
} else {
660-
// eg: 'users.*.id' => 'id'
661-
foreach ($recently as $item) {
662-
if (is_array($item)) {
663-
$result[] = $item[$subPath] ?? $this->_arrayNotKeyValue;
664-
}
659+
660+
return $result;
661+
}
662+
663+
// eg: 'users.*.id' => $subPath: 'id'
664+
foreach ($recently as $item) {
665+
if (is_array($item)) {
666+
$result[] = $item[$subPath] ?? $this->_arrayNotKeyValue;
665667
}
666668
}
667669

test/ValidationTraitTest.php

+82-53
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ public function testGetByPath(): void
3434
'attr' => [
3535
'wid' => 1
3636
]
37-
]
37+
],
38+
[
39+
'attr' => [
40+
'wid' => 2,
41+
]
42+
],
43+
[
44+
'attr' => [
45+
'wid' => 3,
46+
]
47+
],
3848
],
3949
'users' => [
4050
['id' => 1,],
@@ -55,61 +65,31 @@ public function testGetByPath(): void
5565
$this->assertSame(1, $val);
5666

5767
$val = $v->getByPath('prod.*.attr');
58-
$this->assertSame([['wid' => 1]], $val);
68+
$this->assertSame([['wid' => 1], ['wid' => 2], ['wid' => 3],], $val);
5969

60-
// $val = $v->getByPath('prod.*.attr.wid');
61-
// $this->assertSame([1], $val);
62-
}
63-
64-
public function testBeforeAndAfter(): void
65-
{
66-
$v = Validation::make(['name' => 'inhere'], [
67-
['name', 'string', 'min' => 3, 'filter' => 'trim|upper']
68-
]);
69-
70-
$v->onBeforeValidate(function (Validation $v) {
71-
$this->assertSame('inhere', $v->getRaw('name'));
72-
$this->assertNull($v->getSafe('name'));
73-
74-
return true;
75-
});
76-
77-
$v->onAfterValidate(function (Validation $v) {
78-
$this->assertSame('INHERE', $v->getRaw('name'));
79-
$this->assertSame('INHERE', $v->getSafe('name'));
80-
});
81-
82-
$v->validate();
83-
84-
$this->assertTrue($v->isOk());
85-
$this->assertTrue($v->isValidated());
86-
87-
$v->validate();
88-
89-
$this->assertTrue($v->isValidated());
90-
}
91-
92-
public function testRuleBeforeAndAfter(): void
93-
{
94-
$v = Validation::make(['name' => 'inhere'], [
95-
[
96-
'name',
97-
'string',
98-
'min' => 3,
99-
'before' => function ($value) {
100-
return $value === 'inhere';
101-
},
102-
'after' => function ($value) {
103-
$this->assertSame('inhere', $value);
104-
return true;
105-
}
106-
]
107-
]);
108-
109-
$v->validate();
110-
$this->assertTrue($v->isOk());
70+
$val = $v->getByPath('prod.*.attr.wid');
71+
$this->assertSame([1, 2, 3], $val);
11172
}
11273

74+
// TODO key is must exists on data.
75+
// public function testIndexedArrayGetByPath(): void
76+
// {
77+
// $v = Validation::make([
78+
// ['attr' => ['wid' => 1]],
79+
// ['attr' => ['wid' => 2]],
80+
// ['attr' => ['wid' => 3]],
81+
// ]);
82+
//
83+
// $val = $v->GetByPath('0.attr');
84+
// $this->assertSame(['wid' => 1], $val);
85+
//
86+
// $val = $v->getByPath('0.attr.wid');
87+
// $this->assertSame(1, $val);
88+
// }
89+
90+
/**
91+
* @var \array[][] see PR https://github.com/inhere/php-validate/pull/19
92+
*/
11393
public $deepData = [
11494
'companies' => [
11595
[
@@ -208,4 +188,53 @@ public function testMultidimensionalArray3(): void
208188
$val = $v->getByPath('companies.*.departments.*.employees.*.name');
209189
$this->assertSame(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'xxx', 'yyy'], $val);
210190
}
191+
192+
public function testBeforeAndAfter(): void
193+
{
194+
$v = Validation::make(['name' => 'inhere'], [
195+
['name', 'string', 'min' => 3, 'filter' => 'trim|upper']
196+
]);
197+
198+
$v->onBeforeValidate(function (Validation $v) {
199+
$this->assertSame('inhere', $v->getRaw('name'));
200+
$this->assertNull($v->getSafe('name'));
201+
202+
return true;
203+
});
204+
205+
$v->onAfterValidate(function (Validation $v) {
206+
$this->assertSame('INHERE', $v->getRaw('name'));
207+
$this->assertSame('INHERE', $v->getSafe('name'));
208+
});
209+
210+
$v->validate();
211+
212+
$this->assertTrue($v->isOk());
213+
$this->assertTrue($v->isValidated());
214+
215+
$v->validate();
216+
217+
$this->assertTrue($v->isValidated());
218+
}
219+
220+
public function testRuleBeforeAndAfter(): void
221+
{
222+
$v = Validation::make(['name' => 'inhere'], [
223+
[
224+
'name',
225+
'string',
226+
'min' => 3,
227+
'before' => function ($value) {
228+
return $value === 'inhere';
229+
},
230+
'after' => function ($value) {
231+
$this->assertSame('inhere', $value);
232+
return true;
233+
}
234+
]
235+
]);
236+
237+
$v->validate();
238+
$this->assertTrue($v->isOk());
239+
}
211240
}

0 commit comments

Comments
 (0)