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

Commit d9652df

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

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
@@ -46,6 +46,49 @@ public function testMapEmptyResult()
4646
$this->assertEmpty($result->toArray());
4747
}
4848

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

0 commit comments

Comments
 (0)