mirror of
https://github.com/Neur0toxine/pock.git
synced 2025-01-30 20:41:44 +03:00
fix for internal array comparison logic
This commit is contained in:
parent
f455ba52d5
commit
bbfe55bc94
@ -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]) {
|
||||
|
@ -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]) {
|
||||
|
75
tests/src/Comparator/RecursiveArrayComparatorTest.php
Normal file
75
tests/src/Comparator/RecursiveArrayComparatorTest.php
Normal 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));
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user