-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add array_set and Arr::set functionality
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace ArrayHelpers; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class ArraySetTest extends TestCase | ||
{ | ||
public function testWillSetSingleValue() | ||
{ | ||
$expected = ['rolling' => 'stones']; | ||
|
||
$initial = []; | ||
Arr::set($initial, 'rolling', 'stones'); | ||
$this->assertEquals($expected, $initial); | ||
|
||
$initial = []; | ||
array_set($initial, 'rolling', 'stones'); | ||
$this->assertEquals($expected, $initial); | ||
} | ||
|
||
public function testWillOverwriteExistingValue() | ||
{ | ||
$initial = ['rolling' => 'stones']; | ||
Arr::set($initial, 'rolling', 'thunder'); | ||
$this->assertEquals( | ||
['rolling' => 'thunder'], | ||
$initial | ||
); | ||
} | ||
|
||
public function testWillSetUsingDotNotation() | ||
{ | ||
$initial = []; | ||
Arr::set($initial, "i.can't.get.no", 'satisfaction'); | ||
$this->assertEquals( | ||
[ | ||
'i' => [ | ||
"can't" => [ | ||
'get' => [ | ||
'no' => 'satisfaction', | ||
], | ||
], | ||
], | ||
], | ||
$initial | ||
); | ||
} | ||
|
||
public function testWillOnlyPartiallyOverwriteUsingDotNotation() | ||
{ | ||
$initial = [ | ||
'i' => [ | ||
"can't" => [ | ||
'live' => [ | ||
'in' => [ | ||
'a' => 'living room', | ||
] | ||
] | ||
] | ||
] | ||
]; | ||
Arr::set($initial, "i.can't.get.no", 'satisfaction'); | ||
$this->assertEquals( | ||
[ | ||
'i' => [ | ||
"can't" => [ | ||
'live' => [ | ||
'in' => [ | ||
'a' => 'living room', | ||
], | ||
], | ||
'get' => [ | ||
'no' => 'satisfaction', | ||
], | ||
], | ||
], | ||
], | ||
$initial | ||
); | ||
} | ||
} |