diff --git a/src/ArrayDiffMultidimensional.php b/src/ArrayDiffMultidimensional.php index b370674..f72b5d0 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 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 + * @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; } diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index b7d5729..bc19128 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -6,76 +6,133 @@ 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 BeAwareThatThisArrayAreEquals() + { + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + ], + ]; + + $old = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => uniqid(), + ], + ]; + + $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