Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 6d4477b

Browse files
migo315Jens Schulze
authored andcommitted
fix(ProductSuggest): fix suggestion parameters
Supports also fuzzy level for suggest endpoint Closes #310
1 parent ad9a9d6 commit 6d4477b

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/Request/Products/ProductsSuggestRequest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,19 @@ protected function getProjectionAction()
7979
}
8080

8181
/**
82-
* @param bool $fuzzy
82+
* @param bool|int $level
8383
* @return $this
8484
*/
85-
public function fuzzy($fuzzy)
85+
public function fuzzy($level)
8686
{
87-
if (!is_null($fuzzy)) {
88-
$this->addParamObject(new Parameter('fuzzy', (bool)$fuzzy));
87+
if (!is_bool($level)) {
88+
$level = min(2, max(0, (int)$level));
89+
}
90+
$fuzzy = (bool)$level;
91+
$this->addParamObject(new Parameter('fuzzy', $fuzzy));
92+
93+
if (!is_bool($level) && $fuzzy) {
94+
$this->addParamObject(new Parameter('fuzzyLevel', $level));
8995
}
9096

9197
return $this;
@@ -142,14 +148,12 @@ public function setSearchKeywords(LocalizedString $searchKeywords)
142148
public function getParamString()
143149
{
144150
$params = [];
145-
foreach ($this->searchKeywords->toArray() as $lang => $keyword) {
146-
$params[] = 'searchKeywords.' . $lang . '=' . urlencode($keyword);
151+
foreach ($this->getSearchKeywords()->toArray() as $lang => $keyword) {
152+
$param = new Parameter('searchKeywords.' . $lang, $keyword);
153+
$params[$param->getId()] = $param;
147154
}
148155

149-
$params = array_merge($params, array_keys($this->params));
150-
sort($params);
151-
$params = implode('&', $params);
152-
156+
$params = $this->convertToString(array_merge($this->params, $params));
153157
return (!empty($params) ? '?' . $params : '');
154158
}
155159

tests/unit/Request/Products/ProductsSuggestRequestTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,49 @@ public function testMapEmptyResult()
4444
$this->assertEmpty($result->toArray());
4545
}
4646

47+
public function fuzzyProvider()
48+
{
49+
return [
50+
[true, 'fuzzy=true'],
51+
[false, 'fuzzy=false'],
52+
[-1, 'fuzzy=false'],
53+
[ 0, 'fuzzy=false'],
54+
[ 1, 'fuzzy=true&fuzzyLevel=1'],
55+
[ 2, 'fuzzy=true&fuzzyLevel=2'],
56+
[ 3, 'fuzzy=true&fuzzyLevel=2'],
57+
['1', 'fuzzy=true&fuzzyLevel=1'],
58+
];
59+
}
60+
61+
/**
62+
* @dataProvider fuzzyProvider
63+
*/
64+
public function testFuzzyLevel($level, $expected)
65+
{
66+
/**
67+
* @var ProductsSuggestRequest $request
68+
*/
69+
$request = $this->getRequest(static::PRODUCT_SUGGEST_REQUEST);
70+
$request->fuzzy($level);
71+
$httpRequest = $request->httpRequest();
72+
73+
$this->assertStringStartsWith('product-projections/suggest', (string)$httpRequest->getUri());
74+
$this->assertContains($expected, (string)$httpRequest->getUri());
75+
}
76+
77+
public function testFuzzyKeyword()
78+
{
79+
$request = $this->getRequest(static::PRODUCT_SUGGEST_REQUEST);
80+
/**
81+
* @var ProductsSuggestRequest $request
82+
*/
83+
$request->fuzzy(true)->addKeyword('en', 'test');
84+
$httpRequest = $request->httpRequest();
85+
86+
$this->assertStringStartsWith('product-projections/suggest', (string)$httpRequest->getUri());
87+
$this->assertContains('fuzzy=true&searchKeywords.en=test', (string)$httpRequest->getUri());
88+
}
89+
4790
public function testAddKeyword()
4891
{
4992
$request = ProductsSuggestRequest::of();

0 commit comments

Comments
 (0)