From f88ddc5c32af51d34ccc80a44fa806ea2372549c Mon Sep 17 00:00:00 2001 From: Vadim Galizyanov Date: Fri, 5 May 2023 14:13:59 +0500 Subject: [PATCH] updated tests --- composer.json | 2 +- tests/AbstractTestCase.php | 11 +- tests/FIlter/QueryAndPostFilterTest.php | 1119 ++++++++++++++++++++++- tests/Helpers/Arr.php | 27 +- 4 files changed, 1108 insertions(+), 51 deletions(-) diff --git a/composer.json b/composer.json index 0656262..2362612 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "require": { "php": ">=7.4", "ramsey/collection": "^1.2", - "iqdev/search-dc": "dev-feature_query", + "iqdev/search-dc": "1.2.*", "elasticsearch/elasticsearch": "^8.5", "vlucas/phpdotenv": "^5.4.1" }, diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 783e158..dd3fe12 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -7,10 +7,17 @@ use PHPUnit\Framework\TestCase; abstract class AbstractTestCase extends TestCase { + /** + * Частичное сравнение массивов + * @param array $expected + * @param array $actual массив который должен иметь структуру и значения $expected + * @param string $message + * @return void + */ protected function assertArray(array $expected, array $actual, string $message = '') { - $expectedKeys = Arr::convertingOneDimensional($expected); - $actualKeys = Arr::convertingOneDimensional($actual); + $expectedKeys = Arr::dot($expected); + $actualKeys = Arr::dot($actual); foreach ($expectedKeys as $key => $value) { $this->assertArrayHasKey($key, $actualKeys, $message); if (isset($actualKeys[$key])) { diff --git a/tests/FIlter/QueryAndPostFilterTest.php b/tests/FIlter/QueryAndPostFilterTest.php index 2d3b21f..dcf6fff 100644 --- a/tests/FIlter/QueryAndPostFilterTest.php +++ b/tests/FIlter/QueryAndPostFilterTest.php @@ -41,10 +41,11 @@ class QueryAndPostFilterTest extends AbstractTestCase $result = $handler->handle($q)->result; $expected = [ - 'hits' => [] + 'hits' => [], + 'facets' => [] ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** @@ -74,10 +75,95 @@ class QueryAndPostFilterTest extends AbstractTestCase 'hits' => [ 's1', 's2', - ] + ], + 'facets' => [ + 0 => [ + 'code' => 'brand', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'adidas', + 'count' => 2, + 'active' => true, + ] + ], + 'range' => [], + ] + ], + 1 => [ + 'code' => 'color', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'green', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'red', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [], + ] + ], + 2 => [ + 'code' => 'size', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => '46', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => '47', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [], + ] + ], + 3 => [ + 'code' => 'price', + 'label' => null, + 'type' => 'range', + 'items' => [ + 'list' => [], + 'range' => [ + 0 => [ + 'label' => null, + 'count' => 2, + 'active' => true, + 'fullRange' => [ + 'min' => 100.0, + 'max' => 101.0, + ], + 'activeRange' => [ + 'min' => 100.0, + 'max' => 101.0, + ] + ] + ] + ] + ] + ], ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** @@ -124,10 +210,125 @@ class QueryAndPostFilterTest extends AbstractTestCase 's4', 'h1', 'h2', + ], + 'facets' => [ + 0 => [ + 'code' => 'brand', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'adidas', + 'count' => 2, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'nike', + 'count' => 3, + 'active' => true, + ] + ], + 'range' => [], + ] + ], + 1 => [ + 'code' => 'color', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'green', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'red', + 'count' => 1, + 'active' => true, + ], + 2 => [ + 'label' => null, + 'value' => 'white', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [], + ] + ], + 2 => [ + 'code' => 'size', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => '43', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => '46', + 'count' => 0, + 'active' => false, + ], + 2 => [ + 'label' => null, + 'value' => '47', + 'count' => 0, + 'active' => false, + ], + 3 => [ + 'label' => null, + 'value' => 'xl', + 'count' => 1, + 'active' => true, + ], + 4 => [ + 'label' => null, + 'value' => 'xxl', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [], + ], + ], + 3 => [ + 'code' => 'price', + 'label' => null, + 'type' => 'range', + 'items' => [ + 'list' => [], + 'range' => [ + 0 => [ + 'label' => null, + 'count' => 3, + 'active' => true, + 'fullRange' => [ + 'min' => 100.0, + 'max' => 105.0, + ], + 'activeRange' => [ + 'min' => 103.0, + 'max' => 105.0, + ] + ] + ] + ] + ] ] ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** @@ -167,10 +368,107 @@ class QueryAndPostFilterTest extends AbstractTestCase $expected = [ 'hits' => [ 'h2', + ], + 'facets' => [ + 0 => [ + 'code' => 'brand', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'nike', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [], + ] + ], + 1 => [ + 'code' => 'color', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'green', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'red', + 'count' => 1, + 'active' => true, + ], + 2 => [ + 'label' => null, + 'value' => 'white', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [], + ] + ], + 2 => [ + 'code' => 'size', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => '43', + 'count' => 0, + 'active' => false, + ], + 1 => [ + 'label' => null, + 'value' => 'xl', + 'count' => 0, + 'active' => false, + ], + 2 => [ + 'label' => null, + 'value' => 'xxl', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [], + ] + ], + 3 => [ + 'code' => 'price', + 'label' => null, + 'type' => 'range', + 'items' => [ + 'list' => [], + 'range' => [ + 0 => [ + 'label' => null, + 'count' => 1, + 'active' => true, + 'fullRange' => [ + 'min' => 103.0, + 'max' => 105.0, + ], + 'activeRange' => [ + 'min' => 105.0, + 'max' => 105.0, + ] + ] + ] + ] + ] ] ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** @@ -225,17 +523,102 @@ class QueryAndPostFilterTest extends AbstractTestCase $expected = [ 'hits' => [ 'h2', + ], + 'facets' => [ + 0 => [ + 'code' => 'brand', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'nike', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'rebook', + 'count' => 2, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 1 => [ + 'code' => 'color', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'white', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 2 => [ + 'code' => 'size', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'xl', + 'count' => 0, + 'active' => false, + ], + 1 => [ + 'label' => null, + 'value' => 'xxl', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 3 => [ + 'code' => 'price', + 'label' => null, + 'type' => 'range', + 'items' => [ + 'list' => [], + 'range' => [ + 0 => [ + 'label' => null, + 'count' => 1, + 'active' => true, + 'fullRange' => [ + 'min' => 105.0, + 'max' => 107.0, + ], + 'activeRange' => [ + 'min' => 105.0, + 'max' => 105.0, + ] + ] + ] + ] + ] ] ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** * query по сущ бренду и post_filter по бренду и цвету * @return void */ - public function testQueryExistingBrandAndColorAndPostFilterExistingBrandAndColor() + public function testQueryExistingBrandAndPostFilterExistingBrandAndColor() { $criteria = new Criteria(); @@ -256,22 +639,6 @@ class QueryAndPostFilterTest extends AbstractTestCase ->setFilterType(FilterType::query()); $criteria->filters()->add($filterCollectionBrand); - $filterCollectionColor = new FilterGroupCollection([ - new Filter( - new Field('color'), - new FilterOperator(FilterOperator::EQ), - new FilterKeyword('white') - ), - new Filter( - new Field('color'), - new FilterOperator(FilterOperator::EQ), - new FilterKeyword('blue') - ), - ]); - $filterCollectionColor - ->setLogicalType(LogicOperator::or()) - ->setFilterType(FilterType::query()); - $criteria->filters()->add($filterCollectionColor); $filterCollectionBrand = new FilterGroupCollection([ new Filter( @@ -286,7 +653,7 @@ class QueryAndPostFilterTest extends AbstractTestCase new Filter( new Field('color'), new FilterOperator(FilterOperator::EQ), - new FilterKeyword('white') + new FilterKeyword('red') ), ]); $criteria->filters()->add($filterCollectionColor); @@ -298,11 +665,126 @@ class QueryAndPostFilterTest extends AbstractTestCase $expected = [ 'hits' => [ - 'h2', + 'h1', + ], + 'facets' => [ + 0 => [ + 'code' => 'brand', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'nike', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'rebook', + 'count' => 0, + 'active' => false, + ] + ], + 'range' => [] + ] + ], + 1 => [ + 'code' => 'color', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'blue', + 'count' => 0, + 'active' => false, + ], + 1 => [ + 'label' => null, + 'value' => 'green', + 'count' => 1, + 'active' => true, + ], + 2 => [ + 'label' => null, + 'value' => 'red', + 'count' => 1, + 'active' => true, + ], + 3 => [ + 'label' => null, + 'value' => 'white', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 2 => [ + 'code' => 'size', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => '43', + 'count' => 0, + 'active' => false, + ], + 1 => [ + 'label' => null, + 'value' => '47', + 'count' => 0, + 'active' => false, + ], + 2 => [ + 'label' => null, + 'value' => 'xl', + 'count' => 1, + 'active' => true, + ], + 3 => [ + 'label' => null, + 'value' => 'xxl', + 'count' => 0, + 'active' => false, + ] + ], + 'range' => [] + ] + ], + 3 => [ + 'code' => 'price', + 'label' => null, + 'type' => 'range', + 'items' => [ + 'list' => [], + 'range' => [ + 0 => [ + 'label' => null, + 'count' => 1, + 'active' => true, + 'fullRange' => [ + 'min' => 102.0, + 'max' => 107.0, + ], + 'activeRange' => [ + 'min' => 104.0, + 'max' => 104.0, + ] + ] + ] + ] + ] ] ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** @@ -329,10 +811,11 @@ class QueryAndPostFilterTest extends AbstractTestCase $result = $handler->handle($q)->result; $expected = [ - 'hits' => [] + 'hits' => [], + 'facets' => [], ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** @@ -363,10 +846,95 @@ class QueryAndPostFilterTest extends AbstractTestCase 'h2', 'h3', 'p1', + ], + 'facets' => [ + 0 => [ + 'code' => 'brand', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'nike', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'rebook', + 'count' => 2, + 'active' => true, + ] + ], + 'range' => [], + ] + ], + 1 => [ + 'code' => 'color', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'white', + 'count' => 3, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 2 => [ + 'code' => 'size', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'xl', + 'count' => 2, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'xxl', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 3 => [ + 'code' => 'price', + 'label' => null, + 'type' => 'range', + 'items' => [ + 'list' => [], + 'range' => [ + 0 => [ + 'label' => null, + 'count' => 3, + 'active' => true, + 'fullRange' => [ + 'min' => 105.0, + 'max' => 107.0, + ], + 'activeRange' => [ + 'min' => 105.0, + 'max' => 107.0, + ] + ] + ] + ] + ] ] ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** @@ -400,10 +968,95 @@ class QueryAndPostFilterTest extends AbstractTestCase 'hits' => [ 'h1', 'h2', + ], + 'facets' => [ + 0 => [ + 'code' => 'brand', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'nike', + 'count' => 2, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 1 => [ + 'code' => 'color', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'red', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'white', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 2 => [ + 'code' => 'size', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'xl', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'xxl', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 3 => [ + 'code' => 'price', + 'label' => null, + 'type' => 'range', + 'items' => [ + 'list' => [], + 'range' => [ + 0 => [ + 'label' => null, + 'count' => 2, + 'active' => true, + 'fullRange' => [ + 'min' => 104.0, + 'max' => 105.0, + ], + 'activeRange' => [ + 'min' => 104.0, + 'max' => 105.0, + ] + ] + ] + ] + ] ] ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** @@ -445,10 +1098,95 @@ class QueryAndPostFilterTest extends AbstractTestCase $expected = [ 'hits' => [ 'h1', + ], + 'facets' => [ + 0 => [ + 'code' => 'brand', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'nike', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 1 => [ + 'code' => 'color', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'red', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'white', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 2 => [ + 'code' => 'size', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'xl', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'xxl', + 'count' => 0, + 'active' => false, + ] + ], + 'range' => [] + ] + ], + 3 => [ + 'code' => 'price', + 'label' => null, + 'type' => 'range', + 'items' => [ + 'list' => [], + 'range' => [ + 0 => [ + 'label' => null, + 'count' => 1, + 'active' => true, + 'fullRange' => [ + 'min' => 104.0, + 'max' => 105.0, + ], + 'activeRange' => [ + 'min' => 104.0, + 'max' => 104.0, + ] + ] + ] + ] + ] ] ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** @@ -513,10 +1251,113 @@ class QueryAndPostFilterTest extends AbstractTestCase $expected = [ 'hits' => [ 's4', + ], + 'facets' => [ + 0 => [ + 'code' => 'brand', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'adidas', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'nike', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 1 => [ + 'code' => 'color', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'green', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'red', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 2 => [ + 'code' => 'size', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => '43', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => '46', + 'count' => 0, + 'active' => false, + ], + 2 => [ + 'label' => null, + 'value' => '47', + 'count' => 0, + 'active' => false, + ], + 3 => [ + 'label' => null, + 'value' => 'xl', + 'count' => 0, + 'active' => false, + ] + ], + 'range' => [] + ] + ], + 3 => [ + 'code' => 'price', + 'label' => null, + 'type' => 'range', + 'items' => [ + 'list' => [], + 'range' => [ + 0 => [ + 'label' => null, + 'count' => 1, + 'active' => true, + 'fullRange' => [ + 'min' => 100.0, + 'max' => 104.0, + ], + 'activeRange' => [ + 'min' => 103.0, + 'max' => 103.0, + ] + ] + ] + ] + ] ] ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** @@ -573,10 +1414,113 @@ class QueryAndPostFilterTest extends AbstractTestCase 'hits' => [ 's4', 'h1', + ], + 'facets' => [ + 0 => [ + 'code' => 'brand', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'adidas', + 'count' => 0, + 'active' => false, + ], + 1 => [ + 'label' => null, + 'value' => 'nike', + 'count' => 2, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 1 => [ + 'code' => 'color', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'green', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'red', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 2 => [ + 'code' => 'size', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => '43', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => '46', + 'count' => 0, + 'active' => false, + ], + 2 => [ + 'label' => null, + 'value' => '47', + 'count' => 0, + 'active' => false, + ], + 3 => [ + 'label' => null, + 'value' => 'xl', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [], + ] + ], + 3 => [ + 'code' => 'price', + 'label' => null, + 'type' => 'range', + 'items' => [ + 'list' => [], + 'range' => [ + 0 => [ + 'label' => null, + 'count' => 2, + 'active' => true, + 'fullRange' => [ + 'min' => 100.0, + 'max' => 104.0, + ], + 'activeRange' => [ + 'min' => 103.0, + 'max' => 104.0, + ] + ] + ] + ] + ] ] ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } /** @@ -619,7 +1563,7 @@ class QueryAndPostFilterTest extends AbstractTestCase new Filter( new Field('price'), new FilterOperator(FilterOperator::GTE), - new FilterNumber(102) + new FilterNumber(101) ), ]); $criteria->filters()->add($filterCollectionPrice); @@ -641,9 +1585,112 @@ class QueryAndPostFilterTest extends AbstractTestCase $expected = [ 'hits' => [ 's4', + ], + 'facets' => [ + 0 => [ + 'code' => 'brand', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'adidas', + 'count' => 0, + 'active' => false, + ], + 1 => [ + 'label' => null, + 'value' => 'nike', + 'count' => 1, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 1 => [ + 'code' => 'color', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => 'green', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => 'red', + 'count' => 2, + 'active' => true, + ] + ], + 'range' => [] + ] + ], + 2 => [ + 'code' => 'size', + 'label' => null, + 'type' => 'list', + 'items' => [ + 'list' => [ + 0 => [ + 'label' => null, + 'value' => '43', + 'count' => 1, + 'active' => true, + ], + 1 => [ + 'label' => null, + 'value' => '46', + 'count' => 0, + 'active' => false, + ], + 2 => [ + 'label' => null, + 'value' => '47', + 'count' => 0, + 'active' => false, + ], + 3 => [ + 'label' => null, + 'value' => 'xl', + 'count' => 0, + 'active' => false, + ] + ], + 'range' => [] + ] + ], + 3 => [ + 'code' => 'price', + 'label' => null, + 'type' => 'range', + 'items' => [ + 'list' => [], + 'range' => [ + 0 => [ + 'label' => null, + 'count' => 1, + 'active' => true, + 'fullRange' => [ + 'min' => 100.0, + 'max' => 104.0, + ], + 'activeRange' => [ + 'min' => 103.0, + 'max' => 103.0, + ], + ] + ] + ] + ] ] ]; - $this->assertEqualsCanonicalizing($expected, FormatData::formatData($result)); + $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result)); } } diff --git a/tests/Helpers/Arr.php b/tests/Helpers/Arr.php index 6164986..54f6cc8 100644 --- a/tests/Helpers/Arr.php +++ b/tests/Helpers/Arr.php @@ -4,21 +4,24 @@ namespace IQDEV\ElasticSearchTests\Helpers; class Arr { - public static function convertingOneDimensional(array $expected, $startKey = null): array + /** + * Flatten a multi-dimensional associative array with dots. + * @param array $array + * @param $prepend + * @return array + */ + public static function dot(array $array, $prepend = ''): array { - $keys = []; - foreach ($expected as $key => $item) { - if ($startKey !== null) { - $key = $startKey.'.'.$key; - } + $results = []; - if (is_array($item)) { - $keys = array_merge($keys, static::convertingOneDimensional($item, $key)); - continue; + foreach ($array as $key => $value) { + if (is_array($value) && ! empty($value)) { + $results = array_merge($results, static::dot($value, $prepend.$key.'.')); + } else { + $results[$prepend.$key] = $value; } - - $keys[$key] = $item; } - return $keys; + + return $results; } } \ No newline at end of file -- GitLab