CurrencyInformationAcquirerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /**
  8. * Class CurrencyInformationAcquirerTest
  9. */
  10. class CurrencyInformationAcquirerTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Directory\Model\CurrencyInformationAcquirer
  14. */
  15. protected $model;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $currencyInformationFactory;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $exchangeRateFactory;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $storeManager;
  28. /**
  29. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  30. */
  31. protected $objectManager;
  32. /**
  33. * Setup the test
  34. */
  35. protected function setUp()
  36. {
  37. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  38. $className = \Magento\Directory\Model\Data\CurrencyInformationFactory::class;
  39. $this->currencyInformationFactory = $this->createPartialMock($className, ['create']);
  40. $className = \Magento\Directory\Model\Data\ExchangeRateFactory::class;
  41. $this->exchangeRateFactory = $this->createPartialMock($className, ['create']);
  42. $className = \Magento\Store\Model\StoreManager::class;
  43. $this->storeManager = $this->createPartialMock($className, ['getStore']);
  44. $this->model = $this->objectManager->getObject(
  45. \Magento\Directory\Model\CurrencyInformationAcquirer::class,
  46. [
  47. 'currencyInformationFactory' => $this->currencyInformationFactory,
  48. 'exchangeRateFactory' => $this->exchangeRateFactory,
  49. 'storeManager' => $this->storeManager,
  50. ]
  51. );
  52. }
  53. /**
  54. * test GetCurrencyInfo
  55. */
  56. public function testGetCurrencyInfo()
  57. {
  58. /** @var \Magento\Directory\Model\Data\ExchangeRate $exchangeRate */
  59. $exchangeRate = $this->createPartialMock(\Magento\Directory\Model\Data\ExchangeRate::class, ['load']);
  60. $exchangeRate->expects($this->any())->method('load')->willReturnSelf();
  61. $this->exchangeRateFactory->expects($this->any())->method('create')->willReturn($exchangeRate);
  62. /** @var \Magento\Directory\Model\Data\CurrencyInformation $currencyInformation */
  63. $currencyInformation = $this->createPartialMock(
  64. \Magento\Directory\Model\Data\CurrencyInformation::class,
  65. ['load']
  66. );
  67. $currencyInformation->expects($this->any())->method('load')->willReturnSelf();
  68. $this->currencyInformationFactory->expects($this->any())->method('create')->willReturn($currencyInformation);
  69. /** @var \Magento\Store\Model\Store $store */
  70. $store = $this->createMock(\Magento\Store\Model\Store::class);
  71. /** @var \Magento\Directory\Model\Currency $baseCurrency */
  72. $baseCurrency = $this->createPartialMock(
  73. \Magento\Directory\Model\Currency::class,
  74. ['getCode', 'getCurrencySymbol', 'getRate']
  75. );
  76. $baseCurrency->expects($this->atLeastOnce())->method('getCode')->willReturn('USD');
  77. $baseCurrency->expects($this->atLeastOnce())->method('getCurrencySymbol')->willReturn('$');
  78. $baseCurrency->expects($this->once())->method('getRate')->with('AUD')->willReturn('0.80');
  79. $store->expects($this->atLeastOnce())->method('getBaseCurrency')->willReturn($baseCurrency);
  80. $store->expects($this->atLeastOnce())->method('getDefaultCurrency')->willReturn($baseCurrency);
  81. $store->expects($this->atLeastOnce())->method('getAvailableCurrencyCodes')->willReturn(['AUD']);
  82. $this->storeManager->expects($this->any())->method('getStore')->willReturn($store);
  83. $result = $this->model->getCurrencyInfo();
  84. $this->assertEquals($currencyInformation, $result);
  85. $this->assertEquals('USD', $result->getBaseCurrencyCode());
  86. $this->assertEquals('$', $result->getBaseCurrencySymbol());
  87. $this->assertEquals('USD', $result->getDefaultDisplayCurrencyCode());
  88. $this->assertEquals('$', $result->getDefaultDisplayCurrencySymbol());
  89. $this->assertEquals(['AUD'], $result->getAvailableCurrencyCodes());
  90. $this->assertTrue(is_array($result->getExchangeRates()));
  91. $this->assertEquals([$exchangeRate], $result->getExchangeRates());
  92. $this->assertEquals('0.80', $result->getExchangeRates()[0]->getRate());
  93. $this->assertEquals('AUD', $result->getExchangeRates()[0]->getCurrencyTo());
  94. }
  95. }