-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Unit tests for the suggested terms provider
- Loading branch information
1 parent
b7904ad
commit 75387ca
Showing
10 changed files
with
409 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @package Smile\ElasticsuiteCore | ||
* @author Romain Ruaud <[email protected]> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
|
@@ -24,7 +24,7 @@ | |
* Based on the Term provider but will manipulate it according to configuration. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @package Smile\ElasticsuiteCore | ||
* @author Romain Ruaud <[email protected]> | ||
*/ | ||
class SuggestedTermsProvider | ||
|
@@ -44,6 +44,11 @@ class SuggestedTermsProvider | |
*/ | ||
private $queryStringProviderFactory; | ||
|
||
/** | ||
* @var null|string | ||
*/ | ||
private $queryString = null; | ||
|
||
/** | ||
* @var null | ||
*/ | ||
|
@@ -67,6 +72,8 @@ public function __construct( | |
/** | ||
* List of search terms suggested by the search terms data provider, and reworked according to configuration. | ||
* | ||
* @SuppressWarnings(PHPMD.CyclomaticComplexity) | ||
* | ||
* @return array|string[] | ||
*/ | ||
public function getSuggestedTerms() | ||
|
@@ -84,32 +91,42 @@ function (TermItem $termItem) { | |
|
||
$hasAlreadyStoppedExtending = false; | ||
if ($this->helper->isExtensionStoppedOnMatch()) { | ||
if (array_search(trim($this->getQueryStringProvider()->get()), $terms) !== false) { | ||
$terms = [$this->getQueryStringProvider()->get()]; | ||
if (array_search(trim($this->getQueryString()), $terms) !== false) { | ||
$terms = [$this->getQueryString()]; | ||
$hasAlreadyStoppedExtending = true; | ||
} | ||
} | ||
|
||
if ($this->helper->isExtensionLimited() && !$hasAlreadyStoppedExtending) { | ||
$terms = array_slice($terms, 0, (int) $this->helper->getExtensionSize()); | ||
} | ||
|
||
if ($this->helper->isPreservingBaseQuery() && !$hasAlreadyStoppedExtending) { | ||
array_unshift($terms, $this->getQueryString()); | ||
} | ||
} | ||
|
||
if (empty($terms)) { | ||
$terms = [$this->getQueryStringProvider()->get()]; | ||
$terms = [$this->getQueryString()]; | ||
} | ||
|
||
$this->terms = array_unique($terms); | ||
$this->terms = array_values(array_unique($terms)); | ||
} | ||
|
||
return $this->terms; | ||
} | ||
|
||
/** | ||
* @return \Smile\ElasticsuiteCore\Model\Search\QueryStringProvider | ||
* Retrieve current query string | ||
* | ||
* @return string | ||
*/ | ||
private function getQueryStringProvider() | ||
private function getQueryString() | ||
{ | ||
return $this->queryStringProviderFactory->create(); | ||
if ($this->queryString === null) { | ||
$this->queryString = $this->queryStringProviderFactory->create()->get(); | ||
} | ||
|
||
return $this->queryString; | ||
} | ||
} |
Oops, something went wrong.