Skip to content

Commit

Permalink
update some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 26, 2020
1 parent e75ec76 commit 9ac09cd
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public static function call($cb, ...$args)
// className::method
if (strpos($cb, '::') > 0) {
$cb = explode('::', $cb, 2);
// function
// function
} elseif (function_exists($cb)) {
return $cb(...$args);
}
Expand Down
20 changes: 11 additions & 9 deletions src/ValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,32 +636,34 @@ protected function getByWildcard(string $path, $default = null, array $data = []

$result = [];

// eg: "companies.*.departments.*.employees.*.name" => "departments.*.employees.*.name"
// eg: "companies.*.departments.*.employees.*.name" => $subPath: "departments.*.employees.*.name"
if (strpos($subPath, '.*') > 0) {
foreach ($recently as $item) {
if (is_array($item)) {
// $result[] = $this->getByWildcard($subPath, $this->_arrayNotKeyValue, $item);
$result[] = $this->getByWildcard($subPath, $this->_arrayNotKeyValue, $item);
}
}

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

// eg: "companies.0.departments.*.employees.0.manage" => "employees.0.manage"
// eg: "companies.0.departments.*.employees.0.manage" => $subPath: "employees.0.manage"
if (strpos($subPath, '.') > 0) {
foreach ($recently as $item) {
if (is_array($item)) {
$result[] = Helper::getValueOfArray($item, $subPath, $this->_arrayNotKeyValue);
}
}
} else {
// eg: 'users.*.id' => 'id'
foreach ($recently as $item) {
if (is_array($item)) {
$result[] = $item[$subPath] ?? $this->_arrayNotKeyValue;
}

return $result;
}

// eg: 'users.*.id' => $subPath: 'id'
foreach ($recently as $item) {
if (is_array($item)) {
$result[] = $item[$subPath] ?? $this->_arrayNotKeyValue;
}
}

Expand Down
135 changes: 82 additions & 53 deletions test/ValidationTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ public function testGetByPath(): void
'attr' => [
'wid' => 1
]
]
],
[
'attr' => [
'wid' => 2,
]
],
[
'attr' => [
'wid' => 3,
]
],
],
'users' => [
['id' => 1,],
Expand All @@ -55,61 +65,31 @@ public function testGetByPath(): void
$this->assertSame(1, $val);

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

// $val = $v->getByPath('prod.*.attr.wid');
// $this->assertSame([1], $val);
}

public function testBeforeAndAfter(): void
{
$v = Validation::make(['name' => 'inhere'], [
['name', 'string', 'min' => 3, 'filter' => 'trim|upper']
]);

$v->onBeforeValidate(function (Validation $v) {
$this->assertSame('inhere', $v->getRaw('name'));
$this->assertNull($v->getSafe('name'));

return true;
});

$v->onAfterValidate(function (Validation $v) {
$this->assertSame('INHERE', $v->getRaw('name'));
$this->assertSame('INHERE', $v->getSafe('name'));
});

$v->validate();

$this->assertTrue($v->isOk());
$this->assertTrue($v->isValidated());

$v->validate();

$this->assertTrue($v->isValidated());
}

public function testRuleBeforeAndAfter(): void
{
$v = Validation::make(['name' => 'inhere'], [
[
'name',
'string',
'min' => 3,
'before' => function ($value) {
return $value === 'inhere';
},
'after' => function ($value) {
$this->assertSame('inhere', $value);
return true;
}
]
]);

$v->validate();
$this->assertTrue($v->isOk());
$val = $v->getByPath('prod.*.attr.wid');
$this->assertSame([1, 2, 3], $val);
}

// TODO key is must exists on data.
// public function testIndexedArrayGetByPath(): void
// {
// $v = Validation::make([
// ['attr' => ['wid' => 1]],
// ['attr' => ['wid' => 2]],
// ['attr' => ['wid' => 3]],
// ]);
//
// $val = $v->GetByPath('0.attr');
// $this->assertSame(['wid' => 1], $val);
//
// $val = $v->getByPath('0.attr.wid');
// $this->assertSame(1, $val);
// }

/**
* @var \array[][] see PR https://github.com/inhere/php-validate/pull/19
*/
public $deepData = [
'companies' => [
[
Expand Down Expand Up @@ -208,4 +188,53 @@ public function testMultidimensionalArray3(): void
$val = $v->getByPath('companies.*.departments.*.employees.*.name');
$this->assertSame(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'xxx', 'yyy'], $val);
}

public function testBeforeAndAfter(): void
{
$v = Validation::make(['name' => 'inhere'], [
['name', 'string', 'min' => 3, 'filter' => 'trim|upper']
]);

$v->onBeforeValidate(function (Validation $v) {
$this->assertSame('inhere', $v->getRaw('name'));
$this->assertNull($v->getSafe('name'));

return true;
});

$v->onAfterValidate(function (Validation $v) {
$this->assertSame('INHERE', $v->getRaw('name'));
$this->assertSame('INHERE', $v->getSafe('name'));
});

$v->validate();

$this->assertTrue($v->isOk());
$this->assertTrue($v->isValidated());

$v->validate();

$this->assertTrue($v->isValidated());
}

public function testRuleBeforeAndAfter(): void
{
$v = Validation::make(['name' => 'inhere'], [
[
'name',
'string',
'min' => 3,
'before' => function ($value) {
return $value === 'inhere';
},
'after' => function ($value) {
$this->assertSame('inhere', $value);
return true;
}
]
]);

$v->validate();
$this->assertTrue($v->isOk());
}
}

0 comments on commit 9ac09cd

Please sign in to comment.