diff --git a/app/code/Magento/CurrencySymbol/Helper/Data.php b/app/code/Magento/CurrencySymbol/Helper/Data.php deleted file mode 100644 index e3ae5efd76a484db1673e6cbd1322b9dd8a204d7..0000000000000000000000000000000000000000 --- a/app/code/Magento/CurrencySymbol/Helper/Data.php +++ /dev/null @@ -1,56 +0,0 @@ -<?php -/** - * Copyright © 2015 Magento. All rights reserved. - * See COPYING.txt for license details. - */ - -/** - * Currency Symbol helper - * - * @author Magento Core Team <core@magentocommerce.com> - */ -namespace Magento\CurrencySymbol\Helper; - -class Data extends \Magento\Framework\App\Helper\AbstractHelper -{ - /** - * @var \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory - */ - protected $_symbolFactory; - - /** - * @param \Magento\Framework\App\Helper\Context $context - * @param \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory $symbolFactory - */ - public function __construct( - \Magento\Framework\App\Helper\Context $context, - \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory $symbolFactory - ) { - $this->_symbolFactory = $symbolFactory; - parent::__construct( - $context - ); - } - - /** - * Get currency display options - * - * @param string $baseCode - * @return array - */ - public function getCurrencyOptions($baseCode) - { - $currencyOptions = []; - $currencySymbol = $this->_symbolFactory->create(); - if ($currencySymbol) { - $customCurrencySymbol = $currencySymbol->getCurrencySymbol($baseCode); - - if ($customCurrencySymbol) { - $currencyOptions['symbol'] = $customCurrencySymbol; - $currencyOptions['display'] = \Magento\Framework\Currency::USE_SYMBOL; - } - } - - return $currencyOptions; - } -} diff --git a/app/code/Magento/CurrencySymbol/Model/Observer.php b/app/code/Magento/CurrencySymbol/Model/Observer.php index e769c3d81503f60ed7d2ddc9a23a94b2f77c7844..3e55902031ee3cda6ad4e28794472692c574bddd 100644 --- a/app/code/Magento/CurrencySymbol/Model/Observer.php +++ b/app/code/Magento/CurrencySymbol/Model/Observer.php @@ -6,26 +6,24 @@ /** * Currency Symbol Observer - * - * @author Magento Core Team <core@magentocommerce.com> */ namespace Magento\CurrencySymbol\Model; +use Magento\Framework\Locale\Currency; + class Observer { /** - * Currency symbol data - * - * @var \Magento\CurrencySymbol\Helper\Data + * @var \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory */ - protected $_currencySymbolData = null; + protected $symbolFactory; /** - * @param \Magento\CurrencySymbol\Helper\Data $currencySymbolData + * @param \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory */ - public function __construct(\Magento\CurrencySymbol\Helper\Data $currencySymbolData) + public function __construct(\Magento\CurrencySymbol\Model\System\CurrencysymbolFactory $symbolFactory) { - $this->_currencySymbolData = $currencySymbolData; + $this->symbolFactory = $symbolFactory; } /** @@ -39,8 +37,28 @@ class Observer { $baseCode = $observer->getEvent()->getBaseCode(); $currencyOptions = $observer->getEvent()->getCurrencyOptions(); - $currencyOptions->setData($this->_currencySymbolData->getCurrencyOptions($baseCode)); + $currencyOptions->setData($this->getCurrencyOptions($baseCode)); return $this; } + + /** + * Get currency display options + * + * @param string $baseCode + * @return array + */ + protected function getCurrencyOptions($baseCode) + { + $currencyOptions = []; + if ($baseCode) { + $customCurrencySymbol = $this->symbolFactory->create()->getCurrencySymbol($baseCode); + if ($customCurrencySymbol) { + $currencyOptions[Currency::CURRENCY_OPTION_SYMBOL] = $customCurrencySymbol; + $currencyOptions[Currency::CURRENCY_OPTION_DISPLAY] = \Magento\Framework\Currency::USE_SYMBOL; + } + } + + return $currencyOptions; + } } diff --git a/app/code/Magento/CurrencySymbol/Test/Unit/Model/ObserverTest.php b/app/code/Magento/CurrencySymbol/Test/Unit/Model/ObserverTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6c5c69594dfe085875fae1c1da2261c150c251bf --- /dev/null +++ b/app/code/Magento/CurrencySymbol/Test/Unit/Model/ObserverTest.php @@ -0,0 +1,115 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +/** + * Test for \Magento\CurrencySymbol\Model\Observer + */ +class ObserverTest extends PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\CurrencySymbol\Model\Observer + */ + private $observer; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\CurrencySymbol\Model\System\CurrencysymbolFactory $mockSymbolFactory + */ + private $mockSymbolFactory; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\CurrencySymbol\Model\System\Currencysymbol $mockSymbol + */ + private $mockSymbol; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event\Observer $mockEvent + */ + private $mockEventObserver; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event $mockEvent + */ + private $mockEvent; + + public function setUp() + { + $this->mockSymbolFactory = $this->getMock( + 'Magento\CurrencySymbol\Model\System\CurrencysymbolFactory', + ['create'], + [], + '', + false + ); + + $this->mockSymbol = $this->getMock( + 'Magento\CurrencySymbol\Model\System\Currencysymbol', + ['getCurrencySymbol'], + [], + '', + false + ); + + $this->mockEventObserver = $this->getMock( + 'Magento\Framework\Event\Observer', + ['getEvent'], + [], + '', + false + ); + + $this->mockEvent = $this->getMock( + 'Magento\Framework\Event', + ['getBaseCode', 'getCurrencyOptions'], + [], + '', + false + ); + + $this->mockEventObserver->expects($this->any())->method('getEvent')->willReturn($this->mockEvent); + $this->mockSymbolFactory->expects($this->any())->method('create')->willReturn($this->mockSymbol); + + $this->observer = new Magento\CurrencySymbol\Model\Observer($this->mockSymbolFactory); + } + + public function testCurrencyDisplayOptionsEmpty() + { + $sampleCurrencyOptionObject = new \Magento\Framework\Object; + //Return invalid value + $this->mockEvent->expects($this->once())->method('getBaseCode')->willReturn(null); + $this->mockEvent->expects($this->once())->method('getCurrencyOptions')->willReturn($sampleCurrencyOptionObject); + $this->mockSymbol->expects($this->never())->method('getCurrencySymbol')->with(null)->willReturn(null); + + $this->observer->currencyDisplayOptions($this->mockEventObserver); + + // Check if option set is empty + $this->assertEquals([], $sampleCurrencyOptionObject->getData()); + } + + public function testCurrencyDisplayOptions() + { + $sampleCurrencyOptionObject = new \Magento\Framework\Object; + $sampleCurrency = 'USD'; + $sampleCurrencySymbol = '$'; + + $expectedCurrencyOptions = [ + \Magento\Framework\Locale\Currency::CURRENCY_OPTION_SYMBOL => $sampleCurrencySymbol, + \Magento\Framework\Locale\Currency::CURRENCY_OPTION_NAME => \Magento\Framework\Currency::USE_SYMBOL + ]; + + //Return invalid value + $this->mockEvent->expects($this->once())->method('getBaseCode')->willReturn($sampleCurrency); + $this->mockEvent->expects($this->once())->method('getCurrencyOptions')->willReturn($sampleCurrencyOptionObject); + $this->mockSymbol->expects($this->once()) + ->method('getCurrencySymbol') + ->with($sampleCurrency) + ->willReturn($sampleCurrencySymbol); + + $this->observer->currencyDisplayOptions($this->mockEventObserver); + + // Check if option set is empty + $this->assertEquals($expectedCurrencyOptions, $sampleCurrencyOptionObject->getData()); + } +} diff --git a/lib/internal/Magento/Framework/Locale/Currency.php b/lib/internal/Magento/Framework/Locale/Currency.php index f0be6c12172278b8e3a32b86848f296cd606d96a..efca02a754d0a8b2d0a5312ee83ac1986da6b87e 100644 --- a/lib/internal/Magento/Framework/Locale/Currency.php +++ b/lib/internal/Magento/Framework/Locale/Currency.php @@ -11,6 +11,18 @@ class Currency implements \Magento\Framework\Locale\CurrencyInterface * Default currency */ const DEFAULT_CURRENCY = 'USD'; + + /**#@+ + * Currency Options + */ + const CURRENCY_OPTION_SYMBOL = 'symbol'; + + const CURRENCY_OPTION_CURRENCY = 'currency'; + + const CURRENCY_OPTION_NAME = 'name'; + + const CURRENCY_OPTION_DISPLAY = 'name'; + /** * @var array */ @@ -72,9 +84,9 @@ class Currency implements \Magento\Framework\Locale\CurrencyInterface $currencyObject = $this->_currencyFactory->create( ['options' => $this->getDefaultCurrency(), 'locale' => $this->_localeResolver->getLocale()] ); - $options['name'] = $currency; - $options['currency'] = $currency; - $options['symbol'] = $currency; + $options[self::CURRENCY_OPTION_NAME] = $currency; + $options[self::CURRENCY_OPTION_CURRENCY] = $currency; + $options[self::CURRENCY_OPTION_SYMBOL] = $currency; } $options = new \Magento\Framework\Object($options);