equal check improvement and tests

This commit is contained in:
Yurii
2026-03-04 15:56:44 +02:00
parent ca136225cb
commit 61677e34e0
2 changed files with 214 additions and 3 deletions
+13 -3
View File
@@ -53,15 +53,25 @@ class Util
}
foreach ($v1 as $i => $itemValue) {
if (is_object($itemValue) && is_object($v2[$i])) {
if (!self::areValuesEqual(AttributeType::JSON_OBJECT, $itemValue, $v2[$i])) {
$otherValue = $v2[$i];
if (is_object($itemValue) && is_object($otherValue)) {
if (!self::areValuesEqual(AttributeType::JSON_OBJECT, $itemValue, $otherValue)) {
return false;
}
continue;
}
if ($itemValue !== $v2[$i]) {
if (is_array($itemValue) && is_array($otherValue)) {
if (!self::areValuesEqual(AttributeType::JSON_ARRAY, $itemValue, $otherValue)) {
return false;
}
continue;
}
if ($itemValue !== $otherValue) {
return false;
}
}
+201
View File
@@ -0,0 +1,201 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\ORM;
use Espo\ORM\Type\AttributeType;
use Espo\ORM\Util;
use PHPUnit\Framework\TestCase;
class UtilTest extends TestCase
{
public function testAreValuesEqualScalar(): void
{
$this->assertTrue(
Util::areValuesEqual(
AttributeType::VARCHAR,
'a1',
'a1'
)
);
$this->assertTrue(
Util::areValuesEqual(
AttributeType::VARCHAR,
1,
1
)
);
$this->assertTrue(
Util::areValuesEqual(
AttributeType::VARCHAR,
1.1,
1.1
)
);
$this->assertFalse(
Util::areValuesEqual(
AttributeType::VARCHAR,
'a1',
'a2'
)
);
}
public function testAreValuesEqualArray(): void
{
$this->assertTrue(
Util::areValuesEqual(
AttributeType::JSON_ARRAY,
['a1', 'a2'],
['a1', 'a2'],
)
);
$this->assertFalse(
Util::areValuesEqual(
AttributeType::JSON_ARRAY,
['a1', 'a2'],
['a2', 'a1'],
)
);
$this->assertTrue(
Util::areValuesEqual(
AttributeType::JSON_ARRAY,
['a1', 'a2'],
['a1', 'a2'],
isUnordered: true,
)
);
$this->assertTrue(
Util::areValuesEqual(
AttributeType::JSON_ARRAY,
['a1', 'a2'],
['a2', 'a1'],
isUnordered: true,
)
);
$this->assertTrue(
Util::areValuesEqual(
AttributeType::JSON_ARRAY,
['a1', 'a2'],
['a2', 'a1'],
isUnordered: true,
)
);
$this->assertTrue(
Util::areValuesEqual(
AttributeType::JSON_ARRAY,
[['a1'], ['a2']],
[['a1'], ['a2']],
)
);
$this->assertTrue(
Util::areValuesEqual(
AttributeType::JSON_ARRAY,
[(object) ['a1' => 1], (object) ['a2' => 1]],
[(object) ['a1' => 1], (object) ['a2' => 1]],
)
);
$this->assertFalse(
Util::areValuesEqual(
AttributeType::JSON_ARRAY,
[(object) ['a1' => 1], (object) ['a2' => 1]],
[(object) ['a1' => 2], (object) ['a2' => 1]],
)
);
$this->assertTrue(
Util::areValuesEqual(
AttributeType::JSON_ARRAY,
[[(object) ['a1' => 1], (object) ['a2' => 1]]],
[[(object) ['a1' => 1], (object) ['a2' => 1]]],
)
);
$this->assertFalse(
Util::areValuesEqual(
AttributeType::JSON_ARRAY,
[[(object) ['a1' => 1], (object) ['a2' => 1]]],
[[(object) ['a1' => 2], (object) ['a2' => 1]]],
)
);
}
public function testAreValuesEqualObject(): void
{
$this->assertTrue(
Util::areValuesEqual(
AttributeType::JSON_OBJECT,
(object) ['a1' => 1],
(object) ['a1' => 1],
)
);
$this->assertFalse(
Util::areValuesEqual(
AttributeType::JSON_OBJECT,
(object) ['a1' => 1],
(object) ['a1' => 2],
)
);
$this->assertFalse(
Util::areValuesEqual(
AttributeType::JSON_OBJECT,
(object) ['a1' => 1],
(object) ['a2' => 2],
)
);
$this->assertTrue(
Util::areValuesEqual(
AttributeType::JSON_OBJECT,
(object) ['a1' => 1, 'a2' => 2],
(object) ['a2' => 2, 'a1' => 1],
)
);
$this->assertTrue(
Util::areValuesEqual(
AttributeType::JSON_OBJECT,
(object) ['a1' => [1, 2], 'a2' => [3, 4]],
(object) ['a1' => [1, 2], 'a2' => [3, 4]],
)
);
}
}