Skip to content

Commit 181b563

Browse files
ENGCOM-6441: Code refactor in Catalog ViewModel Breadcrumbs #25920
2 parents ca3a45a + 8389794 commit 181b563

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

app/code/Magento/Catalog/Test/Unit/ViewModel/Product/BreadcrumbsTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Catalog\ViewModel\Product\Breadcrumbs;
1313
use Magento\Framework\App\Config\ScopeConfigInterface;
1414
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Framework\Serialize\Serializer\JsonHexTag;
1516

1617
/**
1718
* Unit test for Magento\Catalog\ViewModel\Product\Breadcrumbs.
@@ -38,6 +39,11 @@ class BreadcrumbsTest extends \PHPUnit\Framework\TestCase
3839
*/
3940
private $objectManager;
4041

42+
/**
43+
* @var JsonHexTag|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $serializer;
46+
4147
/**
4248
* @inheritdoc
4349
*/
@@ -55,12 +61,15 @@ protected function setUp() : void
5561

5662
$escaper = $this->getObjectManager()->getObject(\Magento\Framework\Escaper::class);
5763

64+
$this->serializer = $this->createMock(JsonHexTag::class);
65+
5866
$this->viewModel = $this->getObjectManager()->getObject(
5967
Breadcrumbs::class,
6068
[
6169
'catalogData' => $this->catalogHelper,
6270
'scopeConfig' => $this->scopeConfig,
63-
'escaper' => $escaper
71+
'escaper' => $escaper,
72+
'jsonSerializer' => $this->serializer
6473
]
6574
);
6675
}
@@ -141,6 +150,8 @@ public function testGetJsonConfiguration($product, string $expectedJson) : void
141150
->with('catalog/seo/category_url_suffix', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
142151
->willReturn('."html');
143152

153+
$this->serializer->expects($this->once())->method('serialize')->willReturn($expectedJson);
154+
144155
$this->assertEquals($expectedJson, $this->viewModel->getJsonConfiguration());
145156
}
146157

app/code/Magento/Catalog/ViewModel/Product/Breadcrumbs.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\App\ObjectManager;
1212
use Magento\Framework\DataObject;
1313
use Magento\Framework\Serialize\Serializer\Json;
14+
use Magento\Framework\Serialize\Serializer\JsonHexTag;
1415
use Magento\Framework\View\Element\Block\ArgumentInterface;
1516
use Magento\Framework\Escaper;
1617

@@ -36,24 +37,32 @@ class Breadcrumbs extends DataObject implements ArgumentInterface
3637
*/
3738
private $escaper;
3839

40+
/**
41+
* @var JsonHexTag
42+
*/
43+
private $jsonSerializer;
44+
3945
/**
4046
* @param Data $catalogData
4147
* @param ScopeConfigInterface $scopeConfig
4248
* @param Json|null $json
4349
* @param Escaper|null $escaper
50+
* @param JsonHexTag|null $jsonSerializer
4451
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4552
*/
4653
public function __construct(
4754
Data $catalogData,
4855
ScopeConfigInterface $scopeConfig,
4956
Json $json = null,
50-
Escaper $escaper = null
57+
Escaper $escaper = null,
58+
JsonHexTag $jsonSerializer = null
5159
) {
5260
parent::__construct();
5361

5462
$this->catalogData = $catalogData;
5563
$this->scopeConfig = $scopeConfig;
5664
$this->escaper = $escaper ?: ObjectManager::getInstance()->get(Escaper::class);
65+
$this->jsonSerializer = $jsonSerializer ?: ObjectManager::getInstance()->get(JsonHexTag::class);
5766
}
5867

5968
/**
@@ -101,15 +110,14 @@ public function getProductName(): string
101110
*/
102111
public function getJsonConfigurationHtmlEscaped() : string
103112
{
104-
return json_encode(
113+
return $this->jsonSerializer->serialize(
105114
[
106115
'breadcrumbs' => [
107116
'categoryUrlSuffix' => $this->escaper->escapeHtml($this->getCategoryUrlSuffix()),
108117
'useCategoryPathInUrl' => (int)$this->isCategoryUsedInProductUrl(),
109118
'product' => $this->escaper->escapeHtml($this->getProductName())
110119
]
111-
],
112-
JSON_HEX_TAG
120+
]
113121
);
114122
}
115123

0 commit comments

Comments
 (0)