Skip to content

Commit

Permalink
MAGETWO-32756: [GITHUB] Access to the currency code and symbol from p…
Browse files Browse the repository at this point in the history
…rice templates #941
  • Loading branch information
Yu Tang committed Apr 28, 2015
1 parent 786ed7b commit 5bd0fe3
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/code/Magento/Directory/Model/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ public function formatTxt($price, $options = [])
return $this->_localeCurrency->getCurrency($this->getCode())->toCurrency($price, $options);
}

/**
* Return currency symbol for current locale and currency code
*
* @return string
*/
public function getCurrencySymbol()
{
return $this->_localeCurrency->getCurrency($this->getCode())->getSymbol();
}

/**
* @return string
*/
Expand Down
10 changes: 10 additions & 0 deletions app/code/Magento/Directory/Model/PriceCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ public function getCurrency($scope = null, $currency = null)
return $currentCurrency;
}

/**
* @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
* @param \Magento\Framework\Model\AbstractModel|string|null $currency
* @return string
*/
public function getCurrencySymbol($scope = null, $currency = null)
{
return $this->getCurrency($scope, $currency)->getCurrencySymbol();
}

/**
* Get store model
*
Expand Down
55 changes: 55 additions & 0 deletions app/code/Magento/Directory/Test/Unit/Model/CurrencyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Directory\Test\Unit\Model;

use Magento\Directory\Model\Currency;

class CurrencyTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Currency
*/
protected $currency;

protected $currencyCode = 'USD';

/**
* @var \Magento\Framework\Locale\CurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $localeCurrencyMock;

public function setUp()
{
$this->localeCurrencyMock = $this->getMock('\Magento\Framework\Locale\CurrencyInterface');

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->currency = $objectManager->getObject('Magento\Directory\Model\Currency', [
'localeCurrency' => $this->localeCurrencyMock,
'data' => [
'currency_code' => $this->currencyCode,
]
]);
}

public function testGetCurrencySymbol()
{
$currencySymbol = '$';

$currencyMock = $this->getMockBuilder('\Magento\Framework\Currency')
->disableOriginalConstructor()
->getMock();
$currencyMock->expects($this->once())
->method('getSymbol')
->willReturn($currencySymbol);

$this->localeCurrencyMock->expects($this->once())
->method('getCurrency')
->with($this->currencyCode)
->willReturn($currencyMock);
$this->assertEquals($currencySymbol, $this->currency->getCurrencySymbol());
}
}
12 changes: 12 additions & 0 deletions app/code/Magento/Directory/Test/Unit/Model/PriceCurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ public function testConvertAndFormat()
));
}

public function testGetCurrencySymbol()
{
$storeId = 2;
$currencySymbol = '$';

$currencyMock = $this->getCurrentCurrencyMock();
$currencyMock->expects($this->once())
->method('getCurrencySymbol')
->willReturn($currencySymbol);
$this->assertEquals($currencySymbol, $this->priceCurrency->getCurrencySymbol($storeId, $currencyMock));
}

protected function getCurrentCurrencyMock()
{
$currency = $this->getMockBuilder('Magento\Directory\Model\Currency')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,11 @@ public function round($price);
* @return \Magento\Framework\Model\AbstractModel
*/
public function getCurrency($scope = null, $currency = null);

/**
* @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
* @param \Magento\Framework\Model\AbstractModel|string|null $currency
* @return string
*/
public function getCurrencySymbol($scope = null, $currency = null);
}
8 changes: 8 additions & 0 deletions lib/internal/Magento/Framework/Pricing/Render/Amount.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ public function getDisplayCurrencyCode()
return $this->priceCurrency->getCurrency()->getCurrencyCode();
}

/**
* @return string
*/
public function getDisplayCurrencySymbol()
{
return $this->priceCurrency->getCurrencySymbol();
}

/**
* @return bool
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function getPrice();
*/
public function getDisplayCurrencyCode();

/**
* @return string
*/
public function getDisplayCurrencySymbol();

/**
* @return string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ public function testFormatCurrency()
$this->assertEquals($result, $this->model->formatCurrency($amount, $includeContainer, $precision));
}

public function testGetDisplayCurrencySymbol()
{
$currencySymbol = '$';
$this->priceCurrency->expects($this->once())
->method('getCurrencySymbol')
->willReturn($currencySymbol);
$this->assertEquals($currencySymbol, $this->model->getDisplayCurrencySymbol());
}

/**
* Test case for getAdjustmentRenders method through toHtml()
*/
Expand Down

0 comments on commit 5bd0fe3

Please sign in to comment.