forked from Smile-SA/elasticsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Smile-SA#3447 from rbayet/feat-thesaurus-limit-rew…
…rites-caching [Thesaurus] Additional PHPUnit tests + Conditional caching
- Loading branch information
Showing
9 changed files
with
2,239 additions
and
6 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/module-elasticsuite-thesaurus/Config/ThesaurusCacheConfig.php
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteThesaurus | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteThesaurus\Config; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface; | ||
|
||
/** | ||
* Thesaurus cache configuration helper. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteThesaurus | ||
* @author Richard Bayet <richard.bayet@smile.fr> | ||
*/ | ||
class ThesaurusCacheConfig | ||
{ | ||
/** @var string */ | ||
const ALWAYS_CACHE_RESULTS_XML_PATH = 'smile_elasticsuite_thesaurus_settings/cache/always'; | ||
|
||
/** @var string */ | ||
const MIN_REWRITES_FOR_CACHING_XML_PATH = 'smile_elasticsuite_thesaurus_settings/cache/min_rewites'; | ||
|
||
/** | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ScopeConfigInterface $scopeConfig Scope config interface. | ||
*/ | ||
public function __construct(ScopeConfigInterface $scopeConfig) | ||
{ | ||
$this->scopeConfig = $scopeConfig; | ||
} | ||
|
||
/** | ||
* Returns true if it is allowed by the config to store in cache the results of the thesaurus rules computation. | ||
* | ||
* @param ContainerConfigurationInterface $config Container configuration. | ||
* @param int $rewritesCount Number of rewrites/alternative queries. | ||
* | ||
* @return bool | ||
*/ | ||
public function isCacheStorageAllowed(ContainerConfigurationInterface $config, $rewritesCount) | ||
{ | ||
$alwaysCache = $this->scopeConfig->isSetFlag( | ||
self::ALWAYS_CACHE_RESULTS_XML_PATH, | ||
ScopeInterface::SCOPE_STORES, | ||
$config->getStoreId() | ||
); | ||
|
||
if (false === $alwaysCache) { | ||
$minRewritesForCaching = $this->scopeConfig->getValue( | ||
self::MIN_REWRITES_FOR_CACHING_XML_PATH, | ||
ScopeInterface::SCOPE_STORES, | ||
$config->getStoreId() | ||
); | ||
|
||
return ($rewritesCount >= $minRewritesForCaching); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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
131 changes: 131 additions & 0 deletions
131
src/module-elasticsuite-thesaurus/Test/Unit/Config/ThesaurusCacheConfigTest.php
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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteThesaurus | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Smile\ElasticsuiteThesaurus\Test\Unit\Config; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Store\Model\ScopeInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface; | ||
use Smile\ElasticsuiteThesaurus\Config\ThesaurusCacheConfig; | ||
|
||
/** | ||
* Thesaurus Cache Config helper unit tests. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteThesaurus | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
*/ | ||
class ThesaurusCacheConfigTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* Test the cache storage limitation behavior. | ||
* @dataProvider cacheStorageLimitationDataProvider | ||
* | ||
* @param array $isSetFlagReturnsMap Map of return results for method 'isSetFlag'. | ||
* @param array $getValueReturnsMap Map of return results for method 'getValue'. | ||
* @param int $storeId Store Id. | ||
* @param int $rewritesCount Number of rewrites/alternative queries. | ||
* @param bool $expectedCacheStorageAllowed Expected cache storage allowed result. | ||
*/ | ||
public function testCacheStorageLimitation( | ||
$isSetFlagReturnsMap, | ||
$getValueReturnsMap, | ||
$storeId, | ||
$rewritesCount, | ||
$expectedCacheStorageAllowed | ||
) { | ||
$containerConfigMock = $this->getContainerConfigurationInterfaceMock(); | ||
$containerConfigMock->method('getStoreId')->willReturn($storeId); | ||
|
||
$scopeConfigMock = $this->getScopeConfigInterfaceMock(); | ||
$scopeConfigMock->method('isSetFlag')->willReturnMap($isSetFlagReturnsMap); | ||
$scopeConfigMock->method('getValue')->willReturnMap($getValueReturnsMap); | ||
|
||
$thesaurusCacheConfig = new ThesaurusCacheConfig($scopeConfigMock); | ||
$this->assertEquals( | ||
$expectedCacheStorageAllowed, | ||
$thesaurusCacheConfig->isCacheStorageAllowed($containerConfigMock, $rewritesCount) | ||
); | ||
} | ||
|
||
/** | ||
* Data provider for testCacheStorageLimitation. | ||
* | ||
* @return array | ||
*/ | ||
public function cacheStorageLimitationDataProvider() | ||
{ | ||
$isSetFlagReturnsMap = [ | ||
[ThesaurusCacheConfig::ALWAYS_CACHE_RESULTS_XML_PATH, ScopeInterface::SCOPE_STORES, 1, true], | ||
[ThesaurusCacheConfig::ALWAYS_CACHE_RESULTS_XML_PATH, ScopeInterface::SCOPE_STORES, 2, false], | ||
[ThesaurusCacheConfig::ALWAYS_CACHE_RESULTS_XML_PATH, ScopeInterface::SCOPE_STORES, 3, false], | ||
]; | ||
$getValueReturnsMap = [ | ||
[ThesaurusCacheConfig::MIN_REWRITES_FOR_CACHING_XML_PATH, ScopeInterface::SCOPE_STORES, 1, 10], | ||
[ThesaurusCacheConfig::MIN_REWRITES_FOR_CACHING_XML_PATH, ScopeInterface::SCOPE_STORES, 2, 0], | ||
[ThesaurusCacheConfig::MIN_REWRITES_FOR_CACHING_XML_PATH, ScopeInterface::SCOPE_STORES, 3, 10], | ||
]; | ||
|
||
return [ | ||
/* | ||
* [isSetFlagReturnsMap, getValueReturnsMap, storeId, rewritesCount, expectedCacheStorageAllowed] | ||
*/ | ||
// StoreId 1. | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 1, 0, true], | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 1, 9, true], | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 1, 10, true], | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 1, 11, true], | ||
// StoreId 2. | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 2, 0, true], | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 2, 9, true], | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 2, 10, true], | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 2, 11, true], | ||
// StoreId 3. | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 3, 0, false], | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 3, 9, false], | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 3, 10, true], | ||
[$isSetFlagReturnsMap, $getValueReturnsMap, 3, 11, true], | ||
]; | ||
} | ||
|
||
/** | ||
* Get Container configuration mock. | ||
* | ||
* @return MockObject|ContainerConfigurationInterface | ||
*/ | ||
private function getContainerConfigurationInterfaceMock() | ||
{ | ||
$containerConfiguration = $this->getMockBuilder(ContainerConfigurationInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
return $containerConfiguration; | ||
} | ||
|
||
/** | ||
* Get Scope config mock. | ||
* | ||
* @return MockObject|ScopeConfigInterface | ||
*/ | ||
private function getScopeConfigInterfaceMock() | ||
{ | ||
$scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
return $scopeConfig; | ||
} | ||
} |
Oops, something went wrong.