EmulationTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Tests Magento\Store\Model\App\Emulation
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Store\Test\Unit\Model\App;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class EmulationTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManagerInterface
  16. */
  17. private $storeManagerMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\TranslateInterface
  20. */
  21. private $translateMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Config\ScopeConfigInterface
  24. */
  25. private $scopeConfigMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Locale\ResolverInterface
  28. */
  29. private $localeResolverMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Theme\Model\Design
  32. */
  33. private $designMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Translate\Inline\ConfigInterface
  36. */
  37. private $inlineConfigMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Translate\Inline\StateInterface
  40. */
  41. private $inlineTranslationMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\DesignInterface
  44. */
  45. private $viewDesignMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store
  48. */
  49. private $storeMock;
  50. /**
  51. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  52. */
  53. private $objectManager;
  54. /**
  55. * New store id
  56. */
  57. const NEW_STORE_ID = 9;
  58. /**
  59. * @var \Magento\Store\Model\App\Emulation
  60. */
  61. private $model;
  62. protected function setUp()
  63. {
  64. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  65. // Mocks
  66. $this->designMock = $this->getMockBuilder(\Magento\Theme\Model\Design::class)
  67. ->disableOriginalConstructor()
  68. ->setMethods([])->getMock();
  69. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  70. ->disableOriginalConstructor()
  71. ->setMethods([])->getMock();
  72. $this->translateMock = $this->getMockBuilder(\Magento\Framework\TranslateInterface::class)
  73. ->disableOriginalConstructor()
  74. ->setMethods([])->getMock();
  75. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  76. ->disableOriginalConstructor()
  77. ->setMethods([])->getMock();
  78. $this->localeResolverMock = $this->getMockBuilder(\Magento\Framework\Locale\ResolverInterface::class)
  79. ->disableOriginalConstructor()
  80. ->setMethods([])->getMock();
  81. $this->inlineConfigMock = $this->getMockBuilder(\Magento\Framework\Translate\Inline\ConfigInterface::class)
  82. ->disableOriginalConstructor()
  83. ->setMethods([])->getMock();
  84. $this->inlineTranslationMock = $this->getMockBuilder(\Magento\Framework\Translate\Inline\StateInterface::class)
  85. ->disableOriginalConstructor()
  86. ->setMethods([])->getMock();
  87. $this->viewDesignMock = $this->getMockForAbstractClass(\Magento\Framework\View\DesignInterface::class);
  88. $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  89. ->disableOriginalConstructor()
  90. ->setMethods(['__wakeup', 'getStoreId'])
  91. ->getMock();
  92. // Stubs
  93. $this->designMock->expects($this->any())->method('loadChange')->willReturnSelf();
  94. $this->designMock->expects($this->any())->method('getData')->willReturn(false);
  95. // Prepare SUT
  96. $this->model = $this->objectManager->getObject(
  97. \Magento\Store\Model\App\Emulation::class,
  98. [
  99. 'storeManager' => $this->storeManagerMock,
  100. 'viewDesign' => $this->viewDesignMock,
  101. 'design' => $this->designMock,
  102. 'translate' => $this->translateMock,
  103. 'scopeConfig' => $this->scopeConfigMock,
  104. 'inlineConfig' => $this->inlineConfigMock,
  105. 'inlineTranslation' => $this->inlineTranslationMock,
  106. 'localeResolver' => $this->localeResolverMock,
  107. ]
  108. );
  109. }
  110. public function testStartDefaults()
  111. {
  112. // Test data
  113. $inlineTranslate = false;
  114. $initArea = 'initial area';
  115. $initTheme = 'initial design theme';
  116. $initStore = 1;
  117. $initLocale = 'initial locale code';
  118. $newInlineTranslate = false;
  119. $newLocale = 'new locale code';
  120. $newArea = \Magento\Framework\App\Area::AREA_FRONTEND;
  121. // Stubs
  122. $this->inlineTranslationMock->expects($this->any())->method('isEnabled')->willReturn($inlineTranslate);
  123. $this->viewDesignMock->expects($this->any())->method('getArea')->willReturn($initArea);
  124. $this->viewDesignMock->expects($this->any())->method('getDesignTheme')->willReturn($initTheme);
  125. $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($this->storeMock);
  126. $this->storeMock->expects($this->any())
  127. ->method('getStoreId')->willReturn($initStore);
  128. $this->localeResolverMock->expects($this->any())->method('getLocale')->willReturn($initLocale);
  129. $this->inlineConfigMock->expects($this->any())->method('isActive')->willReturn($newInlineTranslate);
  130. $this->viewDesignMock->expects($this->any())->method('getConfigurationDesignTheme')->willReturn($initTheme);
  131. $this->scopeConfigMock->expects($this->any())->method('getValue')->willReturn($newLocale);
  132. // Expectations
  133. $this->storeMock->expects($this->any())->method('getStoreId')->willReturn($initStore);
  134. $this->inlineTranslationMock->expects($this->any())->method('suspend')->with($newInlineTranslate);
  135. $this->viewDesignMock->expects($this->any())->method('setDesignTheme')->with($initTheme);
  136. $this->localeResolverMock->expects($this->any())->method('setLocale')->with($newLocale);
  137. $this->translateMock->expects($this->any())->method('setLocale')->with($newLocale);
  138. $this->translateMock->expects($this->any())->method('loadData')->with($newArea);
  139. $this->storeManagerMock->expects($this->any())
  140. ->method('setCurrentStore')->with(self::NEW_STORE_ID);
  141. // Test
  142. $result = $this->model->startEnvironmentEmulation(
  143. self::NEW_STORE_ID,
  144. \Magento\Framework\App\Area::AREA_FRONTEND
  145. );
  146. $this->assertNull($result);
  147. }
  148. public function testStop()
  149. {
  150. // Test data
  151. $initArea = 'initial area';
  152. $initTheme = 'initial design theme';
  153. $initLocale = 'initial locale code';
  154. $initialStore = 1;
  155. $initTranslateInline = false;
  156. $this->inlineTranslationMock->expects($this->once())
  157. ->method('isEnabled')
  158. ->willReturn($initTranslateInline);
  159. $this->viewDesignMock->expects($this->once())
  160. ->method('getArea')
  161. ->willReturn($initArea);
  162. $this->viewDesignMock->expects($this->once())
  163. ->method('getDesignTheme')
  164. ->willReturn($initTheme);
  165. $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($this->storeMock);
  166. $this->storeMock->expects($this->once())->method('getStoreId')->willReturn($initialStore);
  167. $this->localeResolverMock->expects($this->once())
  168. ->method('getLocale')
  169. ->willReturn($initLocale);
  170. $this->model->storeCurrentEnvironmentInfo();
  171. // Expectations
  172. $this->inlineTranslationMock->expects($this->once())
  173. ->method('resume')
  174. ->with($initTranslateInline);
  175. $this->viewDesignMock->expects($this->once())
  176. ->method('setDesignTheme')
  177. ->with($initTheme, $initArea);
  178. $this->storeManagerMock->expects($this->once())
  179. ->method('setCurrentStore')->with($initialStore);
  180. $this->localeResolverMock->expects($this->once())
  181. ->method('setLocale')
  182. ->with($initLocale);
  183. $this->translateMock->expects($this->once())
  184. ->method('setLocale')
  185. ->with($initLocale);
  186. $this->translateMock->expects($this->once())->method('loadData')->with($initArea);
  187. // Test
  188. $result = $this->model->stopEnvironmentEmulation();
  189. $this->assertNotNull($result);
  190. }
  191. }