Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "diff" result for dates and other objects #599

Merged
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
42 changes: 41 additions & 1 deletion src/Utils/ArrayDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function diff($oldData, $newData)
$old = \array_key_exists($field, $oldData) ? $oldData[$field] : null;
$new = \array_key_exists($field, $newData) ? $newData[$field] : null;

if ($old === $new) {
// If the values are objects, we will compare them by their properties.
// This is necessary because the strict comparison operator (===) will return false if the objects are not the same instance.
if ((\is_object($old) && \is_object($new) && $this->compareObjects($old, $new)) || ($old === $new)) {
$row = ['old' => '', 'new' => '', 'same' => $old];
} else {
$row = ['old' => $old, 'new' => $new, 'same' => ''];
Expand All @@ -48,4 +50,42 @@ public function diff($oldData, $newData)

return $diff;
}

/**
* Compare the type and the property values of two objects.
* Return true if they are the same, false otherwise.
* If the type is the same and all properties are the same, this will return true, even if they are not the same instance.
befresh-mweimerskirch marked this conversation as resolved.
Show resolved Hide resolved
* This method is different from comparing two objects using ==,
* because internally the strict comparison operator (===) is used to compare the properties.
*
* @see https://www.php.net/manual/en/language.oop5.object-comparison.php
*/
private function compareObjects(object $object1, object $object2): bool
{
// Check if the objects are of the same type.
if ($object1::class !== $object2::class) {
return false;
}

// Check if all properties are the same.
$obj1Properties = (array) $object1;
$obj2Properties = (array) $object2;
foreach ($obj1Properties as $key => $value) {
if (!\array_key_exists($key, $obj2Properties)) {
return false;
}
if (\is_object($value) && \is_object($obj2Properties[$key])) {
if (!$this->compareObjects($value, $obj2Properties[$key])) {
return false;
}
befresh-mweimerskirch marked this conversation as resolved.
Show resolved Hide resolved

continue;
}
if ($value !== $obj2Properties[$key]) {
return false;
}
}

return true;
}
}
120 changes: 120 additions & 0 deletions tests/Utils/ArrayDiffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\EntityAuditBundle\Tests\Utils;

use PHPUnit\Framework\TestCase;
use SimpleThings\EntityAudit\Utils\ArrayDiff;

final class ArrayDiffTest extends TestCase
{
public function testDiff(): void
{
$diff = new ArrayDiff();
$array1 = ['one' => 'I', 'two' => '2'];
$array2 = ['one' => 'I', 'two' => 'II'];
$expected = ['one' => ['old' => '', 'new' => '', 'same' => 'I'], 'two' => ['old' => '2', 'new' => 'II', 'same' => '']];

$result = $diff->diff($array1, $array2);

static::assertSame($expected, $result);
}

public function testDiffIsCaseSensitive(): void
{
$diff = new ArrayDiff();
$array1 = ['one' => 'I', 'two' => 'ii'];
$array2 = ['one' => 'I', 'two' => 'II'];
$expected = ['one' => ['old' => '', 'new' => '', 'same' => 'I'], 'two' => ['old' => 'ii', 'new' => 'II', 'same' => '']];

$result = $diff->diff($array1, $array2);

static::assertSame($expected, $result);
}

public function testDiffDate(): void
{
$diff = new ArrayDiff();

$dateInstance1 = new \DateTimeImmutable('2014-01-01 00:00:00.000000');
$dateInstance2 = new \DateTimeImmutable('2014-01-01 00:00:00.000000');

$array1 = ['date' => $dateInstance1];
$array2 = ['date' => $dateInstance2];
$expected = ['date' => ['old' => '', 'new' => '', 'same' => $dateInstance1]];

$result = $diff->diff($array1, $array2);

static::assertSame($expected, $result);
}

public function testDiffDateDifferent(): void
{
$diff = new ArrayDiff();

$dateInstance1 = new \DateTimeImmutable('2014-01-01 00:00:00.000000');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, we should also add a test setting different time zones in the compared objects, in order to expose how the comparison behaves with different time zones using the same UTC offset.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add that test case on Monday.

$dateInstance2 = new \DateTimeImmutable('2014-01-02 00:00:00.000000');

$array1 = ['date' => $dateInstance1];
$array2 = ['date' => $dateInstance2];
$expected = ['date' => ['old' => $dateInstance1, 'new' => $dateInstance2, 'same' => '']];

$result = $diff->diff($array1, $array2);

static::assertSame($expected, $result);
}

public function testDiffDateSameButTimezoneDifferent(): void
{
$diff = new ArrayDiff();

$dateInstance1 = new \DateTimeImmutable('2014-01-01 00:00:00.000000', new \DateTimeZone('Europe/Luxembourg'));
$dateInstance2 = new \DateTimeImmutable('2014-01-01 00:00:00.000000', new \DateTimeZone('UTC'));

$array1 = ['date' => $dateInstance1];
$array2 = ['date' => $dateInstance2];
$expected = ['date' => ['old' => $dateInstance1, 'new' => $dateInstance2, 'same' => '']];

$result = $diff->diff($array1, $array2);

static::assertSame($expected, $result);
}

public function testDiffObjectSame(): void
{
$diff = new ArrayDiff();
$object1 = (object) ['one' => 'I', 'two' => 'II'];
$object2 = (object) ['one' => 'I', 'two' => 'II'];
$array1 = ['object' => $object1];
$array2 = ['object' => $object2];
$expected = ['object' => ['old' => '', 'new' => '', 'same' => $object1]];

$result = $diff->diff($array1, $array2);

static::assertSame($expected, $result);
}

public function testDiffObjectDifferent(): void
{
$diff = new ArrayDiff();
$object1 = (object) ['one' => 'I', 'two' => 'ii'];
$object2 = (object) ['one' => 'I', 'two' => 'II'];
$array1 = ['object' => $object1];
$array2 = ['object' => $object2];
$expected = ['object' => ['old' => $object1, 'new' => $object2, 'same' => '']];

$result = $diff->diff($array1, $array2);

static::assertSame($expected, $result);
}
}