CountryInformationAcquirerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 CountryInformationAcquirerTest extends WebapiAbstract
  9. {
  10. const SERVICE_NAME = 'directoryCountryInformationAcquirerV1';
  11. const RESOURCE_COUNTRIES_PATH = '/V1/directory/countries';
  12. const RESOURCE_COUNTRY = 'US';
  13. const SERVICE_VERSION = 'V1';
  14. const STORE_CODE_FROM_FIXTURE = 'fixturestore';
  15. /**
  16. * @magentoApiDataFixture Magento/Store/_files/core_fixturestore.php
  17. */
  18. public function testGetCountries()
  19. {
  20. /** @var $store \Magento\Store\Model\Group */
  21. $store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class);
  22. $store->load(self::STORE_CODE_FROM_FIXTURE);
  23. $this->assertNotEmpty($store->getId(), 'Precondition failed: fixture store was not created.');
  24. $result = $this->getCountriesInfo(self::STORE_CODE_FROM_FIXTURE);
  25. $this->assertNotEmpty($result);
  26. $this->assertArrayHasKey('id', $result[0]);
  27. $this->assertArrayHasKey('two_letter_abbreviation', $result[0]);
  28. $this->assertArrayHasKey('three_letter_abbreviation', $result[0]);
  29. $this->assertArrayHasKey('full_name_locale', $result[0]);
  30. $this->assertArrayHasKey('full_name_english', $result[0]);
  31. $this->assertSame('AD', $result[0]['id']);
  32. $this->assertSame('AD', $result[0]['two_letter_abbreviation']);
  33. $this->assertSame('AND', $result[0]['three_letter_abbreviation']);
  34. $this->assertSame('Andorra', $result[0]['full_name_english']);
  35. }
  36. /**
  37. * @magentoApiDataFixture Magento/Store/_files/core_fixturestore.php
  38. */
  39. public function testGetCountry()
  40. {
  41. /** @var $store \Magento\Store\Model\Group */
  42. $store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class);
  43. $store->load(self::STORE_CODE_FROM_FIXTURE);
  44. $this->assertNotEmpty($store->getId(), 'Precondition failed: fixture store was not created.');
  45. $result = $this->getCountryInfo(self::STORE_CODE_FROM_FIXTURE);
  46. $this->assertNotEmpty($result);
  47. $this->assertArrayHasKey('id', $result);
  48. $this->assertArrayHasKey('two_letter_abbreviation', $result);
  49. $this->assertArrayHasKey('three_letter_abbreviation', $result);
  50. $this->assertArrayHasKey('full_name_locale', $result);
  51. $this->assertArrayHasKey('full_name_english', $result);
  52. $this->assertArrayHasKey('available_regions', $result);
  53. $this->assertSame('US', $result['id']);
  54. $this->assertSame('US', $result['two_letter_abbreviation']);
  55. $this->assertSame('USA', $result['three_letter_abbreviation']);
  56. $this->assertSame('United States', $result['full_name_english']);
  57. }
  58. /**
  59. * Retrieve existing country information for the store
  60. *
  61. * @param string $storeCode
  62. * @return \Magento\Directory\Api\Data\CountryInformationInterface
  63. */
  64. protected function getCountriesInfo($storeCode = 'default')
  65. {
  66. $serviceInfo = [
  67. 'rest' => [
  68. 'resourcePath' => self::RESOURCE_COUNTRIES_PATH,
  69. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  70. ],
  71. 'soap' => [
  72. 'service' => self::SERVICE_NAME,
  73. 'serviceVersion' => self::SERVICE_VERSION,
  74. 'operation' => self::SERVICE_NAME . 'GetCountriesInfo',
  75. ],
  76. ];
  77. $requestData = ['storeId' => $storeCode];
  78. return $this->_webApiCall($serviceInfo, $requestData);
  79. }
  80. /**
  81. * Retrieve existing country information for the store
  82. *
  83. * @param string $storeCode
  84. * @return \Magento\Directory\Api\Data\CountryInformationInterface
  85. */
  86. protected function getCountryInfo($storeCode = 'default')
  87. {
  88. $serviceInfo = [
  89. 'rest' => [
  90. 'resourcePath' => self::RESOURCE_COUNTRIES_PATH . '/' . self::RESOURCE_COUNTRY,
  91. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  92. ],
  93. 'soap' => [
  94. 'service' => self::SERVICE_NAME,
  95. 'serviceVersion' => self::SERVICE_VERSION,
  96. 'operation' => self::SERVICE_NAME . 'GetCountryInfo',
  97. ],
  98. ];
  99. $requestData = ['storeId' => $storeCode, 'countryId' => self::RESOURCE_COUNTRY];
  100. return $this->_webApiCall($serviceInfo, $requestData);
  101. }
  102. /**
  103. * Remove test store
  104. */
  105. public static function tearDownAfterClass()
  106. {
  107. parent::tearDownAfterClass();
  108. /** @var \Magento\Framework\Registry $registry */
  109. $registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  110. ->get(\Magento\Framework\Registry::class);
  111. $registry->unregister('isSecureArea');
  112. $registry->register('isSecureArea', true);
  113. /** @var $store \Magento\Store\Model\Store */
  114. $store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class);
  115. $store->load(self::STORE_CODE_FROM_FIXTURE);
  116. if ($store->getId()) {
  117. $store->delete();
  118. }
  119. $registry->unregister('isSecureArea');
  120. $registry->register('isSecureArea', false);
  121. }
  122. }