Skip to content

Commit 5bd0fe3

Browse files
author
Yu Tang
committedApr 28, 2015
MAGETWO-32756: [GITHUB] Access to the currency code and symbol from price templates #941
1 parent 786ed7b commit 5bd0fe3

File tree

8 files changed

+116
-0
lines changed

8 files changed

+116
-0
lines changed
 

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

+10
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,16 @@ public function formatTxt($price, $options = [])
311311
return $this->_localeCurrency->getCurrency($this->getCode())->toCurrency($price, $options);
312312
}
313313

314+
/**
315+
* Return currency symbol for current locale and currency code
316+
*
317+
* @return string
318+
*/
319+
public function getCurrencySymbol()
320+
{
321+
return $this->_localeCurrency->getCurrency($this->getCode())->getSymbol();
322+
}
323+
314324
/**
315325
* @return string
316326
*/

‎app/code/Magento/Directory/Model/PriceCurrency.php

+10
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ public function getCurrency($scope = null, $currency = null)
117117
return $currentCurrency;
118118
}
119119

120+
/**
121+
* @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
122+
* @param \Magento\Framework\Model\AbstractModel|string|null $currency
123+
* @return string
124+
*/
125+
public function getCurrencySymbol($scope = null, $currency = null)
126+
{
127+
return $this->getCurrency($scope, $currency)->getCurrencySymbol();
128+
}
129+
120130
/**
121131
* Get store model
122132
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Directory\Test\Unit\Model;
8+
9+
use Magento\Directory\Model\Currency;
10+
11+
class CurrencyTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var Currency
15+
*/
16+
protected $currency;
17+
18+
protected $currencyCode = 'USD';
19+
20+
/**
21+
* @var \Magento\Framework\Locale\CurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
protected $localeCurrencyMock;
24+
25+
public function setUp()
26+
{
27+
$this->localeCurrencyMock = $this->getMock('\Magento\Framework\Locale\CurrencyInterface');
28+
29+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
30+
$this->currency = $objectManager->getObject('Magento\Directory\Model\Currency', [
31+
'localeCurrency' => $this->localeCurrencyMock,
32+
'data' => [
33+
'currency_code' => $this->currencyCode,
34+
]
35+
]);
36+
}
37+
38+
public function testGetCurrencySymbol()
39+
{
40+
$currencySymbol = '$';
41+
42+
$currencyMock = $this->getMockBuilder('\Magento\Framework\Currency')
43+
->disableOriginalConstructor()
44+
->getMock();
45+
$currencyMock->expects($this->once())
46+
->method('getSymbol')
47+
->willReturn($currencySymbol);
48+
49+
$this->localeCurrencyMock->expects($this->once())
50+
->method('getCurrency')
51+
->with($this->currencyCode)
52+
->willReturn($currencyMock);
53+
$this->assertEquals($currencySymbol, $this->currency->getCurrencySymbol());
54+
}
55+
}

‎app/code/Magento/Directory/Test/Unit/Model/PriceCurrencyTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ public function testConvertAndFormat()
168168
));
169169
}
170170

171+
public function testGetCurrencySymbol()
172+
{
173+
$storeId = 2;
174+
$currencySymbol = '$';
175+
176+
$currencyMock = $this->getCurrentCurrencyMock();
177+
$currencyMock->expects($this->once())
178+
->method('getCurrencySymbol')
179+
->willReturn($currencySymbol);
180+
$this->assertEquals($currencySymbol, $this->priceCurrency->getCurrencySymbol($storeId, $currencyMock));
181+
}
182+
171183
protected function getCurrentCurrencyMock()
172184
{
173185
$currency = $this->getMockBuilder('Magento\Directory\Model\Currency')

‎lib/internal/Magento/Framework/Pricing/PriceCurrencyInterface.php

+7
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,11 @@ public function round($price);
8989
* @return \Magento\Framework\Model\AbstractModel
9090
*/
9191
public function getCurrency($scope = null, $currency = null);
92+
93+
/**
94+
* @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
95+
* @param \Magento\Framework\Model\AbstractModel|string|null $currency
96+
* @return string
97+
*/
98+
public function getCurrencySymbol($scope = null, $currency = null);
9299
}

‎lib/internal/Magento/Framework/Pricing/Render/Amount.php

+8
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ public function getDisplayCurrencyCode()
142142
return $this->priceCurrency->getCurrency()->getCurrencyCode();
143143
}
144144

145+
/**
146+
* @return string
147+
*/
148+
public function getDisplayCurrencySymbol()
149+
{
150+
return $this->priceCurrency->getCurrencySymbol();
151+
}
152+
145153
/**
146154
* @return bool
147155
*/

‎lib/internal/Magento/Framework/Pricing/Render/AmountRenderInterface.php

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public function getPrice();
5050
*/
5151
public function getDisplayCurrencyCode();
5252

53+
/**
54+
* @return string
55+
*/
56+
public function getDisplayCurrencySymbol();
57+
5358
/**
5459
* @return string
5560
*/

‎lib/internal/Magento/Framework/Pricing/Test/Unit/Render/AmountTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ public function testFormatCurrency()
127127
$this->assertEquals($result, $this->model->formatCurrency($amount, $includeContainer, $precision));
128128
}
129129

130+
public function testGetDisplayCurrencySymbol()
131+
{
132+
$currencySymbol = '$';
133+
$this->priceCurrency->expects($this->once())
134+
->method('getCurrencySymbol')
135+
->willReturn($currencySymbol);
136+
$this->assertEquals($currencySymbol, $this->model->getDisplayCurrencySymbol());
137+
}
138+
130139
/**
131140
* Test case for getAdjustmentRenders method through toHtml()
132141
*/

0 commit comments

Comments
 (0)