1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CurrencySymbol\Test\Unit\Observer;
use Magento\CurrencySymbol\Model\System\CurrencysymbolFactory;
/**
* Test for \Magento\CurrencySymbol\Observer\CurrencyDisplayOptions
*/
class CurrencyDisplayOptionsTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\CurrencySymbol\Observer\CurrencyDisplayOptions
*/
private $observer;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|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;
protected function setUp()
{
$this->mockSymbolFactory = $this->createPartialMock(
\Magento\CurrencySymbol\Model\System\CurrencysymbolFactory::class,
['create']
);
$this->mockSymbol = $this->createPartialMock(
\Magento\CurrencySymbol\Model\System\Currencysymbol::class,
['getCurrencySymbol']
);
$this->mockEventObserver = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getEvent']);
$this->mockEvent = $this->createPartialMock(
\Magento\Framework\Event::class,
['getBaseCode', 'getCurrencyOptions']
);
$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\Observer\CurrencyDisplayOptions($this->mockSymbolFactory);
}
public function testCurrencyDisplayOptionsEmpty()
{
$baseData = [
\Magento\Framework\Locale\Currency::CURRENCY_OPTION_NAME => 'US Dollar'
];
$sampleCurrencyOptionObject = new \Magento\Framework\DataObject($baseData);
//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->execute($this->mockEventObserver);
// Check if option set is empty
$this->assertEquals($baseData, $sampleCurrencyOptionObject->getData());
}
public function testCurrencyDisplayOptions()
{
$baseData = [
\Magento\Framework\Locale\Currency::CURRENCY_OPTION_NAME => 'US Dollar'
];
$sampleCurrencyOptionObject = new \Magento\Framework\DataObject($baseData);
$sampleCurrency = 'USD';
$sampleCurrencySymbol = '$';
$expectedCurrencyOptions = array_merge(
$baseData,
[
\Magento\Framework\Locale\Currency::CURRENCY_OPTION_NAME => 'US Dollar',
\Magento\Framework\Locale\Currency::CURRENCY_OPTION_SYMBOL => $sampleCurrencySymbol,
\Magento\Framework\Locale\Currency::CURRENCY_OPTION_DISPLAY => \Magento\Framework\Currency::USE_SYMBOL
]
);
$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->execute($this->mockEventObserver);
$this->assertEquals($expectedCurrencyOptions, $sampleCurrencyOptionObject->getData());
}
}