From e917b2ec9673ee4db07e2bf64e4d7001e82d4d41 Mon Sep 17 00:00:00 2001
From: Anup Dugar <anup@x.com>
Date: Thu, 25 Jun 2015 21:39:42 -0500
Subject: [PATCH] MAGETWO-38915: Add unit test coverage for
 \Magento\CurrencySymbol\Helper\*

---
 .../Magento/CurrencySymbol/Helper/Data.php    |  56 ---------
 .../Magento/CurrencySymbol/Model/Observer.php |  38 ++++--
 .../Test/Unit/Model/ObserverTest.php          | 115 ++++++++++++++++++
 .../Magento/Framework/Locale/Currency.php     |  18 ++-
 4 files changed, 158 insertions(+), 69 deletions(-)
 delete mode 100644 app/code/Magento/CurrencySymbol/Helper/Data.php
 create mode 100644 app/code/Magento/CurrencySymbol/Test/Unit/Model/ObserverTest.php

diff --git a/app/code/Magento/CurrencySymbol/Helper/Data.php b/app/code/Magento/CurrencySymbol/Helper/Data.php
deleted file mode 100644
index e3ae5efd76a..00000000000
--- 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 e769c3d8150..3e55902031e 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 00000000000..6c5c69594df
--- /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 f0be6c12172..efca02a754d 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);
-- 
GitLab