RecursiveLtrArrayComparator improvement and test

This commit is contained in:
Pavel 2021-05-31 13:25:30 +03:00
parent b4142304ab
commit 2a124cfdd8
2 changed files with 46 additions and 1 deletions

View File

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

View File

@ -0,0 +1,42 @@
<?php
/**
* PHP version 7.3
*
* @category RecursiveArrayLtrComparatorTest
* @package Pock\Tests\Comparator
*/
namespace Pock\Tests\Comparator;
use PHPUnit\Framework\TestCase;
use Pock\Comparator\ComparatorLocator;
use Pock\Comparator\RecursiveLtrArrayComparator;
/**
* Class RecursiveArrayLtrComparatorTest
*
* @category RecursiveArrayLtrComparatorTest
* @package Pock\Tests\Comparator
*/
class RecursiveArrayLtrComparatorTest extends TestCase
{
public function testMatches(): void
{
$needle = [
'filter' => [
'createdAtFrom' => '2020-01-01 00:00:00',
'createdAtTo' => '2021-08-01 00:00:00',
]
];
$haystack = [
'filter' => [
'createdAtFrom' => '2020-01-01 00:00:00',
'createdAtTo' => '2021-08-01 00:00:00',
],
'test' => ''
];
self::assertTrue(ComparatorLocator::get(RecursiveLtrArrayComparator::class)->compare($needle, $haystack));
}
}