CurrencyInformationAcquirerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Api;
  7. use Magento\TestFramework\TestCase\WebapiAbstract;
  8. class CurrencyInformationAcquirerTest extends WebapiAbstract
  9. {
  10. const SERVICE_NAME = 'directoryCurrencyInformationAcquirerV1';
  11. const RESOURCE_PATH = '/V1/directory/currency';
  12. const SERVICE_VERSION = 'V1';
  13. const STORE_CODE_FROM_FIXTURE = 'fixturestore';
  14. /**
  15. * @magentoApiDataFixture Magento/Store/_files/core_fixturestore.php
  16. */
  17. public function testGet()
  18. {
  19. /** @var $store \Magento\Store\Model\Group */
  20. $store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class);
  21. $store->load(self::STORE_CODE_FROM_FIXTURE);
  22. $this->assertNotEmpty($store->getId(), 'Precondition failed: fixture store was not created.');
  23. $result = $this->getCurrencyInfo(self::STORE_CODE_FROM_FIXTURE);
  24. $this->assertNotEmpty($result);
  25. $this->assertArrayHasKey('base_currency_code', $result);
  26. $this->assertArrayHasKey('base_currency_symbol', $result);
  27. $this->assertArrayHasKey('default_display_currency_code', $result);
  28. $this->assertArrayHasKey('default_display_currency_symbol', $result);
  29. $this->assertArrayHasKey('available_currency_codes', $result);
  30. $this->assertArrayHasKey('exchange_rates', $result);
  31. $this->assertTrue(
  32. in_array($result['base_currency_code'], $result['available_currency_codes']),
  33. 'Missing the base currency code in the array of all available codes'
  34. );
  35. $this->assertTrue(
  36. in_array($result['default_display_currency_code'], $result['available_currency_codes']),
  37. 'Missing the default display currency code in the array of all available codes'
  38. );
  39. // ensure the base currency is listed in the array of exchange rates, and has a rate of 1 (no conversion)
  40. $this->verifyExchangeRate($result['base_currency_code'], 1.0, $result['exchange_rates']);
  41. }
  42. /**
  43. * @param string $code
  44. * @param float $rate
  45. * @param array $exchangeRates
  46. */
  47. protected function verifyExchangeRate($code, $rate, $exchangeRates)
  48. {
  49. $this->assertNotEmpty($exchangeRates, 'Expected to have non-empty structure of exchange rates');
  50. $foundCode = false;
  51. $foundRate = false;
  52. foreach ($exchangeRates as $exchangeRate) {
  53. if ($code == $exchangeRate['currency_to']) {
  54. $foundCode = true;
  55. if ($rate == $exchangeRate['rate']) {
  56. $foundRate = true;
  57. }
  58. }
  59. }
  60. $this->assertTrue($foundCode, 'Did not find currency code in the exchange rates: ' . $code);
  61. $this->assertTrue($foundRate, 'Did not find the expected rate for currency ' . $code . ': ' . $rate);
  62. }
  63. /**
  64. * Retrieve existing currency information for the store
  65. *
  66. * @param string $storeCode
  67. * @return \Magento\Directory\Api\Data\CurrencyInformationInterface
  68. */
  69. protected function getCurrencyInfo($storeCode = 'default')
  70. {
  71. $serviceInfo = [
  72. 'rest' => [
  73. 'resourcePath' => self::RESOURCE_PATH,
  74. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  75. ],
  76. 'soap' => [
  77. 'service' => self::SERVICE_NAME,
  78. 'serviceVersion' => self::SERVICE_VERSION,
  79. 'operation' => self::SERVICE_NAME . 'GetCurrencyInfo',
  80. ],
  81. ];
  82. $requestData = ['storeId' => $storeCode];
  83. return $this->_webApiCall($serviceInfo, $requestData);
  84. }
  85. /**
  86. * Remove test store
  87. */
  88. public static function tearDownAfterClass()
  89. {
  90. parent::tearDownAfterClass();
  91. /** @var \Magento\Framework\Registry $registry */
  92. $registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  93. ->get(\Magento\Framework\Registry::class);
  94. $registry->unregister('isSecureArea');
  95. $registry->register('isSecureArea', true);
  96. /** @var $store \Magento\Store\Model\Store */
  97. $store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  98. ->create(\Magento\Store\Model\Store::class);
  99. $store->load(self::STORE_CODE_FROM_FIXTURE);
  100. if ($store->getId()) {
  101. $store->delete();
  102. }
  103. $registry->unregister('isSecureArea');
  104. $registry->register('isSecureArea', false);
  105. }
  106. }