From d9652df835216ce4a0fc975f9491d5179477d47f Mon Sep 17 00:00:00 2001 From: Migo Date: Tue, 18 Apr 2017 18:01:19 +0200 Subject: [PATCH] fix(ProductSuggest): fix suggestion parameters Supports also fuzzy level for suggest endpoint Closes #310 --- .../Products/ProductsSuggestRequest.php | 24 ++++++----- .../Products/ProductsSuggestRequestTest.php | 43 +++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/Request/Products/ProductsSuggestRequest.php b/src/Request/Products/ProductsSuggestRequest.php index d0368f9ad5..ee503850da 100644 --- a/src/Request/Products/ProductsSuggestRequest.php +++ b/src/Request/Products/ProductsSuggestRequest.php @@ -79,13 +79,19 @@ protected function getProjectionAction() } /** - * @param bool $fuzzy + * @param bool|int $level * @return $this */ - public function fuzzy($fuzzy) + public function fuzzy($level) { - if (!is_null($fuzzy)) { - $this->addParamObject(new Parameter('fuzzy', (bool)$fuzzy)); + if (!is_bool($level)) { + $level = min(2, max(0, (int)$level)); + } + $fuzzy = (bool)$level; + $this->addParamObject(new Parameter('fuzzy', $fuzzy)); + + if (!is_bool($level) && $fuzzy) { + $this->addParamObject(new Parameter('fuzzyLevel', $level)); } return $this; @@ -142,14 +148,12 @@ public function setSearchKeywords(LocalizedString $searchKeywords) public function getParamString() { $params = []; - foreach ($this->searchKeywords->toArray() as $lang => $keyword) { - $params[] = 'searchKeywords.' . $lang . '=' . urlencode($keyword); + foreach ($this->getSearchKeywords()->toArray() as $lang => $keyword) { + $param = new Parameter('searchKeywords.' . $lang, $keyword); + $params[$param->getId()] = $param; } - $params = array_merge($params, array_keys($this->params)); - sort($params); - $params = implode('&', $params); - + $params = $this->convertToString(array_merge($this->params, $params)); return (!empty($params) ? '?' . $params : ''); } diff --git a/tests/unit/Request/Products/ProductsSuggestRequestTest.php b/tests/unit/Request/Products/ProductsSuggestRequestTest.php index 2a2ccaaea9..aa2886d196 100644 --- a/tests/unit/Request/Products/ProductsSuggestRequestTest.php +++ b/tests/unit/Request/Products/ProductsSuggestRequestTest.php @@ -46,6 +46,49 @@ public function testMapEmptyResult() $this->assertEmpty($result->toArray()); } + public function fuzzyProvider() + { + return [ + [true, 'fuzzy=true'], + [false, 'fuzzy=false'], + [-1, 'fuzzy=false'], + [ 0, 'fuzzy=false'], + [ 1, 'fuzzy=true&fuzzyLevel=1'], + [ 2, 'fuzzy=true&fuzzyLevel=2'], + [ 3, 'fuzzy=true&fuzzyLevel=2'], + ['1', 'fuzzy=true&fuzzyLevel=1'], + ]; + } + + /** + * @dataProvider fuzzyProvider + */ + public function testFuzzyLevel($level, $expected) + { + /** + * @var ProductsSuggestRequest $request + */ + $request = $this->getRequest(static::PRODUCT_SUGGEST_REQUEST); + $request->fuzzy($level); + $httpRequest = $request->httpRequest(); + + $this->assertStringStartsWith('product-projections/suggest', (string)$httpRequest->getUri()); + $this->assertContains($expected, (string)$httpRequest->getUri()); + } + + public function testFuzzyKeyword() + { + $request = $this->getRequest(static::PRODUCT_SUGGEST_REQUEST); + /** + * @var ProductsSuggestRequest $request + */ + $request->fuzzy(true)->addKeyword('en', 'test'); + $httpRequest = $request->httpRequest(); + + $this->assertStringStartsWith('product-projections/suggest', (string)$httpRequest->getUri()); + $this->assertContains('fuzzy=true&searchKeywords.en=test', (string)$httpRequest->getUri()); + } + public function testAddKeyword() { $request = ProductsSuggestRequest::of();