Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Jan 29, 2021
1 parent f3577e7 commit d4f6ca8
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 19 deletions.
4 changes: 2 additions & 2 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ protected function processRules(string $field, string $label = null, $value, $ru
if (in_array('if_exist', $rules, true))
{
// If the if_exist rule is defined
// and the current field does not exists in the input data
// we can return true. Ignoring all other rules to this field.
// and the current field does not exist in the input data
// we can return true, ignoring all other rules to this field.
if (! array_key_exists($field, array_flatten_with_dots($data)))
{
return true;
Expand Down
107 changes: 90 additions & 17 deletions tests/system/Helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,24 +323,97 @@ public static function sortByMultipleKeysProvider()
];
}

public function testArrayFlattening(): void
/**
* @dataProvider arrayFlattenProvider
*
* @param iterable $input
* @param iterable $expected
*
* @return void
*/
public function testArrayFlattening($input, $expected): void
{
$vars = [
'id' => '12',
'user' => [
'first_name' => 'john',
'last_name' => 'smith',
'age' => '26 years',
],
];

$flattened = [
'id' => '12',
'user.first_name' => 'john',
'user.last_name' => 'smith',
'user.age' => '26 years',
];
$this->assertSame($expected, array_flatten_with_dots($input));
}

$this->assertSame($flattened, array_flatten_with_dots($vars));
public function arrayFlattenProvider(): iterable
{
yield 'normal' => [
[
'id' => '12',
'user' => [
'first_name' => 'john',
'last_name' => 'smith',
'age' => '26 years',
],
],
[
'id' => '12',
'user.first_name' => 'john',
'user.last_name' => 'smith',
'user.age' => '26 years',
],
];

yield 'many-levels' => [
[
'foo' => 1,
'bar' => [
'bax' => [
'baz' => 2,
'biz' => 3,
],
],
'baz' => [
'fizz' => 4,
],
],
[
'foo' => 1,
'bar.bax.baz' => 2,
'bar.bax.biz' => 3,
'baz.fizz' => 4,
],
];

yield 'with-empty-arrays' => [
[
'foo' => 'bar',
'baz' => [],
'bar' => [
'fizz' => 'buzz',
'nope' => 'yeah',
'why' => [],
],
],
[
'foo' => 'bar',
'bar.fizz' => 'buzz',
'bar.nope' => 'yeah',
],
];

yield 'with-mixed-empty' => [
[
'foo' => 1,
'' => [
'bar' => 2,
'baz' => 3,
],
0 => [
'fizz' => 4,
],
1 => [
'buzz' => 5,
],
],
[
'foo' => 1,
'.bar' => 2,
'.baz' => 3,
'0.fizz' => 4,
'1.buzz' => 5,
],
];
}
}

0 comments on commit d4f6ca8

Please sign in to comment.