Skip to content

Commit 151a317

Browse files
committed
MAGE-986 Factor out local serialize functions from ConfigHelper
1 parent a2cba8c commit 151a317

File tree

2 files changed

+24
-55
lines changed

2 files changed

+24
-55
lines changed

Helper/ConfigHelper.php

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Algolia\AlgoliaSearch\Helper\Configuration\AutocompleteHelper;
77
use Algolia\AlgoliaSearch\Helper\Configuration\InstantSearchHelper;
88
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
9+
use Algolia\AlgoliaSearch\Service\Serializer;
910
use Magento\Cookie\Helper\Cookie as CookieHelper;
1011
use Magento\Customer\Api\GroupExcludedWebsiteRepositoryInterface;
1112
use Magento\Customer\Model\ResourceModel\Group\Collection as GroupCollection;
@@ -14,7 +15,6 @@
1415
use Magento\Framework\App\Filesystem\DirectoryList;
1516
use Magento\Framework\DataObject;
1617
use Magento\Framework\Locale\Currency;
17-
use Magento\Framework\Serialize\SerializerInterface;
1818
use Magento\Store\Model\ScopeInterface;
1919
use Magento\Store\Model\StoreManagerInterface;
2020

@@ -151,7 +151,7 @@ public function __construct(
151151
protected \Magento\Framework\Module\ResourceInterface $moduleResource,
152152
protected \Magento\Framework\App\ProductMetadataInterface $productMetadata,
153153
protected \Magento\Framework\Event\ManagerInterface $eventManager,
154-
protected SerializerInterface $serializer,
154+
protected Serializer $serializer,
155155
protected GroupCollection $groupCollection,
156156
protected GroupExcludedWebsiteRepositoryInterface $groupExcludedWebsiteRepository,
157157
protected CookieHelper $cookieHelper,
@@ -366,28 +366,6 @@ public function isPartialUpdateEnabled($storeId = null)
366366
return $this->configInterface->isSetFlag(self::PARTIAL_UPDATES, ScopeInterface::SCOPE_STORE, $storeId);
367367
}
368368

369-
370-
371-
protected function serialize(array $value): string {
372-
return $this->serializer->serialize($value) ?: '';
373-
}
374-
375-
/**
376-
* @param $value
377-
* @return array|bool|float|int|mixed|string|null
378-
*/
379-
protected function unserialize($value)
380-
{
381-
if (false === $value || null === $value || '' === $value) {
382-
return false;
383-
}
384-
$unserialized = json_decode($value, true);
385-
if (json_last_error() === JSON_ERROR_NONE) {
386-
return $unserialized;
387-
}
388-
return $this->serializer->unserialize($value);
389-
}
390-
391369
/**
392370
* @param $storeId
393371
* @return bool
@@ -918,7 +896,7 @@ public function isAutoPriceIndexingEnabled(?int $storeId = null): bool
918896
*/
919897
public function getCategoryCustomRanking($storeId = null)
920898
{
921-
$attrs = $this->unserialize($this->configInterface->getValue(
899+
$attrs = $this->serializer->unserialize($this->configInterface->getValue(
922900
self::CATEGORY_CUSTOM_RANKING,
923901
ScopeInterface::SCOPE_STORE,
924902
$storeId
@@ -935,7 +913,7 @@ public function getCategoryCustomRanking($storeId = null)
935913
*/
936914
public function getProductCustomRanking($storeId = null)
937915
{
938-
$attrs = $this->unserialize($this->getRawProductCustomRanking($storeId));
916+
$attrs = $this->serializer->unserialize($this->getRawProductCustomRanking($storeId));
939917
if (is_array($attrs)) {
940918
return $attrs;
941919
}
@@ -1137,27 +1115,27 @@ public function getAttributesToRetrieve($groupId)
11371115
*/
11381116
public function getProductAdditionalAttributes($storeId = null)
11391117
{
1140-
$attributes = $this->unserialize($this->configInterface->getValue(
1118+
$attributes = $this->serializer->unserialize($this->configInterface->getValue(
11411119
self::PRODUCT_ATTRIBUTES,
11421120
ScopeInterface::SCOPE_STORE,
11431121
$storeId
11441122
));
11451123

1146-
$facets = $this->unserialize($this->configInterface->getValue(
1124+
$facets = $this->serializer->unserialize($this->configInterface->getValue(
11471125
self::FACETS,
11481126
ScopeInterface::SCOPE_STORE,
11491127
$storeId
11501128
));
11511129
$attributes = $this->addIndexableAttributes($attributes, $facets, '0');
11521130

1153-
$sorts = $this->unserialize($this->configInterface->getValue(
1131+
$sorts = $this->serializer->unserialize($this->configInterface->getValue(
11541132
self::SORTING_INDICES,
11551133
ScopeInterface::SCOPE_STORE,
11561134
$storeId
11571135
));
11581136
$attributes = $this->addIndexableAttributes($attributes, $sorts, '0');
11591137

1160-
$customRankings = $this->unserialize($this->configInterface->getValue(
1138+
$customRankings = $this->serializer->unserialize($this->configInterface->getValue(
11611139
self::PRODUCT_CUSTOM_RANKING,
11621140
ScopeInterface::SCOPE_STORE,
11631141
$storeId
@@ -1210,12 +1188,12 @@ protected function addIndexableAttributes(
12101188
*/
12111189
public function getCategoryAdditionalAttributes($storeId = null)
12121190
{
1213-
$attributes = $this->unserialize($this->configInterface->getValue(
1191+
$attributes = $this->serializer->unserialize($this->configInterface->getValue(
12141192
self::CATEGORY_ATTRIBUTES,
12151193
ScopeInterface::SCOPE_STORE,
12161194
$storeId
12171195
));
1218-
$customRankings = $this->unserialize($this->configInterface->getValue(
1196+
$customRankings = $this->serializer->unserialize($this->configInterface->getValue(
12191197
self::CATEGORY_CUSTOM_RANKING,
12201198
ScopeInterface::SCOPE_STORE,
12211199
$storeId
@@ -1374,7 +1352,7 @@ public function isAnalyticsEnabled($storeId = null)
13741352
public function getNonCastableAttributes($storeId = null)
13751353
{
13761354
$nonCastableAttributes = [];
1377-
$config = $this->unserialize($this->configInterface->getValue(
1355+
$config = $this->serializer->unserialize($this->configInterface->getValue(
13781356
self::NON_CASTABLE_ATTRIBUTES,
13791357
ScopeInterface::SCOPE_STORE,
13801358
$storeId

Test/Unit/Helper/ConfigHelperTest.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Algolia\AlgoliaSearch\Test\Unit\Helper;
44

5+
use Algolia\AlgoliaSearch\Service\Serializer;
6+
use Algolia\AlgoliaSearch\Helper\Configuration\AutocompleteHelper;
7+
use Algolia\AlgoliaSearch\Helper\Configuration\InstantSearchHelper;
58
use Magento\Cookie\Helper\Cookie as CookieHelper;
69
use Magento\Customer\Api\GroupExcludedWebsiteRepositoryInterface;
710
use Magento\Customer\Model\ResourceModel\Group\Collection as GroupCollection;
@@ -13,7 +16,7 @@
1316
use Magento\Framework\Event\ManagerInterface;
1417
use Magento\Framework\Locale\Currency;
1518
use Magento\Framework\Module\ResourceInterface;
16-
use Magento\Framework\Serialize\SerializerInterface;
19+
1720
use Magento\Store\Model\StoreManagerInterface;
1821
use PHPUnit\Framework\TestCase;
1922

@@ -29,10 +32,12 @@ class ConfigHelperTest extends TestCase
2932
protected ResourceInterface $moduleResource;
3033
protected ProductMetadataInterface $productMetadata;
3134
protected ManagerInterface $eventManager;
32-
protected SerializerInterface $serializer;
35+
protected Serializer $serializer;
3336
protected GroupCollection $groupCollection;
3437
protected GroupExcludedWebsiteRepositoryInterface $groupExcludedWebsiteRepository;
3538
protected CookieHelper $cookieHelper;
39+
protected AutocompleteHelper $autocompleteHelper;
40+
protected InstantSearchHelper $instantSearchHelper;
3641

3742
protected function setUp(): void
3843
{
@@ -45,10 +50,12 @@ protected function setUp(): void
4550
$this->moduleResource = $this->createMock(ResourceInterface::class);
4651
$this->productMetadata = $this->createMock(ProductMetadataInterface::class);
4752
$this->eventManager = $this->createMock(ManagerInterface::class);
48-
$this->serializer = $this->createMock(SerializerInterface::class);
53+
$this->serializer = $this->createMock(Serializer::class);
4954
$this->groupCollection = $this->createMock(GroupCollection::class);
5055
$this->groupExcludedWebsiteRepository = $this->createMock(GroupExcludedWebsiteRepositoryInterface::class);
5156
$this->cookieHelper = $this->createMock(CookieHelper::class);
57+
$this->autocompleteHelper = $this->createMock(AutocompleteHelper::class);
58+
$this->instantSearchHelper = $this->createMock(InstantSearchHelper::class);
5259

5360
$this->configHelper = new ConfigHelperTestable(
5461
$this->configInterface,
@@ -63,7 +70,9 @@ protected function setUp(): void
6370
$this->serializer,
6471
$this->groupCollection,
6572
$this->groupExcludedWebsiteRepository,
66-
$this->cookieHelper
73+
$this->cookieHelper,
74+
$this->autocompleteHelper,
75+
$this->instantSearchHelper
6776
);
6877
}
6978

@@ -78,22 +87,4 @@ public function testGetIndexPrefixWhenNull() {
7887
$this->configInterface->method('getValue')->willReturn(null);
7988
$this->assertEquals('', $this->configHelper->getIndexPrefix());
8089
}
81-
82-
public function testSerializerReturnsString() {
83-
$this->serializer->method('serialize')->willReturn('{"foo":"bar"}');
84-
$array = [
85-
'foo' => 'bar'
86-
];
87-
$result = $this->configHelper->serialize($array);
88-
$this->assertEquals('{"foo":"bar"}', $result);
89-
}
90-
91-
public function testSerializerFailure() {
92-
$this->serializer->method('serialize')->willReturn(false);
93-
$array = [
94-
'foo' => 'bar'
95-
];
96-
$result = $this->configHelper->serialize($array);
97-
$this->assertEquals('', $result);
98-
}
9990
}

0 commit comments

Comments
 (0)