Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/ArrayDiffMultidimensional.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
201 changes: 129 additions & 72 deletions tests/ArrayCompareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}