fix for internal array comparison logic

This commit is contained in:
Pavel 2022-04-14 15:43:40 +03:00
parent f455ba52d5
commit bbfe55bc94
4 changed files with 120 additions and 7 deletions

View File

@ -50,8 +50,12 @@ class RecursiveArrayComparator implements ComparatorInterface
}
foreach ($first as $key => $value) {
if (is_array($value) && !self::recursiveCompareArrays($value, $second[$key])) {
return false;
if (is_array($value)) {
if (!is_array($second[$key]) || !self::recursiveCompareArrays($value, $second[$key])) {
return false;
}
continue;
}
if ($value !== $second[$key]) {

View File

@ -47,11 +47,12 @@ class RecursiveLtrArrayComparator extends RecursiveArrayComparator
}
foreach ($needle as $key => $value) {
if (
is_array($value) &&
(!is_array($haystack[$key]) || !self::recursiveCompareArrays($value, $haystack[$key]))
) {
return false;
if (is_array($value)) {
if (!is_array($haystack[$key]) || !self::recursiveCompareArrays($value, $haystack[$key])) {
return false;
}
continue;
}
if ($value !== $haystack[$key]) {

View File

@ -0,0 +1,75 @@
<?php
/**
* PHP version 7.3
*
* @category RecursiveArrayComparatorTest
* @package Pock\Tests\Comparator
*/
namespace Pock\Tests\Comparator;
use PHPUnit\Framework\TestCase;
use Pock\Comparator\ComparatorLocator;
use Pock\Comparator\RecursiveArrayComparator;
/**
* Class RecursiveArrayComparatorTest
*
* @category RecursiveArrayComparatorTest
* @package Pock\Tests\Comparator
*/
class RecursiveArrayComparatorTest extends TestCase
{
public function testMatches(): void
{
$needle = [
'filter' => [
'createdAtFrom' => '2020-01-01 00:00:00',
'createdAtTo' => '2021-08-01 00:00:00',
],
'test' => ''
];
$haystack = [
'filter' => [
'createdAtFrom' => '2020-01-01 00:00:00',
'createdAtTo' => '2021-08-01 00:00:00',
],
'test' => ''
];
self::assertTrue(ComparatorLocator::get(RecursiveArrayComparator::class)->compare($needle, $haystack));
}
public function testNotMatches(): void
{
$needle = [
'filter' => [
'createdAtFrom' => '2020-01-01 00:00:00',
'createdAtTo' => '2021-08-01 00:00:00',
],
'test2' => [1]
];
$haystack = [
'filter' => [
'createdAtFrom' => '2020-01-01 00:00:00',
'createdAtTo' => '2021-08-01 00:00:00',
],
'test2' => 1
];
self::assertFalse(ComparatorLocator::get(RecursiveArrayComparator::class)->compare($needle, $haystack));
}
public function testNotMatchesKeyPositions(): void
{
$needle = [
'source' => json_decode('{"medium":"tiktok","source":"Test Ad","campaign":"Test Campaign"}', true)
];
$haystack = [
'source' => json_decode('{"source":"Test Ad","medium":"tiktok","campaign":"Test Campaign"}', true)
];
self::assertTrue(ComparatorLocator::get(RecursiveArrayComparator::class)->compare($needle, $haystack));
}
}

View File

@ -39,4 +39,37 @@ class RecursiveArrayLtrComparatorTest extends TestCase
self::assertTrue(ComparatorLocator::get(RecursiveLtrArrayComparator::class)->compare($needle, $haystack));
}
public function testNotMatches(): void
{
$needle = [
'filter' => [
'createdAtFrom' => '2020-01-01 00:00:00',
'createdAtTo' => '2021-08-01 00:00:00',
],
'test2' => [1]
];
$haystack = [
'filter' => [
'createdAtFrom' => '2020-01-01 00:00:00',
'createdAtTo' => '2021-08-01 00:00:00',
],
'test2' => 1,
'test' => ''
];
self::assertFalse(ComparatorLocator::get(RecursiveLtrArrayComparator::class)->compare($needle, $haystack));
}
public function testNotMatchesKeyPositions(): void
{
$needle = [
'source' => json_decode('{"medium":"tiktok","source":"Test Ad","campaign":"Test Campaign"}', true)
];
$haystack = [
'source' => json_decode('{"source":"Test Ad","medium":"tiktok","campaign":"Test Campaign"}', true)
];
self::assertTrue(ComparatorLocator::get(RecursiveLtrArrayComparator::class)->compare($needle, $haystack));
}
}