Skip to content

Commit ba2ae68

Browse files
committed
MCP-288: [Load Cart Section] Replace Zend_Currency component with Intl NumberFormatter
- Use magento json serializer;
1 parent 7af0389 commit ba2ae68

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

app/code/Magento/Directory/Model/Currency.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Locale\Currency as LocaleCurrency;
1313
use Magento\Framework\Locale\ResolverInterface as LocalResolverInterface;
1414
use Magento\Framework\NumberFormatterFactory;
15+
use Magento\Framework\Serialize\Serializer\Json;
1516

1617
/**
1718
* Currency model
@@ -94,6 +95,11 @@ class Currency extends \Magento\Framework\Model\AbstractModel
9495
*/
9596
private $numberFormatterCache;
9697

98+
/**
99+
* @var Json
100+
*/
101+
private $serializer;
102+
97103
/**
98104
* @param \Magento\Framework\Model\Context $context
99105
* @param \Magento\Framework\Registry $registry
@@ -108,6 +114,7 @@ class Currency extends \Magento\Framework\Model\AbstractModel
108114
* @param CurrencyConfig|null $currencyConfig
109115
* @param LocalResolverInterface|null $localeResolver
110116
* @param NumberFormatterFactory|null $numberFormatterFactory
117+
* @param Json|null $serializer
111118
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
112119
*/
113120
public function __construct(
@@ -123,7 +130,8 @@ public function __construct(
123130
array $data = [],
124131
CurrencyConfig $currencyConfig = null,
125132
LocalResolverInterface $localeResolver = null,
126-
\Magento\Framework\NumberFormatterFactory $numberFormatterFactory = null
133+
\Magento\Framework\NumberFormatterFactory $numberFormatterFactory = null,
134+
Json $serializer = null
127135
) {
128136
parent::__construct(
129137
$context,
@@ -140,6 +148,7 @@ public function __construct(
140148
$this->currencyConfig = $currencyConfig ?: ObjectManager::getInstance()->get(CurrencyConfig::class);
141149
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(LocalResolverInterface::class);
142150
$this->numberFormatterFactory = $numberFormatterFactory ?: ObjectManager::getInstance()->get(NumberFormatterFactory::class);
151+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
143152
}
144153

145154
/**
@@ -356,7 +365,7 @@ public function formatTxt($price, $options = [])
356365
*/
357366
$price = sprintf("%F", $price);
358367

359-
if ($this->useNumberFormatter($options)) {
368+
if ($this->canUseNumberFormatter($options)) {
360369
return $this->formatCurrency($price, $options);
361370
}
362371

@@ -369,7 +378,7 @@ public function formatTxt($price, $options = [])
369378
* @param array $options
370379
* @return bool
371380
*/
372-
private function useNumberFormatter(array $options): bool
381+
private function canUseNumberFormatter(array $options): bool
373382
{
374383
$allowedOptions = [
375384
'precision',
@@ -435,7 +444,7 @@ private function formatCurrency(string $price, array $options): string
435444
*/
436445
private function getNumberFormatter(array $options): \Magento\Framework\NumberFormatter
437446
{
438-
$key = 'currency_' . md5($this->localeResolver->getLocale() . serialize($options));
447+
$key = 'currency_' . md5($this->localeResolver->getLocale() . $this->serializer->serialize($options));
439448
if (!isset($this->numberFormatterCache[$key])) {
440449
$this->numberFormatter = $this->numberFormatterFactory->create(
441450
['locale' => $this->localeResolver->getLocale(), 'style' => \NumberFormatter::CURRENCY]

app/code/Magento/Directory/Test/Unit/Model/CurrencyTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Locale\CurrencyInterface;
1212
use Magento\Framework\Locale\ResolverInterface as LocalResolverInterface;
1313
use Magento\Framework\NumberFormatterFactory;
14+
use Magento\Framework\Serialize\Serializer\Json;
1415
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1516
use PHPUnit\Framework\MockObject\MockObject;
1617
use PHPUnit\Framework\TestCase;
@@ -39,6 +40,11 @@ class CurrencyTest extends TestCase
3940
*/
4041
private $numberFormatterFactory;
4142

43+
/**
44+
* @var Json
45+
*/
46+
private $serializer;
47+
4248
protected function setUp(): void
4349
{
4450
$this->localeCurrencyMock = $this->getMockForAbstractClass(CurrencyInterface::class);
@@ -51,6 +57,9 @@ protected function setUp(): void
5157
->disableOriginalConstructor()
5258
->setMethods(['create'])
5359
->getMock();
60+
$this->serializer = $this->getMockBuilder(Json::class)
61+
->disableOriginalConstructor()
62+
->getMock();
5463

5564
$objectManager = new ObjectManager($this);
5665
$this->currency = $objectManager->getObject(
@@ -60,6 +69,7 @@ protected function setUp(): void
6069
'currencyFilterFactory' => $currencyFilterFactory,
6170
'localeResolver' => $this->localeResolver,
6271
'numberFormatterFactory' => $this->numberFormatterFactory,
72+
'serializer' => $this->serializer,
6373
'data' => [
6474
'currency_code' => $this->currencyCode,
6575
]
@@ -97,6 +107,12 @@ public function testGetOutputFormat($expected, $locale): void
97107
->method('create')
98108
->with(['locale' => $locale, 'style' => 2])
99109
->willReturn(new \Magento\Framework\NumberFormatter($locale, 2));
110+
$this->serializer->method('serialize')->willReturnMap(
111+
[
112+
[[], '[]'],
113+
[['display' => 1], '{"display":1}']
114+
]
115+
);
100116
self::assertEquals($expected, $this->currency->getOutputFormat());
101117
}
102118

@@ -138,6 +154,11 @@ public function testFormatTxtWithNumberFormatter(
138154
->method('create')
139155
->with(['locale' => $locale, 'style' => 2])
140156
->willReturn(new \Magento\Framework\NumberFormatter($locale, 2));
157+
$this->serializer->method('serialize')->willReturnMap(
158+
[
159+
[[], '[]']
160+
]
161+
);
141162

142163
self::assertEquals($expected, $this->currency->formatTxt($price, $options));
143164
}
@@ -179,6 +200,11 @@ public function testFormatTxtWithZendCurrency(string $price, array $options, str
179200
->method('getCurrency')
180201
->with($this->currencyCode)
181202
->willReturn(new \Zend_Currency($options, 'en_US'));
203+
$this->serializer->method('serialize')->willReturnMap(
204+
[
205+
[[], '[]']
206+
]
207+
);
182208

183209
self::assertEquals($expected, $this->currency->formatTxt($price, $options));
184210
}

0 commit comments

Comments
 (0)