Skip to content

Commit

Permalink
Merge pull request yiisoft#13216 from yiisoft/arrayhelper-error
Browse files Browse the repository at this point in the history
fix ArrayHelper::getValue() to throw exception on invalid input
  • Loading branch information
SilverFire authored Dec 18, 2016
2 parents 3624eae + e963b2a commit 8562055
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Yii Framework 2 Change Log
- Bug #12879: Console progress bar was not working properly in Windows terminals (samdark, kids-return)
- Bug #12880: Fixed `yii\behaviors\AttributeTypecastBehavior` marks attributes with `null` value as 'dirty' (klimov-paul)
- Bug #12904: Fixed lowercase table name in migrations (zlakomanoff)
- Bug #12927: `ArrayHelper::getValue()` did not throw exception if the input was neither an object nor an array, even though it was documented (cebe)
- Bug #12939: Hard coded table names for MSSQL in RBAC migration (arogachev)
- Bug #12974: Fixed incorrect order of migrations history in case `yii\console\controllers\MigrateController::$migrationNamespaces` is in use (evgen-d, klimov-paul)
- Bug #13071: Help option for commands was not working in modules (arogachev, haimanman)
Expand Down
7 changes: 6 additions & 1 deletion framework/helpers/BaseArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,15 @@ public static function merge($a, $b)
* The possibility to pass an array of keys is available since version 2.0.4.
* @param mixed $default the default value to be returned if the specified array key does not exist. Not used when
* getting value from an object.
* @return mixed the value of the element if found, default value otherwise
* @return mixed the value of the element if found, default value otherwise.
* @throws InvalidParamException if $array is neither an array nor an object.
*/
public static function getValue($array, $key, $default = null)
{
if (!is_array($array) && !is_object($array)) {
throw new InvalidParamException('Argument passed to getValue() must be an array or object.');
}

if ($key instanceof \Closure) {
return $key($array, $default);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/framework/helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace yiiunit\framework\helpers;

use yii\base\InvalidParamException;
use yii\base\Object;
use yii\helpers\ArrayHelper;
use yiiunit\TestCase;
Expand Down Expand Up @@ -734,6 +735,36 @@ public function testGetValueNonexistingProperties2()
$this->assertEquals(23, ArrayHelper::getValue($arrayObject, 'nonExisting'));
}

public function invalidArgumentProvider()
{
return [
[null],
[false],
[true],
[42],
[''],
['not an object'],
];
}

/**
* @dataProvider invalidArgumentProvider
* @expectedException \yii\base\InvalidParamException
*/
public function testGetValueInvalidArgumentWithoutDefaultValue($arg)
{
ArrayHelper::getValue($arg, 'test');
}

/**
* @dataProvider invalidArgumentProvider
* @expectedException \yii\base\InvalidParamException
*/
public function testGetValueInvalidArgumentWithDefaultValue($arg)
{
ArrayHelper::getValue($arg, 'test', 'default');
}

public function testIsAssociative()
{
$this->assertFalse(ArrayHelper::isAssociative('test'));
Expand Down

0 comments on commit 8562055

Please sign in to comment.