Skip to content

Commit b8b98c9

Browse files
committed
MAGE-1066 Handle serialize failure to ensure proper data type is returned
1 parent 7391d9a commit b8b98c9

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

Helper/ConfigHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public function getAutocompleteSections($storeId = null)
446446
}
447447

448448
protected function serialize(array $value): string {
449-
return $this->serializer->serialize($value);
449+
return $this->serializer->serialize($value) ?: '';
450450
}
451451

452452
/**

Test/Unit/ConfigHelperTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@
1818
use Magento\Store\Model\StoreManagerInterface;
1919
use PHPUnit\Framework\TestCase;
2020

21+
class ConfigHelperTestable extends ConfigHelper
22+
{
23+
/** expose protected methods for unit testing */
24+
public function serialize(array $value): string
25+
{
26+
return parent::serialize($value);
27+
}
28+
}
29+
2130
class ConfigHelperTest extends TestCase
2231
{
23-
protected ConfigHelper $configHelper;
32+
protected ConfigHelperTestable $configHelper;
2433
protected ScopeConfigInterface $configInterface;
2534
protected WriterInterface $configWriter;
2635
protected StoreManagerInterface $storeManager;
@@ -51,7 +60,7 @@ public function setUp(): void
5160
$this->groupExcludedWebsiteRepository = $this->createMock(GroupExcludedWebsiteRepositoryInterface::class);
5261
$this->cookieHelper = $this->createMock(CookieHelper::class);
5362

54-
$this->configHelper = new ConfigHelper(
63+
$this->configHelper = new ConfigHelperTestable(
5564
$this->configInterface,
5665
$this->configWriter,
5766
$this->storeManager,
@@ -79,4 +88,22 @@ public function testGetIndexPrefixWhenNull() {
7988
$this->configInterface->method('getValue')->willReturn(null);
8089
$this->assertEquals('', $this->configHelper->getIndexPrefix());
8190
}
91+
92+
public function testSerializerReturnsString() {
93+
$this->serializer->method('serialize')->willReturn('{"foo":"bar"}');
94+
$array = [
95+
'foo' => 'bar'
96+
];
97+
$result = $this->configHelper->serialize($array);
98+
$this->assertEquals('{"foo":"bar"}', $result);
99+
}
100+
101+
public function testSerializerFailure() {
102+
$this->serializer->method('serialize')->willReturn(false);
103+
$array = [
104+
'foo' => 'bar'
105+
];
106+
$result = $this->configHelper->serialize($array);
107+
$this->assertEquals('', $result);
108+
}
82109
}

0 commit comments

Comments
 (0)