Skip to content

Commit

Permalink
Merge 1.x into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI authored Jan 23, 2024
2 parents ef0580b + bc9bf8c commit c149164
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [1.16.1](https://github.com/sonata-project/EntityAuditBundle/compare/1.16.0...1.16.1) - 2024-01-22
### Fixed
- [[#599](https://github.com/sonata-project/EntityAuditBundle/pull/599)] Objects now no longer show as different when their values are the same. This restores some of the old behaviour of the EntityAuditBundle ([@befresh-mweimerskirch](https://github.com/befresh-mweimerskirch))

## [1.16.0](https://github.com/sonata-project/EntityAuditBundle/compare/1.15.0...1.16.0) - 2023-12-04
### Added
- [[#592](https://github.com/sonata-project/EntityAuditBundle/pull/592)] Support for Symfony 7 ([@VincentLanglet](https://github.com/VincentLanglet))
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ parameters:
featureToggles:
skipCheckGenericClasses:
- Symfony\Component\Form\AbstractType
- Symfony\Component\Form\FormBuilderInterface
- Symfony\Component\Form\FormInterface
- Symfony\Component\Form\FormTypeExtensionInterface
- Symfony\Component\Form\FormTypeInterface
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.
* 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;
}

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');
$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);
}
}

0 comments on commit c149164

Please sign in to comment.