DataTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Test\Unit\Block;
  7. use Magento\Directory\Block\Data;
  8. use Magento\Directory\Helper\Data as HelperData;
  9. use Magento\Directory\Model\ResourceModel\Country\Collection as CountryCollection;
  10. use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory;
  11. use Magento\Framework\App\Cache\Type\Config;
  12. use Magento\Framework\App\Config\ScopeConfigInterface;
  13. use Magento\Framework\Serialize\SerializerInterface;
  14. use Magento\Framework\View\Element\Template\Context;
  15. use Magento\Framework\View\LayoutInterface;
  16. use Magento\Store\Model\ScopeInterface;
  17. use Magento\Store\Model\Store;
  18. use Magento\Store\Model\StoreManagerInterface;
  19. /**
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class DataTest extends \PHPUnit\Framework\TestCase
  23. {
  24. /** @var Data */
  25. private $block;
  26. /** @var Context |\PHPUnit_Framework_MockObject_MockObject */
  27. private $contextMock;
  28. /** @var HelperData |\PHPUnit_Framework_MockObject_MockObject */
  29. private $helperDataMock;
  30. /** @var Config |\PHPUnit_Framework_MockObject_MockObject */
  31. private $cacheTypeConfigMock;
  32. /** @var CountryCollectionFactory |\PHPUnit_Framework_MockObject_MockObject */
  33. private $countryCollectionFactoryMock;
  34. /** @var ScopeConfigInterface |\PHPUnit_Framework_MockObject_MockObject */
  35. private $scopeConfigMock;
  36. /** @var StoreManagerInterface |\PHPUnit_Framework_MockObject_MockObject */
  37. private $storeManagerMock;
  38. /** @var Store |\PHPUnit_Framework_MockObject_MockObject */
  39. private $storeMock;
  40. /** @var CountryCollection |\PHPUnit_Framework_MockObject_MockObject */
  41. private $countryCollectionMock;
  42. /** @var LayoutInterface |\PHPUnit_Framework_MockObject_MockObject */
  43. private $layoutMock;
  44. /** @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */
  45. private $serializerMock;
  46. protected function setUp()
  47. {
  48. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  49. $this->prepareContext();
  50. $this->helperDataMock = $this->getMockBuilder(\Magento\Directory\Helper\Data::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->cacheTypeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Cache\Type\Config::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->prepareCountryCollection();
  57. $this->block = $objectManagerHelper->getObject(
  58. Data::class,
  59. [
  60. 'context' => $this->contextMock,
  61. 'directoryHelper' => $this->helperDataMock,
  62. 'configCacheType' => $this->cacheTypeConfigMock,
  63. 'countryCollectionFactory' => $this->countryCollectionFactoryMock
  64. ]
  65. );
  66. $this->serializerMock = $this->createMock(SerializerInterface::class);
  67. $objectManagerHelper->setBackwardCompatibleProperty(
  68. $this->block,
  69. 'serializer',
  70. $this->serializerMock
  71. );
  72. }
  73. protected function prepareContext()
  74. {
  75. $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  79. ->getMockForAbstractClass();
  80. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  81. ->getMockForAbstractClass();
  82. $this->storeManagerMock->expects($this->any())
  83. ->method('getStore')
  84. ->willReturn($this->storeMock);
  85. $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
  86. ->getMockForAbstractClass();
  87. $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $this->contextMock->expects($this->any())
  91. ->method('getScopeConfig')
  92. ->willReturn($this->scopeConfigMock);
  93. $this->contextMock->expects($this->any())
  94. ->method('getStoreManager')
  95. ->willReturn($this->storeManagerMock);
  96. $this->contextMock->expects($this->any())
  97. ->method('getLayout')
  98. ->willReturn($this->layoutMock);
  99. }
  100. protected function prepareCountryCollection()
  101. {
  102. $this->countryCollectionMock = $this->getMockBuilder(
  103. \Magento\Directory\Model\ResourceModel\Country\Collection::class
  104. )->disableOriginalConstructor()->getMock();
  105. $this->countryCollectionFactoryMock = $this->getMockBuilder(
  106. \Magento\Directory\Model\ResourceModel\Country\CollectionFactory::class
  107. )
  108. ->disableOriginalConstructor()
  109. ->setMethods([
  110. 'create'
  111. ])
  112. ->getMock();
  113. $this->countryCollectionFactoryMock->expects($this->any())
  114. ->method('create')
  115. ->willReturn($this->countryCollectionMock);
  116. }
  117. /**
  118. * @param string $storeCode
  119. * @param int $defaultCountry
  120. * @param string $destinations
  121. * @param array $expectedDestinations
  122. * @param array $options
  123. * @param string $resultHtml
  124. * @dataProvider dataProviderGetCountryHtmlSelect
  125. */
  126. public function testGetCountryHtmlSelect(
  127. $storeCode,
  128. $defaultCountry,
  129. $destinations,
  130. $expectedDestinations,
  131. $options,
  132. $resultHtml
  133. ) {
  134. $this->helperDataMock->expects($this->once())
  135. ->method('getDefaultCountry')
  136. ->willReturn($defaultCountry);
  137. $this->storeMock->expects($this->once())
  138. ->method('getCode')
  139. ->willReturn($storeCode);
  140. $this->serializerMock->expects($this->once())
  141. ->method('serialize')
  142. ->willReturn('serializedData');
  143. $this->cacheTypeConfigMock->expects($this->once())
  144. ->method('load')
  145. ->with('DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode)
  146. ->willReturn(false);
  147. $this->cacheTypeConfigMock->expects($this->once())
  148. ->method('save')
  149. ->with('serializedData', 'DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode)
  150. ->willReturnSelf();
  151. $this->scopeConfigMock->expects($this->once())
  152. ->method('getValue')
  153. ->with('general/country/destinations', ScopeInterface::SCOPE_STORE)
  154. ->willReturn($destinations);
  155. $this->countryCollectionMock->expects($this->once())
  156. ->method('loadByStore')
  157. ->willReturnSelf();
  158. $this->countryCollectionMock->expects($this->any())
  159. ->method('setForegroundCountries')
  160. ->with($expectedDestinations)
  161. ->willReturnSelf();
  162. $this->countryCollectionMock->expects($this->once())
  163. ->method('toOptionArray')
  164. ->willReturn($options);
  165. $elementHtmlSelect = $this->mockElementHtmlSelect($defaultCountry, $options, $resultHtml);
  166. $this->layoutMock->expects($this->once())
  167. ->method('createBlock')
  168. ->willReturn($elementHtmlSelect);
  169. $this->assertEquals($resultHtml, $this->block->getCountryHtmlSelect());
  170. }
  171. /**
  172. * 1. Store code
  173. * 2. Default Country ID
  174. * 3. Top Destinations
  175. * 4. Exploded Top Destinations
  176. * 5. Result options
  177. *
  178. * @return array
  179. */
  180. public function dataProviderGetCountryHtmlSelect()
  181. {
  182. return [
  183. [
  184. 'default',
  185. 1,
  186. '',
  187. [],
  188. [
  189. [
  190. 'value' => 'US',
  191. 'label' => 'United States',
  192. ],
  193. ],
  194. 'result html',
  195. ],
  196. [
  197. 'default',
  198. 1,
  199. 'US',
  200. [
  201. 0 => 'US',
  202. ],
  203. [
  204. [
  205. 'value' => 'US',
  206. 'label' => 'United States',
  207. ],
  208. ],
  209. 'result html',
  210. ],
  211. [
  212. 'default',
  213. 1,
  214. 'US,GB',
  215. [
  216. 0 => 'US',
  217. 1 => 'GB',
  218. ],
  219. [
  220. [
  221. 'value' => 'US',
  222. 'label' => 'United States',
  223. ],
  224. [
  225. 'value' => 'GB',
  226. 'label' => 'Great Britain',
  227. ],
  228. ],
  229. 'result html',
  230. ],
  231. ];
  232. }
  233. /**
  234. * @param $defaultCountry
  235. * @param $options
  236. * @param $resultHtml
  237. * @return \PHPUnit_Framework_MockObject_MockObject
  238. */
  239. protected function mockElementHtmlSelect($defaultCountry, $options, $resultHtml)
  240. {
  241. $name = 'country_id';
  242. $id = 'country';
  243. $title = 'Country';
  244. $elementHtmlSelect = $this->getMockBuilder(\Magento\Framework\View\Element\Html\Select::class)
  245. ->disableOriginalConstructor()
  246. ->setMethods([
  247. 'setName',
  248. 'setId',
  249. 'setTitle',
  250. 'setValue',
  251. 'setOptions',
  252. 'setExtraParams',
  253. 'getHtml',
  254. ])
  255. ->getMock();
  256. $elementHtmlSelect->expects($this->once())
  257. ->method('setName')
  258. ->with($name)
  259. ->willReturnSelf();
  260. $elementHtmlSelect->expects($this->once())
  261. ->method('setId')
  262. ->with($id)
  263. ->willReturnSelf();
  264. $elementHtmlSelect->expects($this->once())
  265. ->method('setTitle')
  266. ->with(__($title))
  267. ->willReturnSelf();
  268. $elementHtmlSelect->expects($this->once())
  269. ->method('setValue')
  270. ->with($defaultCountry)
  271. ->willReturnSelf();
  272. $elementHtmlSelect->expects($this->once())
  273. ->method('setOptions')
  274. ->with($options)
  275. ->willReturnSelf();
  276. $elementHtmlSelect->expects($this->once())
  277. ->method('setExtraParams')
  278. ->with('data-validate="{\'validate-select\':true}"')
  279. ->willReturnSelf();
  280. $elementHtmlSelect->expects($this->once())
  281. ->method('getHtml')
  282. ->willReturn($resultHtml);
  283. return $elementHtmlSelect;
  284. }
  285. }