123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Directory\Test\Unit\Model;
- use Magento\Directory\Model\PriceCurrency;
- class PriceCurrencyTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var PriceCurrency
- */
- protected $priceCurrency;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeManager;
- /**
- * @var \Magento\Directory\Model\CurrencyFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $currencyFactory;
- protected function setUp()
- {
- $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->currencyFactory = $this->getMockBuilder(\Magento\Directory\Model\CurrencyFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
- ->getMock();
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->priceCurrency = $objectManager->getObject(
- \Magento\Directory\Model\PriceCurrency::class,
- [
- 'storeManager' => $this->storeManager,
- 'currencyFactory' => $this->currencyFactory
- ]
- );
- }
- public function testConvert()
- {
- $amount = 5.6;
- $convertedAmount = 9.3;
- $currency = $this->getCurrentCurrencyMock();
- $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
- $store = $this->getStoreMock($baseCurrency);
- $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $store, $currency));
- }
- public function testConvertWithStoreCode()
- {
- $amount = 5.6;
- $storeCode = 2;
- $convertedAmount = 9.3;
- $currency = $this->getCurrentCurrencyMock();
- $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
- $store = $this->getStoreMock($baseCurrency);
- $this->storeManager->expects($this->once())
- ->method('getStore')
- ->with($storeCode)
- ->will($this->returnValue($store));
- $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $storeCode, $currency));
- }
- public function testConvertWithCurrencyString()
- {
- $amount = 5.6;
- $currency = 'ru';
- $convertedAmount = 9.3;
- $currentCurrency = $this->getCurrentCurrencyMock();
- $currentCurrency->expects($this->once())
- ->method('load')
- ->with($currency)
- ->will($this->returnSelf());
- $this->currencyFactory->expects($this->once())
- ->method('create')
- ->will($this->returnValue($currentCurrency));
- $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currentCurrency);
- $baseCurrency->expects($this->once())
- ->method('getRate')
- ->with($currentCurrency)
- ->will($this->returnValue(1.2));
- $store = $this->getStoreMock($baseCurrency);
- $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $store, $currency));
- }
- public function testConvertWithStoreCurrency()
- {
- $amount = 5.6;
- $currency = null;
- $convertedAmount = 9.3;
- $currentCurrency = $this->getCurrentCurrencyMock();
- $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currentCurrency);
- $store = $this->getStoreMock($baseCurrency);
- $store->expects($this->atLeastOnce())
- ->method('getCurrentCurrency')
- ->will($this->returnValue($currentCurrency));
- $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $store, $currency));
- }
- public function testFormat()
- {
- $amount = 5.6;
- $precision = \Magento\Framework\Pricing\PriceCurrencyInterface::DEFAULT_PRECISION;
- $includeContainer = false;
- $store = null;
- $formattedAmount = '5.6 grn';
- $currency = $this->getCurrentCurrencyMock();
- $currency->expects($this->once())
- ->method('formatPrecision')
- ->with($amount, $precision, [], $includeContainer)
- ->will($this->returnValue($formattedAmount));
- $this->assertEquals($formattedAmount, $this->priceCurrency->format(
- $amount,
- $includeContainer,
- $precision,
- $store,
- $currency
- ));
- }
- public function testConvertAndFormat()
- {
- $amount = 5.6;
- $precision = \Magento\Framework\Pricing\PriceCurrencyInterface::DEFAULT_PRECISION;
- $includeContainer = false;
- $store = null;
- $convertedAmount = 9.3;
- $formattedAmount = '9.3 grn';
- $currency = $this->getCurrentCurrencyMock();
- $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
- $store = $this->getStoreMock($baseCurrency);
- $currency->expects($this->once())
- ->method('formatPrecision')
- ->with($convertedAmount, $precision, [], $includeContainer)
- ->will($this->returnValue($formattedAmount));
- $this->assertEquals($formattedAmount, $this->priceCurrency->convertAndFormat(
- $amount,
- $includeContainer,
- $precision,
- $store,
- $currency
- ));
- }
- public function testGetCurrencySymbol()
- {
- $storeId = 2;
- $currencySymbol = '$';
- $currencyMock = $this->getCurrentCurrencyMock();
- $currencyMock->expects($this->once())
- ->method('getCurrencySymbol')
- ->willReturn($currencySymbol);
- $this->assertEquals($currencySymbol, $this->priceCurrency->getCurrencySymbol($storeId, $currencyMock));
- }
- /**
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function getCurrentCurrencyMock()
- {
- $currency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
- ->disableOriginalConstructor()
- ->getMock();
- return $currency;
- }
- /**
- * @param $baseCurrency
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function getStoreMock($baseCurrency)
- {
- $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $store->expects($this->atLeastOnce())
- ->method('getBaseCurrency')
- ->will($this->returnValue($baseCurrency));
- return $store;
- }
- /**
- * @param $amount
- * @param $convertedAmount
- * @param $currency
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function getBaseCurrencyMock($amount, $convertedAmount, $currency)
- {
- $baseCurrency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
- ->disableOriginalConstructor()
- ->getMock();
- $baseCurrency->expects($this->once())
- ->method('convert')
- ->with($amount, $currency)
- ->will($this->returnValue($convertedAmount));
- return $baseCurrency;
- }
- public function testConvertAndRound()
- {
- $amount = 5.6;
- $storeCode = 2;
- $convertedAmount = 9.326;
- $roundedConvertedAmount = 9.33;
- $currency = $this->getCurrentCurrencyMock();
- $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currency);
- $store = $this->getStoreMock($baseCurrency);
- $this->storeManager->expects($this->once())
- ->method('getStore')
- ->with($storeCode)
- ->will($this->returnValue($store));
- $this->assertEquals(
- $roundedConvertedAmount,
- $this->priceCurrency->convertAndRound($amount, $storeCode, $currency)
- );
- }
- }
|