Skip to content

Commit ee7aa09

Browse files
author
Oleksii Korshenko
committed
MAGETWO-58929: Functional Improvements for Magento 2.1.2
2 parents ef44f3f + 9334b98 commit ee7aa09

File tree

5 files changed

+110
-64
lines changed

5 files changed

+110
-64
lines changed

app/code/Magento/Backend/Block/Widget/Grid/Column/Renderer/Currency.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
// @codingStandardsIgnoreFile
8-
97
namespace Magento\Backend\Block\Widget\Grid\Column\Renderer;
108

119
/**
1210
* Backend grid item renderer currency
13-
*
14-
* @author Magento Core Team <core@magentocommerce.com>
1511
*/
1612
class Currency extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
1713
{
@@ -49,6 +45,11 @@ class Currency extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\Abstra
4945
*/
5046
protected $_localeCurrency;
5147

48+
/**
49+
* @var \Magento\Framework\Pricing\PriceCurrencyInterface
50+
*/
51+
private $priceCurrency;
52+
5253
/**
5354
* @param \Magento\Backend\Block\Context $context
5455
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -76,6 +77,23 @@ public function __construct(
7677
$this->_defaultBaseCurrency = $currencyFactory->create()->load($defaultBaseCurrencyCode);
7778
}
7879

80+
/**
81+
* Get price currency
82+
*
83+
* @return \Magento\Framework\Pricing\PriceCurrencyInterface
84+
*
85+
* @deprecated
86+
*/
87+
private function getPriceCurrency()
88+
{
89+
if ($this->priceCurrency === null) {
90+
$this->priceCurrency = \Magento\Framework\App\ObjectManager::getInstance()->get(
91+
\Magento\Framework\Pricing\PriceCurrencyInterface::class
92+
);
93+
}
94+
return $this->priceCurrency;
95+
}
96+
7997
/**
8098
* Renders grid column
8199
*
@@ -84,15 +102,9 @@ public function __construct(
84102
*/
85103
public function render(\Magento\Framework\DataObject $row)
86104
{
87-
if ($data = (string)$this->_getValue($row)) {
88-
$currency_code = $this->_getCurrencyCode($row);
89-
$data = floatval($data) * $this->_getRate($row);
90-
$sign = (bool)(int)$this->getColumn()->getShowNumberSign() && $data > 0 ? '+' : '';
91-
$data = sprintf("%f", $data);
92-
$data = $this->_localeCurrency->getCurrency($currency_code)->toCurrency($data);
93-
return $sign . $data;
94-
}
95-
return $this->getColumn()->getDefault();
105+
$price = $this->_getValue($row) ? $this->_getValue($row) : $this->getColumn()->getDefault();
106+
$displayPrice = $this->getPriceCurrency()->convertAndFormat($price, false);
107+
return $displayPrice;
96108
}
97109

98110
/**

app/code/Magento/Backend/Test/Unit/Block/Widget/Grid/Column/Renderer/CurrencyTest.php

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,44 +47,49 @@ class CurrencyTest extends \PHPUnit_Framework_TestCase
4747
*/
4848
protected $_row;
4949

50+
/*
51+
* @var \PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
protected $priceCurrencyMock;
54+
5055
protected function setUp()
5156
{
52-
$this->_storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface');
53-
$this->_localeMock = $this->getMock('Magento\Framework\Locale\CurrencyInterface');
54-
$this->_requestMock = $this->getMock('Magento\Framework\App\RequestInterface');
57+
$this->_storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class);
58+
$this->_localeMock = $this->getMock(\Magento\Framework\Locale\CurrencyInterface::class);
59+
$this->_requestMock = $this->getMock(\Magento\Framework\App\RequestInterface::class);
5560

5661
$this->_curLocatorMock = $this->getMock(
57-
'Magento\Directory\Model\Currency\DefaultLocator',
62+
\Magento\Directory\Model\Currency\DefaultLocator::class,
5863
[],
5964
[],
6065
'',
6166
false
6267
);
6368
$this->_columnMock = $this->getMock(
64-
'Magento\Backend\Block\Widget\Grid\Column',
69+
\Magento\Backend\Block\Widget\Grid\Column::class,
6570
['getIndex'],
6671
[],
6772
'',
6873
false
6974
);
7075
$this->_columnMock->expects($this->any())->method('getIndex')->will($this->returnValue('columnIndex'));
7176

72-
$this->_currencyMock = $this->getMock('Magento\Directory\Model\Currency', [], [], '', false);
77+
$this->_currencyMock = $this->getMock(\Magento\Directory\Model\Currency::class, [], [], '', false);
7378
$this->_currencyMock->expects($this->any())->method('load')->will($this->returnSelf());
7479
$currencyFactoryMock = $this->getMock(
75-
'Magento\Directory\Model\CurrencyFactory',
80+
\Magento\Directory\Model\CurrencyFactory::class,
7681
['create'],
7782
[],
7883
'',
7984
false
8085
);
8186
$currencyFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_currencyMock));
8287

83-
$this->_row = new \Magento\Framework\DataObject(['columnIndex' => '10']);
88+
$this->_row = new \Magento\Framework\DataObject(['columnIndex' => '$10.00']);
8489

8590
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
8691
$this->_blockCurrency = $helper->getObject(
87-
'Magento\Backend\Block\Widget\Grid\Column\Renderer\Currency',
92+
\Magento\Backend\Block\Widget\Grid\Column\Renderer\Currency::class,
8893
[
8994
'storeManager' => $this->_storeManagerMock,
9095
'localeCurrency' => $this->_localeMock,
@@ -95,6 +100,21 @@ protected function setUp()
95100
);
96101

97102
$this->_blockCurrency->setColumn($this->_columnMock);
103+
104+
$this->priceCurrencyMock = $this->getMockForAbstractClass(
105+
\Magento\Framework\Pricing\PriceCurrencyInterface::class,
106+
[],
107+
'',
108+
false,
109+
true,
110+
true,
111+
['convertAndFormat']
112+
);
113+
$helper->setBackwardCompatibleProperty(
114+
$this->_blockCurrency,
115+
'priceCurrency',
116+
$this->priceCurrencyMock
117+
);
98118
}
99119

100120
protected function tearDown()
@@ -113,46 +133,11 @@ protected function tearDown()
113133
*/
114134
public function testRenderWithDefaultCurrency()
115135
{
116-
$this->_currencyMock->expects(
117-
$this->once()
118-
)->method(
119-
'getRate'
120-
)->with(
121-
'defaultCurrency'
122-
)->will(
123-
$this->returnValue(1.5)
124-
);
125-
126-
$this->_curLocatorMock->expects(
127-
$this->any()
128-
)->method(
129-
'getDefaultCurrency'
130-
)->with(
131-
$this->_requestMock
132-
)->will(
133-
$this->returnValue('defaultCurrency')
134-
);
135-
136-
$currLocaleMock = $this->getMock('Zend_Currency', [], [], '', false);
137-
$currLocaleMock->expects(
138-
$this->once()
139-
)->method(
140-
'toCurrency'
141-
)->with(
142-
15.0000
143-
)->will(
144-
$this->returnValue('15USD')
145-
);
146-
$this->_localeMock->expects(
147-
$this->once()
148-
)->method(
149-
'getCurrency'
150-
)->with(
151-
'defaultCurrency'
152-
)->will(
153-
$this->returnValue($currLocaleMock)
154-
);
136+
$this->priceCurrencyMock->expects($this->once())
137+
->method('convertAndFormat')
138+
->with('$10.00', false)
139+
->willReturn('$10.00');
155140

156-
$this->assertEquals('15USD', $this->_blockCurrency->render($this->_row));
141+
$this->assertEquals('$10.00', $this->_blockCurrency->render($this->_row));
157142
}
158143
}

app/code/Magento/Eav/Model/Entity/Attribute.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
7676
*/
7777
protected $dateTimeFormatter;
7878

79+
/**
80+
* @var \Magento\Framework\Intl\NumberFormatterFactory
81+
*/
82+
private $numberFormatterFactory;
83+
7984
/**
8085
* @param \Magento\Framework\Model\Context $context
8186
* @param \Magento\Framework\Registry $registry
@@ -216,6 +221,23 @@ public function loadEntityAttributeIdBySet()
216221
return $this;
217222
}
218223

224+
/**
225+
* Get number formatter factory
226+
*
227+
* @return \Magento\Framework\Intl\NumberFormatterFactory
228+
*
229+
* @deprecated
230+
*/
231+
private function getNumberFormatterFactory()
232+
{
233+
if ($this->numberFormatterFactory === null) {
234+
$this->numberFormatterFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
235+
\Magento\Framework\Intl\NumberFormatterFactory::class
236+
);
237+
}
238+
return $this->numberFormatterFactory;
239+
}
240+
219241
/**
220242
* Prepare data for save
221243
*
@@ -256,7 +278,8 @@ public function beforeSave()
256278
$hasDefaultValue = (string)$defaultValue != '';
257279

258280
if ($this->getBackendType() == 'decimal' && $hasDefaultValue) {
259-
$numberFormatter = new \NumberFormatter($this->_localeResolver->getLocale(), \NumberFormatter::DECIMAL);
281+
$numberFormatter = $this->getNumberFormatterFactory()
282+
->create($this->_localeResolver->getLocale(), \NumberFormatter::DECIMAL);
260283
$defaultValue = $numberFormatter->parse($defaultValue);
261284
if ($defaultValue === false) {
262285
throw new LocalizedException(__('Invalid default decimal value'));

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"colinmollenhour/php-redis-session-abstract": "1.1",
3737
"colinmollenhour/cache-backend-redis": "1.9",
3838
"colinmollenhour/cache-backend-file": "1.4",
39-
"composer/composer": "1.0.0-beta1",
39+
"composer/composer": "<=1.0.0-beta1",
4040
"monolog/monolog": "1.16.0",
4141
"oyejorge/less.php": "~1.7.0",
4242
"pelago/emogrifier": "0.1.1",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Intl;
7+
8+
/**
9+
* Class NumberFormatterFactory
10+
* @package Magento\Framework
11+
*/
12+
class NumberFormatterFactory
13+
{
14+
/**
15+
* Factory method for \NumberFormatter
16+
*
17+
* @param string $locale
18+
* @param int $style
19+
* @param string $pattern
20+
* @return \NumberFormatter
21+
*/
22+
public function create($locale, $style, $pattern = null)
23+
{
24+
return new \NumberFormatter($locale, $style, $pattern);
25+
}
26+
}

0 commit comments

Comments
 (0)