Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PriceCurrencyTest.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class PriceCurrencyTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $priceCurrency;
17 
21  protected $storeManager;
22 
26  protected $currencyFactory;
27 
28  protected function setUp()
29  {
30  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManager::class)
31  ->disableOriginalConstructor()
32  ->getMock();
33 
34  $this->currencyFactory = $this->getMockBuilder(\Magento\Directory\Model\CurrencyFactory::class)
35  ->disableOriginalConstructor()
36  ->setMethods(['create'])
37  ->getMock();
38 
39  $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
40  ->getMock();
41 
42  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
43  $this->priceCurrency = $objectManager->getObject(
44  \Magento\Directory\Model\PriceCurrency::class,
45  [
46  'storeManager' => $this->storeManager,
47  'currencyFactory' => $this->currencyFactory
48  ]
49  );
50  }
51 
52  public function testConvert()
53  {
54  $amount = 5.6;
55  $convertedAmount = 9.3;
56 
57  $currency = $this->getCurrentCurrencyMock();
58  $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
59  $store = $this->getStoreMock($baseCurrency);
60 
61  $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $store, $currency));
62  }
63 
64  public function testConvertWithStoreCode()
65  {
66  $amount = 5.6;
67  $storeCode = 2;
68  $convertedAmount = 9.3;
69 
70  $currency = $this->getCurrentCurrencyMock();
71  $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
72  $store = $this->getStoreMock($baseCurrency);
73 
74  $this->storeManager->expects($this->once())
75  ->method('getStore')
76  ->with($storeCode)
77  ->will($this->returnValue($store));
78 
79  $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $storeCode, $currency));
80  }
81 
83  {
84  $amount = 5.6;
85  $currency = 'ru';
86  $convertedAmount = 9.3;
87 
88  $currentCurrency = $this->getCurrentCurrencyMock();
89  $currentCurrency->expects($this->once())
90  ->method('load')
91  ->with($currency)
92  ->will($this->returnSelf());
93 
94  $this->currencyFactory->expects($this->once())
95  ->method('create')
96  ->will($this->returnValue($currentCurrency));
97 
98  $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currentCurrency);
99  $baseCurrency->expects($this->once())
100  ->method('getRate')
101  ->with($currentCurrency)
102  ->will($this->returnValue(1.2));
103  $store = $this->getStoreMock($baseCurrency);
104 
105  $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $store, $currency));
106  }
107 
109  {
110  $amount = 5.6;
111  $currency = null;
112  $convertedAmount = 9.3;
113 
114  $currentCurrency = $this->getCurrentCurrencyMock();
115  $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currentCurrency);
116  $store = $this->getStoreMock($baseCurrency);
117  $store->expects($this->atLeastOnce())
118  ->method('getCurrentCurrency')
119  ->will($this->returnValue($currentCurrency));
120 
121  $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $store, $currency));
122  }
123 
124  public function testFormat()
125  {
126  $amount = 5.6;
128  $includeContainer = false;
129  $store = null;
130  $formattedAmount = '5.6 grn';
131 
132  $currency = $this->getCurrentCurrencyMock();
133  $currency->expects($this->once())
134  ->method('formatPrecision')
135  ->with($amount, $precision, [], $includeContainer)
136  ->will($this->returnValue($formattedAmount));
137 
138  $this->assertEquals($formattedAmount, $this->priceCurrency->format(
139  $amount,
140  $includeContainer,
141  $precision,
142  $store,
143  $currency
144  ));
145  }
146 
147  public function testConvertAndFormat()
148  {
149  $amount = 5.6;
151  $includeContainer = false;
152  $store = null;
153  $convertedAmount = 9.3;
154  $formattedAmount = '9.3 grn';
155 
156  $currency = $this->getCurrentCurrencyMock();
157  $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
158  $store = $this->getStoreMock($baseCurrency);
159 
160  $currency->expects($this->once())
161  ->method('formatPrecision')
162  ->with($convertedAmount, $precision, [], $includeContainer)
163  ->will($this->returnValue($formattedAmount));
164 
165  $this->assertEquals($formattedAmount, $this->priceCurrency->convertAndFormat(
166  $amount,
167  $includeContainer,
168  $precision,
169  $store,
170  $currency
171  ));
172  }
173 
174  public function testGetCurrencySymbol()
175  {
176  $storeId = 2;
177  $currencySymbol = '$';
178 
179  $currencyMock = $this->getCurrentCurrencyMock();
180  $currencyMock->expects($this->once())
181  ->method('getCurrencySymbol')
182  ->willReturn($currencySymbol);
183  $this->assertEquals($currencySymbol, $this->priceCurrency->getCurrencySymbol($storeId, $currencyMock));
184  }
185 
189  protected function getCurrentCurrencyMock()
190  {
191  $currency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
192  ->disableOriginalConstructor()
193  ->getMock();
194 
195  return $currency;
196  }
197 
202  protected function getStoreMock($baseCurrency)
203  {
204  $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
205  ->disableOriginalConstructor()
206  ->getMock();
207 
208  $store->expects($this->atLeastOnce())
209  ->method('getBaseCurrency')
210  ->will($this->returnValue($baseCurrency));
211 
212  return $store;
213  }
214 
221  protected function getBaseCurrencyMock($amount, $convertedAmount, $currency)
222  {
223  $baseCurrency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
224  ->disableOriginalConstructor()
225  ->getMock();
226 
227  $baseCurrency->expects($this->once())
228  ->method('convert')
229  ->with($amount, $currency)
230  ->will($this->returnValue($convertedAmount));
231 
232  return $baseCurrency;
233  }
234 
235  public function testConvertAndRound()
236  {
237  $amount = 5.6;
238  $storeCode = 2;
239  $convertedAmount = 9.326;
240  $roundedConvertedAmount = 9.33;
241 
242  $currency = $this->getCurrentCurrencyMock();
243  $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
244  $store = $this->getStoreMock($baseCurrency);
245 
246  $this->storeManager->expects($this->once())
247  ->method('getStore')
248  ->with($storeCode)
249  ->will($this->returnValue($store));
250 
251  $this->assertEquals(
252  $roundedConvertedAmount,
253  $this->priceCurrency->convertAndRound($amount, $storeCode, $currency)
254  );
255  }
256 }
$objectManager
Definition: bootstrap.php:17
$storeCode
Definition: indexer.php:15
$amount
Definition: order.php:14
$currencySymbol
Definition: matrix.phtml:14
getBaseCurrencyMock($amount, $convertedAmount, $currency)