From 37116e554fb8e4c34b8110e1f844450b004e5fa7 Mon Sep 17 00:00:00 2001 From: Szabo Date: Thu, 15 Mar 2018 22:33:27 +0200 Subject: [PATCH 1/5] Add a new valid case to test. If second array has more keys than the first one and all keys in the first array are present in the second the two arrays will be equal. (same as array_diff). --- tests/ArrayCompareTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index b7d5729..309e894 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -78,4 +78,25 @@ public function DetectsChangesOnNestedArrays() $this->assertEquals( count($this->diff->compare($new,$old)), 1 ); $this->assertTrue( isset($this->diff->compare($new,$old)['c']['f']) ); } + + /** @test */ + public function DifferenceFail() + { + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + ], + ]; + + $old = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => uniqid(), + ], + ]; + + $this->assertNotEquals( 1, count($this->diff->compare($new,$old)) ); + } } \ No newline at end of file From 1d38b210a56dfc21d5041d7bd30896484000d03a Mon Sep 17 00:00:00 2001 From: Szabo Date: Thu, 15 Mar 2018 22:34:17 +0200 Subject: [PATCH 2/5] Moved the is_array check at the end of the function. --- src/ArrayDiffMultidimensional.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/ArrayDiffMultidimensional.php b/src/ArrayDiffMultidimensional.php index b370674..4c96b12 100644 --- a/src/ArrayDiffMultidimensional.php +++ b/src/ArrayDiffMultidimensional.php @@ -6,18 +6,25 @@ class ArrayDiffMultidimensional { /** - * Returns an array with the differences between $array1 and $array2 - * - * @param array $aArray1 - * @param array $aArray2 + * Returns an array with the differences between $array1 and $array2. + * Compares array1 against one or more other arrays and returns the values in array1 + * that are not present in any of the other arrays. + * @param mixed $array1 + * @param mixed $array2 * @return array + * @internal param array $aArray1 + * @internal param array $aArray2 */ public static function compare($array1, $array2) { $result = array(); + if (!is_array($array2)) { + return $array1; + } + foreach ($array1 as $key => $value) { - if (!is_array($array2) || !array_key_exists($key, $array2)) { + if (!array_key_exists($key, $array2)) { $result[$key] = $value; continue; } From 8726305989ed12a78095a8ca2fc6cf27e4543246 Mon Sep 17 00:00:00 2001 From: Szabo Date: Mon, 19 Mar 2018 19:22:53 +0200 Subject: [PATCH 3/5] Tests for cases when first or second argument is not array. --- src/ArrayDiffMultidimensional.php | 6 +- tests/ArrayCompareTest.php | 184 ++++++++++++++++++------------ 2 files changed, 113 insertions(+), 77 deletions(-) diff --git a/src/ArrayDiffMultidimensional.php b/src/ArrayDiffMultidimensional.php index 4c96b12..f72b5d0 100644 --- a/src/ArrayDiffMultidimensional.php +++ b/src/ArrayDiffMultidimensional.php @@ -6,9 +6,9 @@ class ArrayDiffMultidimensional { /** - * Returns an array with the differences between $array1 and $array2. - * Compares array1 against one or more other arrays and returns the values in array1 - * that are not present in any of the other arrays. + * Returns an array with items that are present in first array, + * but not in the second one. If second argument is not + * array first array is returned. * @param mixed $array1 * @param mixed $array2 * @return array diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index 309e894..bc19128 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -6,81 +6,81 @@ class ArrayCompareTest extends \PHPUnit_Framework_TestCase { - protected $diff; - - public function setUp() - { - $this->diff = new ArrayDiffMultidimensional(); - } - - /** @test */ - public function returnsAnArray() - { - $this->assertTrue( is_array($this->diff->compare([],[])) ); - } - - /** @test */ - public function DetectsTheDifferenceOnStringValue() - { - $old = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $new = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $this->assertEquals( count($this->diff->compare($new,$old)), 1 ); - $this->assertTrue( isset($this->diff->compare($new,$old)['c']) ); - } - - /** @test */ - public function DetectsChangeFromStringToArray() - { - $new = [ - 'a' => 'b', - 'c' => [ - 'd' => uniqid(), - 'e' => uniqid(), - ], - ]; - - $old = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $this->assertEquals( count($this->diff->compare($new,$old)), 1 ); - $this->assertTrue( is_array($this->diff->compare($new,$old)['c']) ); - } - - /** @test */ - public function DetectsChangesOnNestedArrays() - { - $new = [ - 'a' => 'b', - 'c' => [ - 'd' => 'e', - 'f' => uniqid(), - ], - ]; - - $old = [ - 'a' => 'b', - 'c' => [ - 'd' => 'e', - 'f' => uniqid(), - ], - ]; - - $this->assertEquals( count($this->diff->compare($new,$old)), 1 ); - $this->assertTrue( isset($this->diff->compare($new,$old)['c']['f']) ); - } + protected $diff; + + public function setUp() + { + $this->diff = new ArrayDiffMultidimensional(); + } + + /** @test */ + public function returnsAnArray() + { + $this->assertTrue(is_array($this->diff->compare([], []))); + } + + /** @test */ + public function DetectsTheDifferenceOnStringValue() + { + $old = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $new = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $this->assertEquals(count($this->diff->compare($new, $old)), 1); + $this->assertTrue(isset($this->diff->compare($new, $old)['c'])); + } + + /** @test */ + public function DetectsChangeFromStringToArray() + { + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => uniqid(), + 'e' => uniqid(), + ], + ]; + + $old = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $this->assertEquals(count($this->diff->compare($new, $old)), 1); + $this->assertTrue(is_array($this->diff->compare($new, $old)['c'])); + } + + /** @test */ + public function DetectsChangesOnNestedArrays() + { + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => uniqid(), + ], + ]; + + $old = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => uniqid(), + ], + ]; + + $this->assertEquals(count($this->diff->compare($new, $old)), 1); + $this->assertTrue(isset($this->diff->compare($new, $old)['c']['f'])); + } /** @test */ - public function DifferenceFail() + public function BeAwareThatThisArrayAreEquals() { $new = [ 'a' => 'b', @@ -97,6 +97,42 @@ public function DifferenceFail() ], ]; - $this->assertNotEquals( 1, count($this->diff->compare($new,$old)) ); + $this->assertNotEquals(1, count($this->diff->compare($new, $old))); + } + + + /** @test */ + public function SecondArgumentIsNotArray() + { + $old = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'ff' => [ + 'test' + ] + ], + ]; + + $new = 100; + + $this->assertEquals($old, $this->diff->compare($old, $new)); + } + + /** + * @test + * @expectedException \PHPUnit_Framework_Error + */ + public function FirstArgumentIsNotArray() + { + $old = null; + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + ], + ]; + + $this->assertEquals($new, $this->diff->compare($old, $new)); } } \ No newline at end of file From 7c007dbab983bdc85fd71aae9f273a9bdce8faa9 Mon Sep 17 00:00:00 2001 From: Szabo Date: Mon, 19 Mar 2018 19:50:29 +0200 Subject: [PATCH 4/5] I suspect this test fails for some incompatible versions. --- tests/ArrayCompareTest.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index bc19128..9e9e2dc 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -118,21 +118,4 @@ public function SecondArgumentIsNotArray() $this->assertEquals($old, $this->diff->compare($old, $new)); } - - /** - * @test - * @expectedException \PHPUnit_Framework_Error - */ - public function FirstArgumentIsNotArray() - { - $old = null; - $new = [ - 'a' => 'b', - 'c' => [ - 'd' => 'e', - ], - ]; - - $this->assertEquals($new, $this->diff->compare($old, $new)); - } } \ No newline at end of file From fda840038739baf671b5d9c94710120f968ebed9 Mon Sep 17 00:00:00 2001 From: Szabo Date: Mon, 19 Mar 2018 19:55:34 +0200 Subject: [PATCH 5/5] Nop, is not mine. --- tests/ArrayCompareTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index 9e9e2dc..bc19128 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -118,4 +118,21 @@ public function SecondArgumentIsNotArray() $this->assertEquals($old, $this->diff->compare($old, $new)); } + + /** + * @test + * @expectedException \PHPUnit_Framework_Error + */ + public function FirstArgumentIsNotArray() + { + $old = null; + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + ], + ]; + + $this->assertEquals($new, $this->diff->compare($old, $new)); + } } \ No newline at end of file