StateTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit;
  7. use \Magento\Framework\App\Area;
  8. use \Magento\Framework\App\AreaList;
  9. use \Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  10. class StateTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\App\State
  14. */
  15. protected $model;
  16. /**
  17. * @var \Magento\Framework\Config\ScopeInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $scopeMock;
  20. /**
  21. * @var AreaList|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $areaListMock;
  24. protected function setUp()
  25. {
  26. $objectManager = new ObjectManagerHelper($this);
  27. $this->scopeMock = $this->getMockForAbstractClass(
  28. \Magento\Framework\Config\ScopeInterface::class,
  29. ['setCurrentScope'],
  30. '',
  31. false
  32. );
  33. $this->areaListMock = $this->createMock(AreaList::class);
  34. $this->areaListMock->expects($this->any())
  35. ->method('getCodes')
  36. ->willReturn([Area::AREA_ADMINHTML, Area::AREA_FRONTEND]);
  37. $this->model = $objectManager->getObject(
  38. \Magento\Framework\App\State::class,
  39. ['configScope' => $this->scopeMock]
  40. );
  41. $objectManager->setBackwardCompatibleProperty($this->model, 'areaList', $this->areaListMock);
  42. }
  43. public function testSetAreaCode()
  44. {
  45. $areaCode = Area::AREA_FRONTEND;
  46. $this->scopeMock->expects($this->once())->method('setCurrentScope')->with($areaCode);
  47. $this->model->setAreaCode($areaCode);
  48. $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
  49. $this->model->setAreaCode(Area::AREA_ADMINHTML);
  50. }
  51. public function testGetAreaCodeException()
  52. {
  53. $this->scopeMock->expects($this->never())->method('setCurrentScope');
  54. $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
  55. $this->model->getAreaCode();
  56. }
  57. public function testGetAreaCode()
  58. {
  59. $areaCode = Area::AREA_FRONTEND;
  60. $this->scopeMock->expects($this->once())->method('setCurrentScope')->with($areaCode);
  61. $this->model->setAreaCode($areaCode);
  62. $this->assertEquals($areaCode, $this->model->getAreaCode());
  63. }
  64. public function testEmulateAreaCode()
  65. {
  66. $areaCode = Area::AREA_FRONTEND;
  67. $emulatedCode = Area::AREA_ADMINHTML;
  68. $this->scopeMock->expects($this->once())->method('setCurrentScope')->with($areaCode);
  69. $this->model->setAreaCode($areaCode);
  70. $this->assertEquals(
  71. $emulatedCode,
  72. $this->model->emulateAreaCode($emulatedCode, [$this, 'emulateAreaCodeCallback'])
  73. );
  74. $this->assertEquals($this->model->getAreaCode(), $areaCode);
  75. }
  76. /**
  77. * @return string
  78. * @throws \Magento\Framework\Exception\LocalizedException
  79. */
  80. public function emulateAreaCodeCallback()
  81. {
  82. return $this->model->getAreaCode();
  83. }
  84. public function testIsAreaCodeEmulated()
  85. {
  86. $areaCode = Area::AREA_ADMINHTML;
  87. $emulatedCode = Area::AREA_FRONTEND;
  88. $this->scopeMock->expects($this->once())->method('setCurrentScope')->with($areaCode);
  89. $this->model->setAreaCode($areaCode);
  90. $this->assertFalse(
  91. $this->model->isAreaCodeEmulated(),
  92. 'By default, area code is not emulated'
  93. );
  94. $this->assertTrue(
  95. $this->model->emulateAreaCode($emulatedCode, [$this, 'isAreaCodeEmulatedCallback']),
  96. 'isAreaCodeEmulated should return true when being called within the context of an emulated method'
  97. );
  98. $this->assertFalse(
  99. $this->model->isAreaCodeEmulated(),
  100. 'Now that emulateAreaCode execution has finished, this should return false again'
  101. );
  102. }
  103. /**
  104. * Used to test whether the isAreaCodeEmulated method returns true within an emulated context
  105. *
  106. * @return bool
  107. */
  108. public function isAreaCodeEmulatedCallback()
  109. {
  110. return $this->model->isAreaCodeEmulated();
  111. }
  112. /**
  113. * @expectedException \Exception
  114. * @expectedExceptionMessage Some error
  115. */
  116. public function testEmulateAreaCodeException()
  117. {
  118. $areaCode = Area::AREA_FRONTEND;
  119. $emulatedCode = Area::AREA_ADMINHTML;
  120. $this->scopeMock->expects($this->once())->method('setCurrentScope')->with($areaCode);
  121. $this->model->setAreaCode($areaCode);
  122. $this->model->emulateAreaCode($emulatedCode, [$this, 'emulateAreaCodeCallbackException']);
  123. $this->assertEquals($this->model->getAreaCode(), $areaCode);
  124. }
  125. public function emulateAreaCodeCallbackException()
  126. {
  127. throw new \Exception('Some error');
  128. }
  129. /**
  130. * @param string $mode
  131. * @dataProvider constructorDataProvider
  132. */
  133. public function testConstructor($mode)
  134. {
  135. $model = new \Magento\Framework\App\State(
  136. $this->getMockForAbstractClass(\Magento\Framework\Config\ScopeInterface::class, [], '', false),
  137. $mode
  138. );
  139. $this->assertEquals($mode, $model->getMode());
  140. }
  141. /**
  142. * @return array
  143. */
  144. public static function constructorDataProvider()
  145. {
  146. return [
  147. 'default mode' => [\Magento\Framework\App\State::MODE_DEFAULT],
  148. 'production mode' => [\Magento\Framework\App\State::MODE_PRODUCTION],
  149. 'developer mode' => [\Magento\Framework\App\State::MODE_DEVELOPER]
  150. ];
  151. }
  152. /**
  153. * @expectedException \Exception
  154. * @expectedExceptionMessage Unknown application mode: unknown mode
  155. */
  156. public function testConstructorException()
  157. {
  158. new \Magento\Framework\App\State(
  159. $this->getMockForAbstractClass(\Magento\Framework\Config\ScopeInterface::class, [], '', false),
  160. "unknown mode"
  161. );
  162. }
  163. /**
  164. * @expectedException \Magento\Framework\Exception\LocalizedException
  165. * @expectedExceptionMessage Area code "any code" does not exist
  166. */
  167. public function testCheckAreaCodeException()
  168. {
  169. $this->model->setAreaCode('any code');
  170. }
  171. }