DataTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Test\Unit\Helper;
  7. use Magento\Directory\Helper\Data;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class DataTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Directory\Model\ResourceModel\Country\Collection|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $_countryCollection;
  17. /**
  18. * @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_regionCollection;
  21. /**
  22. * @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $jsonHelperMock;
  25. /**
  26. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $_store;
  29. /**
  30. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $scopeConfigMock;
  33. /**
  34. * @var \Magento\Directory\Helper\Data
  35. */
  36. protected $_object;
  37. protected function setUp()
  38. {
  39. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  40. $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  41. $this->scopeConfigMock->expects($this->any())->method('isSetFlag')->willReturn(false);
  42. $context = $this->createMock(\Magento\Framework\App\Helper\Context::class);
  43. $context->expects($this->any())
  44. ->method('getScopeConfig')
  45. ->willReturn($this->scopeConfigMock);
  46. $configCacheType = $this->createMock(\Magento\Framework\App\Cache\Type\Config::class);
  47. $this->_countryCollection = $this->createMock(\Magento\Directory\Model\ResourceModel\Country\Collection::class);
  48. $this->_regionCollection = $this->createMock(\Magento\Directory\Model\ResourceModel\Region\Collection::class);
  49. $regCollectionFactory = $this->createPartialMock(
  50. \Magento\Directory\Model\ResourceModel\Region\CollectionFactory::class,
  51. ['create']
  52. );
  53. $regCollectionFactory->expects(
  54. $this->any()
  55. )->method(
  56. 'create'
  57. )->will(
  58. $this->returnValue($this->_regionCollection)
  59. );
  60. $this->jsonHelperMock = $this->createMock(\Magento\Framework\Json\Helper\Data::class);
  61. $this->_store = $this->createMock(\Magento\Store\Model\Store::class);
  62. $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  63. $storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->_store));
  64. $currencyFactory = $this->createMock(\Magento\Directory\Model\CurrencyFactory::class);
  65. $arguments = [
  66. 'context' => $context,
  67. 'configCacheType' => $configCacheType,
  68. 'countryCollection' => $this->_countryCollection,
  69. 'regCollectionFactory' => $regCollectionFactory,
  70. 'jsonHelper' => $this->jsonHelperMock,
  71. 'storeManager' => $storeManager,
  72. 'currencyFactory' => $currencyFactory,
  73. ];
  74. $this->_object = $objectManager->getObject(\Magento\Directory\Helper\Data::class, $arguments);
  75. }
  76. public function testGetRegionJson()
  77. {
  78. $countries = [
  79. new \Magento\Framework\DataObject(['country_id' => 'Country1']),
  80. new \Magento\Framework\DataObject(['country_id' => 'Country2'])
  81. ];
  82. $countryIterator = new \ArrayIterator($countries);
  83. $this->_countryCollection->expects(
  84. $this->atLeastOnce()
  85. )->method(
  86. 'getIterator'
  87. )->will(
  88. $this->returnValue($countryIterator)
  89. );
  90. $regions = [
  91. new \Magento\Framework\DataObject(
  92. ['country_id' => 'Country1', 'region_id' => 'r1', 'code' => 'r1-code', 'name' => 'r1-name']
  93. ),
  94. new \Magento\Framework\DataObject(
  95. ['country_id' => 'Country1', 'region_id' => 'r2', 'code' => 'r2-code', 'name' => 'r2-name']
  96. ),
  97. new \Magento\Framework\DataObject(
  98. ['country_id' => 'Country2', 'region_id' => 'r3', 'code' => 'r3-code', 'name' => 'r3-name']
  99. )
  100. ];
  101. $regionIterator = new \ArrayIterator($regions);
  102. $this->_regionCollection->expects(
  103. $this->once()
  104. )->method(
  105. 'addCountryFilter'
  106. )->with(
  107. ['Country1', 'Country2']
  108. )->will(
  109. $this->returnSelf()
  110. );
  111. $this->_regionCollection->expects($this->once())->method('load');
  112. $this->_regionCollection->expects(
  113. $this->once()
  114. )->method(
  115. 'getIterator'
  116. )->will(
  117. $this->returnValue($regionIterator)
  118. );
  119. $expectedDataToEncode = [
  120. 'config' => ['show_all_regions' => false, 'regions_required' => []],
  121. 'Country1' => [
  122. 'r1' => ['code' => 'r1-code', 'name' => 'r1-name'],
  123. 'r2' => ['code' => 'r2-code', 'name' => 'r2-name']
  124. ],
  125. 'Country2' => ['r3' => ['code' => 'r3-code', 'name' => 'r3-name']]
  126. ];
  127. $this->jsonHelperMock->expects(
  128. $this->once()
  129. )->method(
  130. 'jsonEncode'
  131. )->with(
  132. new \PHPUnit\Framework\Constraint\IsIdentical($expectedDataToEncode)
  133. )->will(
  134. $this->returnValue('encoded_json')
  135. );
  136. // Test
  137. $result = $this->_object->getRegionJson();
  138. $this->assertEquals('encoded_json', $result);
  139. }
  140. /**
  141. * @param string $configValue
  142. * @param mixed $expected
  143. * @dataProvider countriesCommaListDataProvider
  144. */
  145. public function testGetCountriesWithStatesRequired($configValue, $expected)
  146. {
  147. $this->scopeConfigMock->expects(
  148. $this->once()
  149. )->method(
  150. 'getValue'
  151. )->with(
  152. 'general/region/state_required'
  153. )->will(
  154. $this->returnValue($configValue)
  155. );
  156. $result = $this->_object->getCountriesWithStatesRequired();
  157. $this->assertEquals($expected, $result);
  158. }
  159. /**
  160. * @param string $configValue
  161. * @param mixed $expected
  162. * @dataProvider countriesCommaListDataProvider
  163. */
  164. public function testGetCountriesWithOptionalZip($configValue, $expected)
  165. {
  166. $this->scopeConfigMock->expects(
  167. $this->once()
  168. )->method(
  169. 'getValue'
  170. )->with(
  171. 'general/country/optional_zip_countries'
  172. )->will(
  173. $this->returnValue($configValue)
  174. );
  175. $result = $this->_object->getCountriesWithOptionalZip();
  176. $this->assertEquals($expected, $result);
  177. }
  178. /**
  179. * @return array
  180. */
  181. public static function countriesCommaListDataProvider()
  182. {
  183. return [
  184. 'empty_list' => ['', []],
  185. 'normal_list' => ['Country1,Country2', ['Country1', 'Country2']]
  186. ];
  187. }
  188. public function testGetDefaultCountry()
  189. {
  190. $storeId = 'storeId';
  191. $country = 'country';
  192. $this->scopeConfigMock->expects($this->once())
  193. ->method('getValue')
  194. ->with(
  195. Data::XML_PATH_DEFAULT_COUNTRY,
  196. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  197. $storeId
  198. )->will($this->returnValue($country));
  199. $this->assertEquals($country, $this->_object->getDefaultCountry($storeId));
  200. }
  201. public function testGetCountryCollection()
  202. {
  203. $this->_countryCollection->expects(
  204. $this->once()
  205. )->method(
  206. 'isLoaded'
  207. )->will(
  208. $this->returnValue(0)
  209. );
  210. $store = $this->createMock(\Magento\Store\Model\Store::class);
  211. $this->_countryCollection->expects(
  212. $this->once()
  213. )->method(
  214. 'loadByStore'
  215. )->with(
  216. $store
  217. );
  218. $this->_object->getCountryCollection($store);
  219. }
  220. /**
  221. * @param string $topCountriesValue
  222. * @param array $expectedResult
  223. * @dataProvider topCountriesDataProvider
  224. */
  225. public function testGetTopCountryCodesReturnsParsedConfigurationValue($topCountriesValue, $expectedResult)
  226. {
  227. $this->scopeConfigMock->expects($this->once())
  228. ->method('getValue')->with(\Magento\Directory\Helper\Data::XML_PATH_TOP_COUNTRIES)
  229. ->willReturn($topCountriesValue);
  230. $this->assertEquals($expectedResult, $this->_object->getTopCountryCodes());
  231. }
  232. /**
  233. * @return array
  234. */
  235. public function topCountriesDataProvider()
  236. {
  237. return [
  238. [null, []],
  239. ['', []],
  240. ['US', ['US']],
  241. ['US,RU', ['US', 'RU']],
  242. ];
  243. }
  244. }