Skip to content

Commit 2f42c17

Browse files
committed
update some tests
1 parent fd5e556 commit 2f42c17

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ php:
1212
# env: ANALYSIS='true'
1313

1414
before_script:
15+
- php -m
1516
- composer require php-coveralls/php-coveralls:^2.1.0
1617

1718
script:

src/Helper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,17 @@ public static function getValueOfArray(array $array, $key, $default = null)
188188
return $default;
189189
}
190190

191+
$found = false;
191192
foreach (\explode('.', $key) as $segment) {
192193
if (\is_array($array) && \array_key_exists($segment, $array)) {
194+
$found = true;
193195
$array = $array[$segment];
194196
} else {
195197
return $default;
196198
}
197199
}
198200

199-
return $array;
201+
return $found ? $array : $default;
200202
}
201203

202204
/**

test/FieldValidationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testValidateField()
4848
$errors = $v->getErrors();
4949
$this->assertNotEmpty($errors);
5050
$this->assertCount(3, $errors);
51+
$this->assertSame('freeTime is required!!!!', $v->getErrors('freeTime')[0]);
5152

5253
$v = FieldValidation::check($this->data, [
5354
['userId', 'required|int'],

test/HelperTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Inhere\ValidateTest;
1010

11+
use Inhere\Validate\Filter\Filters;
1112
use Inhere\Validate\Helper;
1213
use PHPUnit\Framework\TestCase;
1314

@@ -51,10 +52,38 @@ public function testCompareSize()
5152
$this->assertFalse(Helper::compareSize(5, 'invalid', 3));
5253
}
5354

55+
public function testGetValueOfArray()
56+
{
57+
$data = [
58+
'user' => [
59+
'name' => 'inhere',
60+
'age' => 1,
61+
]
62+
];
63+
64+
$this->assertNull(Helper::getValueOfArray($data, 'not-exist'));
65+
$this->assertSame($data, Helper::getValueOfArray($data, null));
66+
}
67+
5468
public function testCall()
5569
{
70+
// function
5671
$this->assertSame(34, Helper::call('intval', '34'));
5772

73+
// class:;method
74+
$this->assertSame(34, Helper::call(Filters::class . '::integer', '34'));
75+
76+
$callabled = new class {
77+
public function __invoke($str)
78+
{
79+
return (int)$str;
80+
}
81+
};
82+
83+
// callabled object
84+
$this->assertSame(34, Helper::call($callabled, '34'));
85+
86+
// invalid
5887
$this->expectException(\InvalidArgumentException::class);
5988
Helper::call('oo-invalid');
6089
}

0 commit comments

Comments
 (0)