-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
array_diff_multidimensional helper function (#21)
* Create helpers.php * Update composer.json * Create HelperFunctionTest.php * Update HelperFunctionTest.php * Update README.md * Update README.md Co-authored-by: William Desportes <[email protected]> Co-authored-by: William Desportes <[email protected]>
- Loading branch information
1 parent
51d68c0
commit d5afb18
Showing
4 changed files
with
121 additions
and
3 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,21 @@ | ||
<?php | ||
|
||
use Rogervila\ArrayDiffMultidimensional; | ||
|
||
if (!function_exists('array_diff_multidimensional')) { | ||
|
||
/** | ||
* Returns an array with the differences between $array1 and $array2 | ||
* $strict variable defines if comparison must be strict or not | ||
* | ||
* @param array $array1 | ||
* @param array $array2 | ||
* @param bool $strict | ||
* | ||
* @return array | ||
*/ | ||
function array_diff_multidimensional($array1, $array2, $strict = true) | ||
{ | ||
return ArrayDiffMultidimensional::compare($array1, $array2, $strict); | ||
} | ||
} |
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 Rogervila\Test; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class HelperFunctionTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_returns_an_array() | ||
{ | ||
$this->assertTrue(is_array(array_diff_multidimensional([], []))); | ||
} | ||
|
||
/** @test */ | ||
public function it_calls_compare_method() | ||
{ | ||
$new = [ | ||
'a' => 'b', | ||
'c' => [ | ||
'd' => 'e', | ||
'f' => 'Hello', | ||
], | ||
]; | ||
|
||
$old = [ | ||
'a' => 'b', | ||
'c' => [ | ||
'd' => 'e', | ||
'f' => 'Goodbye', | ||
], | ||
]; | ||
|
||
$this->assertEquals( | ||
[ | ||
'c' => [ | ||
'f' => 'Hello' | ||
], | ||
], | ||
array_diff_multidimensional($new, $old) | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function loose_comparisons() | ||
{ | ||
$new = [ | ||
'a' => 'b', | ||
'c' => 288, | ||
]; | ||
|
||
$old = [ | ||
'a' => 'b', | ||
'c' => '288', | ||
]; | ||
|
||
$this->assertEquals(0, count(array_diff_multidimensional($new, $old, false))); | ||
$this->assertFalse(isset(array_diff_multidimensional($new, $old, false)['c'])); | ||
} | ||
|
||
/** @test */ | ||
public function strict_comparisons() | ||
{ | ||
$new = [ | ||
'a' => 'b', | ||
'c' => 288, | ||
]; | ||
|
||
$old = [ | ||
'a' => 'b', | ||
'c' => '288', | ||
]; | ||
|
||
$this->assertEquals(1, count(array_diff_multidimensional($new, $old))); | ||
$this->assertTrue(isset(array_diff_multidimensional($new, $old)['c'])); | ||
$this->assertEquals(288, array_diff_multidimensional($new, $old)['c']); | ||
|
||
$this->assertEquals(1, count(array_diff_multidimensional($new, $old, true))); | ||
$this->assertTrue(isset(array_diff_multidimensional($new, $old, true)['c'])); | ||
$this->assertEquals(288, array_diff_multidimensional($new, $old, true)['c']); | ||
} | ||
} |