PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/CurrencySymbol/Test/Unit/Model/System/CurrencysymbolTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 327 lines | 232 code | 32 blank | 63 comment | 0 complexity | 409b97fc9bd46b51961ab74c1245e90e MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CurrencySymbol\Test\Unit\Model\System;
  7. use Magento\CurrencySymbol\Model\System\Currencysymbol;
  8. use Magento\Store\Model\ScopeInterface;
  9. /**
  10. * Class CurrencysymbolTest
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class CurrencysymbolTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * Object manager helper
  18. *
  19. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  20. */
  21. protected $objectManagerHelper;
  22. /**
  23. * @var \Magento\Framework\Locale\ResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $localeResolverMock;
  26. /**
  27. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $scopeConfigMock;
  30. /**
  31. * @var \Magento\Store\Model\System\Store|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $systemStoreMock;
  34. /**
  35. * @var \Magento\Config\Model\Config\Factory|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $configFactoryMock;
  38. /**
  39. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $eventManagerMock;
  42. /**
  43. * @var \Magento\Framework\App\Config\ReinitableConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $coreConfigMock;
  46. /**
  47. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $storeManagerMock;
  50. /**
  51. * @var \Magento\Framework\App\Cache\TypeListInterface|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $cacheTypeListMock;
  54. /**
  55. * @var \Magento\CurrencySymbol\Model\System\Currencysymbol
  56. */
  57. protected $model;
  58. protected function setUp()
  59. {
  60. $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  61. $this->scopeConfigMock = $this->getMock(
  62. \Magento\Framework\App\Config\ScopeConfigInterface::class,
  63. ['getValue', 'isSetFlag'],
  64. [],
  65. '',
  66. false
  67. );
  68. $this->localeResolverMock = $this->getMock(
  69. \Magento\Framework\Locale\ResolverInterface::class,
  70. [
  71. 'getLocale',
  72. 'getDefaultLocalePath',
  73. 'setDefaultLocale',
  74. 'getDefaultLocale',
  75. 'setLocale',
  76. 'emulate',
  77. 'revert'
  78. ],
  79. [],
  80. '',
  81. false
  82. );
  83. $this->systemStoreMock = $this->getMock(
  84. \Magento\Store\Model\System\Store::class,
  85. ['getWebsiteCollection', 'getGroupCollection', 'getStoreCollection'],
  86. [],
  87. '',
  88. false
  89. );
  90. $this->configFactoryMock = $this->getMock(
  91. \Magento\Config\Model\Config\Factory::class,
  92. ['create'],
  93. [],
  94. '',
  95. false
  96. );
  97. $this->eventManagerMock = $this->getMock(
  98. \Magento\Framework\Event\ManagerInterface::class,
  99. ['dispatch'],
  100. [],
  101. '',
  102. false
  103. );
  104. $this->coreConfigMock = $this->getMock(
  105. \Magento\Framework\App\Config\ReinitableConfigInterface::class,
  106. ['reinit', 'setValue', 'getValue', 'isSetFlag'],
  107. [],
  108. '',
  109. false
  110. );
  111. $this->storeManagerMock = $this->getMock(
  112. \Magento\Store\Model\StoreManagerInterface::class,
  113. [],
  114. [],
  115. '',
  116. false
  117. );
  118. $this->cacheTypeListMock = $this->getMock(
  119. \Magento\Framework\App\Cache\TypeListInterface::class,
  120. [],
  121. [],
  122. '',
  123. false
  124. );
  125. $this->model = $this->objectManagerHelper->getObject(
  126. \Magento\CurrencySymbol\Model\System\Currencysymbol::class,
  127. [
  128. 'scopeConfig' => $this->scopeConfigMock,
  129. 'localeResolver' => $this->localeResolverMock,
  130. 'systemStore' => $this->systemStoreMock,
  131. 'configFactory' => $this->configFactoryMock,
  132. 'eventManager' => $this->eventManagerMock,
  133. 'coreConfig' => $this->coreConfigMock,
  134. 'storeManager' => $this->storeManagerMock,
  135. 'cacheTypeList' => $this->cacheTypeListMock,
  136. ]
  137. );
  138. }
  139. protected function tearDown()
  140. {
  141. unset($this->objectManagerHelper);
  142. }
  143. public function testGetCurrencySymbolData()
  144. {
  145. $expectedSymbolsData = [
  146. 'EUR' => [
  147. 'parentSymbol' => '€',
  148. 'displayName' => 'Euro',
  149. 'displaySymbol' => '€',
  150. 'inherited' => true
  151. ],
  152. 'USD' => [
  153. 'parentSymbol' => '$',
  154. 'displayName' => 'US Dollar',
  155. 'displaySymbol' => '$',
  156. 'inherited' => true
  157. ]
  158. ];
  159. $websiteId = 1;
  160. $groupId = 2;
  161. $currencies = 'USD,EUR';
  162. $this->prepareMocksForGetCurrencySymbolsData($websiteId, $groupId, $currencies);
  163. $this->assertEquals($expectedSymbolsData, $this->model->getCurrencySymbolsData());
  164. }
  165. public function testSetCurrencySymbolData()
  166. {
  167. $websiteId = 1;
  168. $groupId = 2;
  169. $currencies = 'USD,EUR';
  170. $symbols = [];
  171. $value['options']['fields']['customsymbol']['inherit'] = 1;
  172. $this->prepareMocksForGetCurrencySymbolsData($websiteId, $groupId, $currencies);
  173. /**
  174. * @var \Magento\Config\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  175. */
  176. $configMock = $this->getMock(
  177. \Magento\Config\Model\Config::class,
  178. ['setSection', 'setWebsite', 'setStore', 'setGroups', 'save'],
  179. [],
  180. '',
  181. false
  182. );
  183. $this->configFactoryMock->expects($this->any())->method('create')->willReturn($configMock);
  184. $configMock->expects($this->any())
  185. ->method('setSection')
  186. ->with(Currencysymbol::CONFIG_SECTION)
  187. ->willReturnSelf();
  188. $configMock->expects($this->any())->method('setWebsite')->with(null)->willReturnSelf();
  189. $configMock->expects($this->any())->method('setStore')->with(null)->willReturnSelf();
  190. $configMock->expects($this->any())->method('setGroups')->with($value)->willReturnSelf();
  191. $this->coreConfigMock->expects($this->once())->method('reinit');
  192. $this->cacheTypeListMock->expects($this->atLeastOnce())->method('invalidate');
  193. $this->eventManagerMock->expects($this->atLeastOnce())->method('dispatch')->willReturnMap(
  194. [
  195. ['admin_system_config_changed_section_currency_before_reinit', null, null],
  196. ['admin_system_config_changed_section_currency', null, null]
  197. ]
  198. );
  199. $this->assertInstanceOf(
  200. \Magento\CurrencySymbol\Model\System\Currencysymbol::class,
  201. $this->model->setCurrencySymbolsData($symbols)
  202. );
  203. }
  204. /**
  205. * @dataProvider getCurrencySymbolDataProvider
  206. */
  207. public function testGetCurrencySymbol($code, $expectedSymbol, $serializedCustomSymbols)
  208. {
  209. $this->scopeConfigMock->expects($this->any())
  210. ->method('getValue')
  211. ->willReturnMap(
  212. [
  213. [
  214. CurrencySymbol::XML_PATH_CUSTOM_CURRENCY_SYMBOL,
  215. ScopeInterface::SCOPE_STORE,
  216. null,
  217. $serializedCustomSymbols
  218. ],
  219. ]
  220. );
  221. $currencySymbol = $this->model->getCurrencySymbol($code);
  222. $this->assertEquals($expectedSymbol, $currencySymbol);
  223. }
  224. public function getCurrencySymbolDataProvider()
  225. {
  226. return [
  227. 'existentCustomSymbol' => [
  228. 'code' => 'USD',
  229. 'expectedSymbol' => '$',
  230. 'serializedCustomSymbols' => 'a:1:{s:3:"USD";s:1:"$";}'
  231. ],
  232. 'nonExistentCustomSymbol' => [
  233. 'code' => 'UAH',
  234. 'expectedSymbol' => false,
  235. 'serializedCustomSymbols' => 'a:1:{s:3:"USD";s:1:"$";}'
  236. ]
  237. ];
  238. }
  239. /**
  240. * Prepare mocks for getCurrencySymbolsData
  241. *
  242. * @param int $websiteId
  243. * @param int $groupId
  244. * @param string $currencies
  245. */
  246. protected function prepareMocksForGetCurrencySymbolsData($websiteId, $groupId, $currencies)
  247. {
  248. /**
  249. * @var \Magento\Store\Model\Website|\PHPUnit_Framework_MockObject_MockObject
  250. */
  251. $websiteMock = $this->getMock(\Magento\Store\Model\Website::class, ['getId', 'getConfig'], [], '', false);
  252. /**
  253. * @var \Magento\Store\Model\Group|\PHPUnit_Framework_MockObject_MockObject
  254. */
  255. $groupMock = $this->getMock(\Magento\Store\Model\Group::class, ['getId', 'getWebsiteId'], [], '', false);
  256. /**
  257. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  258. */
  259. $storeMock = $this->getMock(\Magento\Store\Model\Store::class, ['getGroupId'], [], '', false);
  260. $this->systemStoreMock->expects($this->once())
  261. ->method('getWebsiteCollection')
  262. ->willReturn([$websiteMock]);
  263. $this->systemStoreMock->expects($this->once())->method('getGroupCollection')->willReturn([$groupMock]);
  264. $this->systemStoreMock->expects($this->once())->method('getStoreCollection')->willReturn([$storeMock]);
  265. $websiteMock->expects($this->any())->method('getId')->willReturn($websiteId);
  266. $groupMock->expects($this->any())->method('getWebsiteId')->willReturn($websiteId);
  267. $groupMock->expects($this->any())->method('getId')->willReturn($groupId);
  268. $storeMock->expects($this->any())->method('getGroupId')->willReturn($groupId);
  269. $this->scopeConfigMock->expects($this->any())
  270. ->method('getValue')
  271. ->willReturnMap(
  272. [
  273. [CurrencySymbol::XML_PATH_CUSTOM_CURRENCY_SYMBOL, ScopeInterface::SCOPE_STORE, null, ''],
  274. [
  275. CurrencySymbol::XML_PATH_ALLOWED_CURRENCIES,
  276. ScopeInterface::SCOPE_STORE,
  277. $storeMock,
  278. $currencies
  279. ],
  280. [CurrencySymbol::XML_PATH_ALLOWED_CURRENCIES, ScopeInterface::SCOPE_STORE, null, $currencies],
  281. [
  282. CurrencySymbol::XML_PATH_ALLOWED_CURRENCIES,
  283. ScopeInterface::SCOPE_STORE,
  284. $storeMock,
  285. $currencies
  286. ]
  287. ]
  288. );
  289. $websiteMock->expects($this->any())
  290. ->method('getConfig')
  291. ->with(CurrencySymbol::XML_PATH_ALLOWED_CURRENCIES)
  292. ->willReturn($currencies);
  293. $this->localeResolverMock->expects($this->any())->method('getLocale')->willReturn('en');
  294. }
  295. }