CountryInformationAcquirerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\Framework\Exception\NoSuchEntityException;
  8. /**
  9. * Class CountryInformationAcquirerTest
  10. */
  11. class CountryInformationAcquirerTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Directory\Model\CountryInformationAcquirer
  15. */
  16. protected $model;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $countryInformationFactory;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $regionInformationFactory;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $storeManager;
  29. /**
  30. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  31. */
  32. protected $objectManager;
  33. /**
  34. * Setup the test
  35. */
  36. protected function setUp()
  37. {
  38. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  39. $className = \Magento\Directory\Model\Data\CountryInformationFactory::class;
  40. $this->countryInformationFactory = $this->createPartialMock($className, ['create']);
  41. $className = \Magento\Directory\Model\Data\RegionInformationFactory::class;
  42. $this->regionInformationFactory = $this->createPartialMock($className, ['create']);
  43. $className = \Magento\Directory\Helper\Data::class;
  44. $this->directoryHelper = $this->createPartialMock($className, ['getCountryCollection', 'getRegionData']);
  45. $className = \Magento\Store\Model\StoreManager::class;
  46. $this->storeManager = $this->createPartialMock($className, ['getStore']);
  47. $this->model = $this->objectManager->getObject(
  48. \Magento\Directory\Model\CountryInformationAcquirer::class,
  49. [
  50. 'countryInformationFactory' => $this->countryInformationFactory,
  51. 'regionInformationFactory' => $this->regionInformationFactory,
  52. 'directoryHelper' => $this->directoryHelper,
  53. 'storeManager' => $this->storeManager,
  54. ]
  55. );
  56. }
  57. /**
  58. * test GetCountriesInfo
  59. */
  60. public function testGetCountriesInfo()
  61. {
  62. /** @var \Magento\Store\Model\Store $store */
  63. $store = $this->createMock(\Magento\Store\Model\Store::class);
  64. $this->storeManager->expects($this->once())->method('getStore')->willReturn($store);
  65. $testCountryInfo = $this->objectManager->getObject(\Magento\Directory\Model\Country::class);
  66. $testCountryInfo->setData('country_id', 'US');
  67. $testCountryInfo->setData('iso2_code', 'US');
  68. $testCountryInfo->setData('iso3_code', 'USA');
  69. $testCountryInfo->setData('name_default', 'United States of America');
  70. $testCountryInfo->setData('name_en_US', 'United States of America');
  71. $countries = ['US' => $testCountryInfo];
  72. $this->directoryHelper->expects($this->once())->method('getCountryCollection')->willReturn($countries);
  73. $regions = ['US' => ['TX' => ['code' => 'TX', 'name' => 'Texas']]];
  74. $this->directoryHelper->expects($this->once())->method('getRegionData')->willReturn($regions);
  75. $countryInfo = $this->objectManager->getObject(\Magento\Directory\Model\Data\CountryInformation::class);
  76. $this->countryInformationFactory->expects($this->once())->method('create')->willReturn($countryInfo);
  77. $regionInfo = $this->objectManager->getObject(\Magento\Directory\Model\Data\RegionInformation::class);
  78. $this->regionInformationFactory->expects($this->once())->method('create')->willReturn($regionInfo);
  79. $result = $this->model->getCountriesInfo();
  80. $this->assertEquals('US', $result[0]->getId());
  81. $this->assertEquals('US', $result[0]->getTwoLetterAbbreviation());
  82. $this->assertEquals('USA', $result[0]->getThreeLetterAbbreviation());
  83. $this->assertEquals('United States of America', $result[0]->getFullNameLocale());
  84. $this->assertEquals('United States of America', $result[0]->getFullNameEnglish());
  85. $regionResult = $result[0]->getAvailableRegions();
  86. $this->assertEquals('TX', $regionResult[0]->getCode());
  87. $this->assertEquals('Texas', $regionResult[0]->getName());
  88. }
  89. /**
  90. * test GetGetCountryInfo
  91. */
  92. public function testGetCountryInfo()
  93. {
  94. /** @var \Magento\Store\Model\Store $store */
  95. $store = $this->createMock(\Magento\Store\Model\Store::class);
  96. $this->storeManager->expects($this->once())->method('getStore')->willReturn($store);
  97. $testCountryInfo = $this->objectManager->getObject(\Magento\Directory\Model\Country::class);
  98. $testCountryInfo->setData('country_id', 'AE');
  99. $testCountryInfo->setData('iso2_code', 'AE');
  100. $testCountryInfo->setData('iso3_code', 'ARE');
  101. $testCountryInfo->setData('name_default', 'United Arab Emirates');
  102. $testCountryInfo->setData('name_en_US', 'United Arab Emirates');
  103. $countryCollection = $this->createMock(\Magento\Directory\Model\ResourceModel\Country\Collection::class);
  104. $countryCollection->expects($this->once())->method('load')->willReturnSelf();
  105. $countryCollection->expects($this->once())->method('getItemById')->with('AE')->willReturn($testCountryInfo);
  106. $this->directoryHelper->expects($this->once())->method('getCountryCollection')->willReturn($countryCollection);
  107. $this->directoryHelper->expects($this->once())->method('getRegionData')->willReturn([]);
  108. $countryInfo = $this->objectManager->getObject(\Magento\Directory\Model\Data\CountryInformation::class);
  109. $this->countryInformationFactory->expects($this->once())->method('create')->willReturn($countryInfo);
  110. $result = $this->model->getCountryInfo('AE');
  111. $this->assertEquals('AE', $result->getId());
  112. $this->assertEquals('AE', $result->getTwoLetterAbbreviation());
  113. $this->assertEquals('ARE', $result->getThreeLetterAbbreviation());
  114. $this->assertEquals('United Arab Emirates', $result->getFullNameLocale());
  115. $this->assertEquals('United Arab Emirates', $result->getFullNameEnglish());
  116. }
  117. /**
  118. * test GetGetCountryInfoNotFound
  119. *
  120. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  121. * @expectedExceptionMessage The country isn't available.
  122. */
  123. public function testGetCountryInfoNotFound()
  124. {
  125. /** @var \Magento\Store\Model\Store $store */
  126. $store = $this->createMock(\Magento\Store\Model\Store::class);
  127. $this->storeManager->expects($this->once())->method('getStore')->willReturn($store);
  128. $testCountryInfo = $this->objectManager->getObject(\Magento\Directory\Model\Country::class);
  129. $testCountryInfo->setData('country_id', 'AE');
  130. $testCountryInfo->setData('iso2_code', 'AE');
  131. $testCountryInfo->setData('iso3_code', 'ARE');
  132. $testCountryInfo->setData('name_default', 'United Arab Emirates');
  133. $testCountryInfo->setData('name_en_US', 'United Arab Emirates');
  134. $countryCollection = $this->createMock(\Magento\Directory\Model\ResourceModel\Country\Collection::class);
  135. $countryCollection->expects($this->once())->method('load')->willReturnSelf();
  136. $this->directoryHelper->expects($this->once())->method('getCountryCollection')->willReturn($countryCollection);
  137. $countryCollection->expects($this->once())->method('getItemById')->willReturn(null);
  138. $this->model->getCountryInfo('AE');
  139. }
  140. }