Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CurrencyDisplayOptionsTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\CurrencySymbol\Model\System\CurrencysymbolFactory;
9 
13 class CurrencyDisplayOptionsTest extends \PHPUnit\Framework\TestCase
14 {
18  private $observer;
19 
23  private $mockSymbolFactory;
24 
28  private $mockSymbol;
29 
33  private $mockEventObserver;
34 
38  private $mockEvent;
39 
40  protected function setUp()
41  {
42  $this->mockSymbolFactory = $this->createPartialMock(
43  \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory::class,
44  ['create']
45  );
46 
47  $this->mockSymbol = $this->createPartialMock(
48  \Magento\CurrencySymbol\Model\System\Currencysymbol::class,
49  ['getCurrencySymbol']
50  );
51 
52  $this->mockEventObserver = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getEvent']);
53 
54  $this->mockEvent = $this->createPartialMock(
55  \Magento\Framework\Event::class,
56  ['getBaseCode', 'getCurrencyOptions']
57  );
58 
59  $this->mockEventObserver->expects($this->any())->method('getEvent')->willReturn($this->mockEvent);
60  $this->mockSymbolFactory->expects($this->any())->method('create')->willReturn($this->mockSymbol);
61 
62  $this->observer = new \Magento\CurrencySymbol\Observer\CurrencyDisplayOptions($this->mockSymbolFactory);
63  }
64 
66  {
67  $baseData = [
69  ];
70  $sampleCurrencyOptionObject = new \Magento\Framework\DataObject($baseData);
71 
72  //Return invalid value
73  $this->mockEvent->expects($this->once())->method('getBaseCode')->willReturn(null);
74  $this->mockEvent->expects($this->once())->method('getCurrencyOptions')->willReturn($sampleCurrencyOptionObject);
75  $this->mockSymbol->expects($this->never())->method('getCurrencySymbol')->with(null)->willReturn(null);
76 
77  $this->observer->execute($this->mockEventObserver);
78 
79  // Check if option set is empty
80  $this->assertEquals($baseData, $sampleCurrencyOptionObject->getData());
81  }
82 
83  public function testCurrencyDisplayOptions()
84  {
85  $baseData = [
87  ];
88  $sampleCurrencyOptionObject = new \Magento\Framework\DataObject($baseData);
89  $sampleCurrency = 'USD';
90  $sampleCurrencySymbol = '$';
91 
92  $expectedCurrencyOptions = array_merge(
93  $baseData,
94  [
95  \Magento\Framework\Locale\Currency::CURRENCY_OPTION_NAME => 'US Dollar',
96  \Magento\Framework\Locale\Currency::CURRENCY_OPTION_SYMBOL => $sampleCurrencySymbol,
97  \Magento\Framework\Locale\Currency::CURRENCY_OPTION_DISPLAY => \Magento\Framework\Currency::USE_SYMBOL
98  ]
99  );
100 
101  $this->mockEvent->expects($this->once())->method('getBaseCode')->willReturn($sampleCurrency);
102  $this->mockEvent->expects($this->once())->method('getCurrencyOptions')->willReturn($sampleCurrencyOptionObject);
103  $this->mockSymbol->expects($this->once())
104  ->method('getCurrencySymbol')
105  ->with($sampleCurrency)
106  ->willReturn($sampleCurrencySymbol);
107 
108  $this->observer->execute($this->mockEventObserver);
109 
110  $this->assertEquals($expectedCurrencyOptions, $sampleCurrencyOptionObject->getData());
111  }
112 }