Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
adding regression tests for a bug when inserting a value for a/b/c wh…
Browse files Browse the repository at this point in the history
…en the array is empty
  • Loading branch information
mathiasgrimm committed Apr 26, 2016
1 parent 578966a commit c5bb1ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/ArrayPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ public static function set(&$aSource, $mIndex, $mValue)
$mCurSource = &$aSource;

foreach ($aPath as $mPath) {
if (array_key_exists($mPath, (array) $mCurSource)) {
$mCurSource = &$mCurSource[$mPath];
} else {
if (!array_key_exists($mPath, (array) $mCurSource)) {
$mCurSource[$mPath] = array();
}

$mCurSource = &$mCurSource[$mPath];
}

$mCurSource[$mPath] = $mValue;
$mCurSource = $mValue;

return $mValue;
}
Expand Down
15 changes: 15 additions & 0 deletions test/src/ArrayPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ public function testSet()
$this->assertEquals(17, ArrayPath::set($aSource, 'l1-2/l2-2/l3-3', 17));
$this->assertEquals($aExpected, $aSource);
}

public function testSetWithoutParent()
{
$aSource = array();

$aExpected = array(
'l1-2' => array(
'l2-2' => array(
'l3-3' => 17
)
)
);
$this->assertEquals(17, ArrayPath::set($aSource, 'l1-2/l2-2/l3-3', 17));
$this->assertEquals($aExpected, $aSource);
}

public function testRemove()
{
Expand Down

0 comments on commit c5bb1ed

Please sign in to comment.