Skip to content

Commit

Permalink
Added tests for ArrayDiff. The test comparing dates and other objects…
Browse files Browse the repository at this point in the history
… currently fail, because the strict comparison is used.
  • Loading branch information
befresh-mweimerskirch committed Jan 12, 2024
1 parent 0b12efe commit 36a8a71
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions tests/Utils/ArrayDiffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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;

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 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 36a8a71

Please sign in to comment.