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

Commit

Permalink
fix(ProductSuggest): fix suggestion parameters
Browse files Browse the repository at this point in the history
Supports also fuzzy level for suggest endpoint

Closes #310
  • Loading branch information
migo315 authored and Jens Schulze committed Apr 19, 2017
1 parent ad9a9d6 commit 6d4477b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/Request/Products/ProductsSuggestRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 : '');
}

Expand Down
43 changes: 43 additions & 0 deletions tests/unit/Request/Products/ProductsSuggestRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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();
Expand Down

0 comments on commit 6d4477b

Please sign in to comment.