From 5bd0fe3b7c22df2c63b50c9f452666d64bb4862c Mon Sep 17 00:00:00 2001 From: Yu Tang Date: Tue, 28 Apr 2015 08:46:28 -0500 Subject: [PATCH] MAGETWO-32756: [GITHUB] Access to the currency code and symbol from price templates #941 --- app/code/Magento/Directory/Model/Currency.php | 10 ++++ .../Magento/Directory/Model/PriceCurrency.php | 10 ++++ .../Test/Unit/Model/CurrencyTest.php | 55 +++++++++++++++++++ .../Test/Unit/Model/PriceCurrencyTest.php | 12 ++++ .../Pricing/PriceCurrencyInterface.php | 7 +++ .../Framework/Pricing/Render/Amount.php | 8 +++ .../Pricing/Render/AmountRenderInterface.php | 5 ++ .../Pricing/Test/Unit/Render/AmountTest.php | 9 +++ 8 files changed, 116 insertions(+) create mode 100644 app/code/Magento/Directory/Test/Unit/Model/CurrencyTest.php diff --git a/app/code/Magento/Directory/Model/Currency.php b/app/code/Magento/Directory/Model/Currency.php index 9e7d2b9abef18..31ab3bf266dd2 100644 --- a/app/code/Magento/Directory/Model/Currency.php +++ b/app/code/Magento/Directory/Model/Currency.php @@ -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 */ diff --git a/app/code/Magento/Directory/Model/PriceCurrency.php b/app/code/Magento/Directory/Model/PriceCurrency.php index 3aa697742a416..9cf7dabd2de74 100644 --- a/app/code/Magento/Directory/Model/PriceCurrency.php +++ b/app/code/Magento/Directory/Model/PriceCurrency.php @@ -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 * diff --git a/app/code/Magento/Directory/Test/Unit/Model/CurrencyTest.php b/app/code/Magento/Directory/Test/Unit/Model/CurrencyTest.php new file mode 100644 index 0000000000000..33a312880adc3 --- /dev/null +++ b/app/code/Magento/Directory/Test/Unit/Model/CurrencyTest.php @@ -0,0 +1,55 @@ +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()); + } +} diff --git a/app/code/Magento/Directory/Test/Unit/Model/PriceCurrencyTest.php b/app/code/Magento/Directory/Test/Unit/Model/PriceCurrencyTest.php index 205a63e7b0c33..b934435151a04 100644 --- a/app/code/Magento/Directory/Test/Unit/Model/PriceCurrencyTest.php +++ b/app/code/Magento/Directory/Test/Unit/Model/PriceCurrencyTest.php @@ -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') diff --git a/lib/internal/Magento/Framework/Pricing/PriceCurrencyInterface.php b/lib/internal/Magento/Framework/Pricing/PriceCurrencyInterface.php index 9a0c8b2925b02..980a8ff079c9b 100644 --- a/lib/internal/Magento/Framework/Pricing/PriceCurrencyInterface.php +++ b/lib/internal/Magento/Framework/Pricing/PriceCurrencyInterface.php @@ -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); } diff --git a/lib/internal/Magento/Framework/Pricing/Render/Amount.php b/lib/internal/Magento/Framework/Pricing/Render/Amount.php index f58510aaf55fc..a17c6d08af4c5 100644 --- a/lib/internal/Magento/Framework/Pricing/Render/Amount.php +++ b/lib/internal/Magento/Framework/Pricing/Render/Amount.php @@ -142,6 +142,14 @@ public function getDisplayCurrencyCode() return $this->priceCurrency->getCurrency()->getCurrencyCode(); } + /** + * @return string + */ + public function getDisplayCurrencySymbol() + { + return $this->priceCurrency->getCurrencySymbol(); + } + /** * @return bool */ diff --git a/lib/internal/Magento/Framework/Pricing/Render/AmountRenderInterface.php b/lib/internal/Magento/Framework/Pricing/Render/AmountRenderInterface.php index 66d0dc84a1698..3779c69dd31ca 100644 --- a/lib/internal/Magento/Framework/Pricing/Render/AmountRenderInterface.php +++ b/lib/internal/Magento/Framework/Pricing/Render/AmountRenderInterface.php @@ -50,6 +50,11 @@ public function getPrice(); */ public function getDisplayCurrencyCode(); + /** + * @return string + */ + public function getDisplayCurrencySymbol(); + /** * @return string */ diff --git a/lib/internal/Magento/Framework/Pricing/Test/Unit/Render/AmountTest.php b/lib/internal/Magento/Framework/Pricing/Test/Unit/Render/AmountTest.php index 552cee4846656..fbf8d4f834f2f 100644 --- a/lib/internal/Magento/Framework/Pricing/Test/Unit/Render/AmountTest.php +++ b/lib/internal/Magento/Framework/Pricing/Test/Unit/Render/AmountTest.php @@ -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() */