OptionsTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Locale\Test\Unit\Deployed;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\App\State;
  10. use Magento\Framework\Config\ConfigOptionsListConstants as Constants;
  11. use Magento\Framework\Locale\AvailableLocalesInterface;
  12. use Magento\Framework\Locale\Deployed\Options;
  13. use Magento\Framework\Locale\ListsInterface;
  14. use Magento\Framework\View\Design\ThemeInterface;
  15. use Magento\Framework\View\DesignInterface;
  16. use \PHPUnit_Framework_MockObject_MockObject as MockObject;
  17. /**
  18. * Test for Options class.
  19. *
  20. * @see Options
  21. */
  22. class OptionsTest extends \PHPUnit\Framework\TestCase
  23. {
  24. /**
  25. * @var State|MockObject
  26. */
  27. private $stateMock;
  28. /**
  29. * @var AvailableLocalesInterface|MockObject
  30. */
  31. private $availableLocalesMock;
  32. /**
  33. * @var DesignInterface|MockObject
  34. */
  35. private $designMock;
  36. /**
  37. * @var ListsInterface|MockObject
  38. */
  39. private $localeListsMock;
  40. /**
  41. * @var DeploymentConfig|MockObject
  42. */
  43. private $deploymentConfigMock;
  44. /**
  45. * @var Options
  46. */
  47. private $model;
  48. /**
  49. * @inheritdoc
  50. */
  51. protected function setUp()
  52. {
  53. $this->stateMock = $this->getMockBuilder(State::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->availableLocalesMock = $this->getMockBuilder(AvailableLocalesInterface::class)
  57. ->getMockForAbstractClass();
  58. $this->designMock = $this->getMockBuilder(DesignInterface::class)
  59. ->getMockForAbstractClass();
  60. $this->localeListsMock = $this->getMockBuilder(ListsInterface::class)
  61. ->getMockForAbstractClass();
  62. $this->deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
  63. $this->model = new Options(
  64. $this->localeListsMock,
  65. $this->stateMock,
  66. $this->availableLocalesMock,
  67. $this->designMock,
  68. $this->deploymentConfigMock
  69. );
  70. }
  71. /**
  72. * @param string $mode
  73. * @param int $scdOnDemand
  74. * @param array $locales
  75. * @return void
  76. *
  77. * @dataProvider getFullLocalesDataProvider
  78. */
  79. public function testGetOptionLocalesFull(string $mode, int $scdOnDemand, array $locales): void
  80. {
  81. $this->localeListsMock->expects($this->once())
  82. ->method('getOptionLocales')
  83. ->willReturn($locales);
  84. $this->prepareGetLocalesFull($mode, $scdOnDemand);
  85. $this->assertEquals($locales, array_values($this->model->getOptionLocales()));
  86. }
  87. /**
  88. * @param string $mode
  89. * @param int $scdOnDemand
  90. * @param array $locales
  91. * @return void
  92. *
  93. * @dataProvider getFullLocalesDataProvider
  94. */
  95. public function testGetTranslatedOptionLocalesFull(string $mode, int $scdOnDemand, array $locales): void
  96. {
  97. $this->localeListsMock->expects($this->once())
  98. ->method('getTranslatedOptionLocales')
  99. ->willReturn($locales);
  100. $this->prepareGetLocalesFull($mode, $scdOnDemand);
  101. $this->assertEquals($locales, array_values($this->model->getTranslatedOptionLocales()));
  102. }
  103. /**
  104. * @param string $mode
  105. * @param int $scdOnDemand
  106. * @param array $locales
  107. * @param array $expectedLocales
  108. * @param array $deployedCodes
  109. * @return void
  110. *
  111. * @dataProvider getLimitedLocalesDataProvider
  112. */
  113. public function testGetOptionLocalesLimited(
  114. string $mode,
  115. int $scdOnDemand,
  116. array $locales,
  117. array $expectedLocales,
  118. array $deployedCodes
  119. ): void {
  120. $this->localeListsMock->expects($this->once())
  121. ->method('getOptionLocales')
  122. ->willReturn($locales);
  123. $this->prepareGetLocalesLimited($mode, $scdOnDemand, $deployedCodes);
  124. $this->assertEquals($expectedLocales, array_values($this->model->getOptionLocales()));
  125. }
  126. /**
  127. * @param string $mode
  128. * @param int $scdOnDemand
  129. * @param array $locales
  130. * @param array $expectedLocales
  131. * @param array $deployedCodes
  132. * @return void
  133. *
  134. * @dataProvider getLimitedLocalesDataProvider
  135. */
  136. public function testGetTranslatedOptionLocalesLimited(
  137. string $mode,
  138. int $scdOnDemand,
  139. array $locales,
  140. array $expectedLocales,
  141. array $deployedCodes
  142. ): void {
  143. $this->localeListsMock->expects($this->once())
  144. ->method('getTranslatedOptionLocales')
  145. ->willReturn($locales);
  146. $this->prepareGetLocalesLimited($mode, $scdOnDemand, $deployedCodes);
  147. $this->assertEquals($expectedLocales, array_values($this->model->getTranslatedOptionLocales()));
  148. }
  149. /**
  150. * @param string $mode
  151. * @param int $scdOnDemand
  152. * @param array $deployedCodes
  153. * @return void
  154. */
  155. private function prepareGetLocalesLimited(string $mode, int $scdOnDemand, $deployedCodes): void
  156. {
  157. $this->stateMock->expects($this->once())
  158. ->method('getMode')
  159. ->willReturn($mode);
  160. $this->deploymentConfigMock->expects($this->any())
  161. ->method('getConfigData')
  162. ->with(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)
  163. ->willReturn($scdOnDemand);
  164. $area = 'area';
  165. $code = 'code';
  166. $themeMock = $this->getMockBuilder(ThemeInterface::class)
  167. ->getMockForAbstractClass();
  168. $themeMock->expects($this->once())
  169. ->method('getCode')
  170. ->willReturn($code);
  171. $themeMock->expects($this->once())
  172. ->method('getArea')
  173. ->willReturn($area);
  174. $this->designMock->expects($this->once())
  175. ->method('getDesignTheme')
  176. ->willReturn($themeMock);
  177. $this->availableLocalesMock->expects($this->once())
  178. ->method('getList')
  179. ->with($code, $area)
  180. ->willReturn($deployedCodes);
  181. }
  182. /**
  183. * @param string $mode
  184. * @param int $scdOnDemand
  185. * @return void
  186. */
  187. private function prepareGetLocalesFull(string $mode, int $scdOnDemand): void
  188. {
  189. $this->stateMock->expects($this->once())
  190. ->method('getMode')
  191. ->willReturn($mode);
  192. $this->deploymentConfigMock->expects($this->any())
  193. ->method('getConfigData')
  194. ->with(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)
  195. ->willReturn($scdOnDemand);
  196. $this->designMock->expects($this->never())
  197. ->method('getDesignTheme');
  198. }
  199. /**
  200. * @return array
  201. */
  202. public function getFullLocalesDataProvider(): array
  203. {
  204. $deLocale = [
  205. 'value' => 'de_DE',
  206. 'label' => 'German (German)'
  207. ];
  208. $daLocale = [
  209. 'value' => 'da_DK',
  210. 'label' => 'Danish (Denmark)'
  211. ];
  212. return [
  213. [
  214. State::MODE_PRODUCTION,
  215. 1,
  216. [$daLocale, $deLocale],
  217. ],
  218. [
  219. State::MODE_DEVELOPER,
  220. 0,
  221. [$daLocale, $deLocale],
  222. ],
  223. [
  224. State::MODE_DEVELOPER,
  225. 1,
  226. [$deLocale],
  227. ],
  228. [
  229. State::MODE_DEFAULT,
  230. 0,
  231. [$daLocale],
  232. ],
  233. [
  234. State::MODE_DEFAULT,
  235. 1,
  236. [$daLocale, $deLocale],
  237. ],
  238. ];
  239. }
  240. /**
  241. * @return array
  242. */
  243. public function getLimitedLocalesDataProvider(): array
  244. {
  245. $deLocale = [
  246. 'value' => 'de_DE',
  247. 'label' => 'German (German)'
  248. ];
  249. $daLocale = [
  250. 'value' => 'da_DK',
  251. 'label' => 'Danish (Denmark)'
  252. ];
  253. return [
  254. [
  255. State::MODE_PRODUCTION,
  256. 0,
  257. [
  258. $daLocale,
  259. $deLocale
  260. ],
  261. [
  262. $deLocale
  263. ],
  264. [
  265. 'de_DE'
  266. ]
  267. ],
  268. [
  269. State::MODE_PRODUCTION,
  270. 0,
  271. [
  272. $daLocale,
  273. $deLocale
  274. ],
  275. [
  276. $daLocale,
  277. $deLocale
  278. ],
  279. [
  280. 'da_DK',
  281. 'de_DE'
  282. ]
  283. ],
  284. ];
  285. }
  286. }