PriceCurrencyTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Test\Unit\Model;
  7. use Magento\Directory\Model\PriceCurrency;
  8. class PriceCurrencyTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var PriceCurrency
  12. */
  13. protected $priceCurrency;
  14. /**
  15. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $storeManager;
  18. /**
  19. * @var \Magento\Directory\Model\CurrencyFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $currencyFactory;
  22. protected function setUp()
  23. {
  24. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManager::class)
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $this->currencyFactory = $this->getMockBuilder(\Magento\Directory\Model\CurrencyFactory::class)
  28. ->disableOriginalConstructor()
  29. ->setMethods(['create'])
  30. ->getMock();
  31. $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  32. ->getMock();
  33. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  34. $this->priceCurrency = $objectManager->getObject(
  35. \Magento\Directory\Model\PriceCurrency::class,
  36. [
  37. 'storeManager' => $this->storeManager,
  38. 'currencyFactory' => $this->currencyFactory
  39. ]
  40. );
  41. }
  42. public function testConvert()
  43. {
  44. $amount = 5.6;
  45. $convertedAmount = 9.3;
  46. $currency = $this->getCurrentCurrencyMock();
  47. $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
  48. $store = $this->getStoreMock($baseCurrency);
  49. $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $store, $currency));
  50. }
  51. public function testConvertWithStoreCode()
  52. {
  53. $amount = 5.6;
  54. $storeCode = 2;
  55. $convertedAmount = 9.3;
  56. $currency = $this->getCurrentCurrencyMock();
  57. $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
  58. $store = $this->getStoreMock($baseCurrency);
  59. $this->storeManager->expects($this->once())
  60. ->method('getStore')
  61. ->with($storeCode)
  62. ->will($this->returnValue($store));
  63. $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $storeCode, $currency));
  64. }
  65. public function testConvertWithCurrencyString()
  66. {
  67. $amount = 5.6;
  68. $currency = 'ru';
  69. $convertedAmount = 9.3;
  70. $currentCurrency = $this->getCurrentCurrencyMock();
  71. $currentCurrency->expects($this->once())
  72. ->method('load')
  73. ->with($currency)
  74. ->will($this->returnSelf());
  75. $this->currencyFactory->expects($this->once())
  76. ->method('create')
  77. ->will($this->returnValue($currentCurrency));
  78. $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currentCurrency);
  79. $baseCurrency->expects($this->once())
  80. ->method('getRate')
  81. ->with($currentCurrency)
  82. ->will($this->returnValue(1.2));
  83. $store = $this->getStoreMock($baseCurrency);
  84. $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $store, $currency));
  85. }
  86. public function testConvertWithStoreCurrency()
  87. {
  88. $amount = 5.6;
  89. $currency = null;
  90. $convertedAmount = 9.3;
  91. $currentCurrency = $this->getCurrentCurrencyMock();
  92. $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currentCurrency);
  93. $store = $this->getStoreMock($baseCurrency);
  94. $store->expects($this->atLeastOnce())
  95. ->method('getCurrentCurrency')
  96. ->will($this->returnValue($currentCurrency));
  97. $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $store, $currency));
  98. }
  99. public function testFormat()
  100. {
  101. $amount = 5.6;
  102. $precision = \Magento\Framework\Pricing\PriceCurrencyInterface::DEFAULT_PRECISION;
  103. $includeContainer = false;
  104. $store = null;
  105. $formattedAmount = '5.6 grn';
  106. $currency = $this->getCurrentCurrencyMock();
  107. $currency->expects($this->once())
  108. ->method('formatPrecision')
  109. ->with($amount, $precision, [], $includeContainer)
  110. ->will($this->returnValue($formattedAmount));
  111. $this->assertEquals($formattedAmount, $this->priceCurrency->format(
  112. $amount,
  113. $includeContainer,
  114. $precision,
  115. $store,
  116. $currency
  117. ));
  118. }
  119. public function testConvertAndFormat()
  120. {
  121. $amount = 5.6;
  122. $precision = \Magento\Framework\Pricing\PriceCurrencyInterface::DEFAULT_PRECISION;
  123. $includeContainer = false;
  124. $store = null;
  125. $convertedAmount = 9.3;
  126. $formattedAmount = '9.3 grn';
  127. $currency = $this->getCurrentCurrencyMock();
  128. $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
  129. $store = $this->getStoreMock($baseCurrency);
  130. $currency->expects($this->once())
  131. ->method('formatPrecision')
  132. ->with($convertedAmount, $precision, [], $includeContainer)
  133. ->will($this->returnValue($formattedAmount));
  134. $this->assertEquals($formattedAmount, $this->priceCurrency->convertAndFormat(
  135. $amount,
  136. $includeContainer,
  137. $precision,
  138. $store,
  139. $currency
  140. ));
  141. }
  142. public function testGetCurrencySymbol()
  143. {
  144. $storeId = 2;
  145. $currencySymbol = '$';
  146. $currencyMock = $this->getCurrentCurrencyMock();
  147. $currencyMock->expects($this->once())
  148. ->method('getCurrencySymbol')
  149. ->willReturn($currencySymbol);
  150. $this->assertEquals($currencySymbol, $this->priceCurrency->getCurrencySymbol($storeId, $currencyMock));
  151. }
  152. /**
  153. * @return \PHPUnit_Framework_MockObject_MockObject
  154. */
  155. protected function getCurrentCurrencyMock()
  156. {
  157. $currency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. return $currency;
  161. }
  162. /**
  163. * @param $baseCurrency
  164. * @return \PHPUnit_Framework_MockObject_MockObject
  165. */
  166. protected function getStoreMock($baseCurrency)
  167. {
  168. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  169. ->disableOriginalConstructor()
  170. ->getMock();
  171. $store->expects($this->atLeastOnce())
  172. ->method('getBaseCurrency')
  173. ->will($this->returnValue($baseCurrency));
  174. return $store;
  175. }
  176. /**
  177. * @param $amount
  178. * @param $convertedAmount
  179. * @param $currency
  180. * @return \PHPUnit_Framework_MockObject_MockObject
  181. */
  182. protected function getBaseCurrencyMock($amount, $convertedAmount, $currency)
  183. {
  184. $baseCurrency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
  185. ->disableOriginalConstructor()
  186. ->getMock();
  187. $baseCurrency->expects($this->once())
  188. ->method('convert')
  189. ->with($amount, $currency)
  190. ->will($this->returnValue($convertedAmount));
  191. return $baseCurrency;
  192. }
  193. public function testConvertAndRound()
  194. {
  195. $amount = 5.6;
  196. $storeCode = 2;
  197. $convertedAmount = 9.326;
  198. $roundedConvertedAmount = 9.33;
  199. $currency = $this->getCurrentCurrencyMock();
  200. $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
  201. $store = $this->getStoreMock($baseCurrency);
  202. $this->storeManager->expects($this->once())
  203. ->method('getStore')
  204. ->with($storeCode)
  205. ->will($this->returnValue($store));
  206. $this->assertEquals(
  207. $roundedConvertedAmount,
  208. $this->priceCurrency->convertAndRound($amount, $storeCode, $currency)
  209. );
  210. }
  211. }