CurrencysymbolTest.php 12 KB

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