ViewTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class ViewTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\App\View
  14. */
  15. protected $_view;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $_layoutMock;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $_configScopeMock;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $_requestMock;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $_layoutProcessor;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $_actionFlagMock;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $_eventManagerMock;
  40. /**
  41. * @var \Magento\Framework\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $resultPage;
  44. /**
  45. * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $response;
  48. protected function setUp()
  49. {
  50. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  51. $this->_layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
  52. $this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  53. $this->_configScopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
  54. $this->_layoutProcessor = $this->createMock(\Magento\Framework\View\Model\Layout\Merge::class);
  55. $this->_layoutMock->expects($this->any())->method('getUpdate')
  56. ->will($this->returnValue($this->_layoutProcessor));
  57. $this->_actionFlagMock = $this->createMock(\Magento\Framework\App\ActionFlag::class);
  58. $this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  59. $pageConfigMock = $this->getMockBuilder(
  60. \Magento\Framework\View\Page\Config::class
  61. )->disableOriginalConstructor()->getMock();
  62. $pageConfigMock->expects($this->any())
  63. ->method('publicBuild')
  64. ->willReturnSelf();
  65. $pageConfigRendererFactory = $this->getMockBuilder(\Magento\Framework\View\Page\Config\RendererFactory::class)
  66. ->disableOriginalConstructor()
  67. ->setMethods(['create'])
  68. ->getMock();
  69. $this->resultPage = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
  70. ->setConstructorArgs(
  71. $helper->getConstructArguments(\Magento\Framework\View\Result\Page::class, [
  72. 'request' => $this->_requestMock,
  73. 'pageConfigRendererFactory' => $pageConfigRendererFactory,
  74. 'layout' => $this->_layoutMock
  75. ])
  76. )
  77. ->setMethods(['renderResult', 'getConfig'])
  78. ->getMock();
  79. $this->resultPage->expects($this->any())
  80. ->method('getConfig')
  81. ->will($this->returnValue($pageConfigMock));
  82. $pageFactory = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
  83. ->disableOriginalConstructor()
  84. ->setMethods(['create'])
  85. ->getMock();
  86. $pageFactory->expects($this->once())
  87. ->method('create')
  88. ->will($this->returnValue($this->resultPage));
  89. $this->response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  90. $this->_view = $helper->getObject(
  91. \Magento\Framework\App\View::class,
  92. [
  93. 'layout' => $this->_layoutMock,
  94. 'request' => $this->_requestMock,
  95. 'response' => $this->response,
  96. 'configScope' => $this->_configScopeMock,
  97. 'eventManager' => $this->_eventManagerMock,
  98. 'actionFlag' => $this->_actionFlagMock,
  99. 'pageFactory' => $pageFactory
  100. ]
  101. );
  102. }
  103. public function testGetLayout()
  104. {
  105. $this->assertEquals($this->_layoutMock, $this->_view->getLayout());
  106. }
  107. /**
  108. * @expectedException \RuntimeException
  109. * @expectedExceptionMessage Layout must be loaded only once.
  110. */
  111. public function testLoadLayoutWhenLayoutAlreadyLoaded()
  112. {
  113. $this->_view->setIsLayoutLoaded(true);
  114. $this->_view->loadLayout();
  115. }
  116. public function testLoadLayoutWithDefaultSetup()
  117. {
  118. $this->_layoutProcessor->expects($this->at(0))->method('addHandle')->with('default');
  119. $this->_requestMock->expects(
  120. $this->any()
  121. )->method(
  122. 'getFullActionName'
  123. )->will(
  124. $this->returnValue('action_name')
  125. );
  126. $this->_view->loadLayout();
  127. }
  128. public function testLoadLayoutWhenBlocksNotGenerated()
  129. {
  130. $this->_view->loadLayout('', false, true);
  131. }
  132. public function testLoadLayoutWhenXmlNotGenerated()
  133. {
  134. $this->_view->loadLayout('', true, false);
  135. }
  136. public function testGetDefaultLayoutHandle()
  137. {
  138. $this->_requestMock->expects($this->once())
  139. ->method('getFullActionName')
  140. ->will($this->returnValue('ExpectedValue'));
  141. $this->assertEquals('expectedvalue', $this->_view->getDefaultLayoutHandle());
  142. }
  143. public function testAddActionLayoutHandlesWhenPageLayoutHandlesExist()
  144. {
  145. $this->_requestMock->expects($this->once())
  146. ->method('getFullActionName')
  147. ->will($this->returnValue('Full_Action_Name'));
  148. $this->_layoutProcessor->expects($this->once())
  149. ->method('addHandle')
  150. ->with('full_action_name');
  151. $this->_view->addActionLayoutHandles();
  152. }
  153. public function testAddPageLayoutHandles()
  154. {
  155. $pageHandles = ['full_action_name', 'full_action_name_key_value'];
  156. $this->_requestMock->expects($this->once())
  157. ->method('getFullActionName')
  158. ->will($this->returnValue('Full_Action_Name'));
  159. $this->_layoutProcessor->expects($this->once())
  160. ->method('addHandle')
  161. ->with($pageHandles);
  162. $this->_view->addPageLayoutHandles(['key' => 'value']);
  163. }
  164. public function testGenerateLayoutBlocksWhenFlagIsNotSet()
  165. {
  166. $valueMap = [
  167. ['', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH_BLOCK_EVENT, false],
  168. ['', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH_BLOCK_EVENT, false],
  169. ];
  170. $this->_actionFlagMock->expects($this->any())->method('get')->will($this->returnValueMap($valueMap));
  171. $this->_view->generateLayoutBlocks();
  172. }
  173. public function testGenerateLayoutBlocksWhenFlagIsSet()
  174. {
  175. $valueMap = [
  176. ['', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH_BLOCK_EVENT, true],
  177. ['', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH_BLOCK_EVENT, true],
  178. ];
  179. $this->_actionFlagMock->expects($this->any())->method('get')->will($this->returnValueMap($valueMap));
  180. $this->_eventManagerMock->expects($this->never())->method('dispatch');
  181. $this->_view->generateLayoutBlocks();
  182. }
  183. public function testRenderLayoutIfActionFlagExist()
  184. {
  185. $this->_actionFlagMock->expects(
  186. $this->once()
  187. )->method(
  188. 'get'
  189. )->with(
  190. '',
  191. 'no-renderLayout'
  192. )->will(
  193. $this->returnValue(true)
  194. );
  195. $this->_eventManagerMock->expects($this->never())->method('dispatch');
  196. $this->_view->renderLayout();
  197. }
  198. public function testRenderLayoutWhenOutputNotEmpty()
  199. {
  200. $this->_actionFlagMock->expects($this->once())
  201. ->method('get')
  202. ->with('', 'no-renderLayout')
  203. ->will($this->returnValue(false));
  204. $this->_layoutMock->expects($this->once())->method('addOutputElement')->with('output');
  205. $this->resultPage->expects($this->once())->method('renderResult')->with($this->response);
  206. $this->_view->renderLayout('output');
  207. }
  208. public function testRenderLayoutWhenOutputEmpty()
  209. {
  210. $this->_actionFlagMock->expects($this->once())
  211. ->method('get')
  212. ->with('', 'no-renderLayout')
  213. ->will($this->returnValue(false));
  214. $this->_layoutMock->expects($this->never())->method('addOutputElement');
  215. $this->resultPage->expects($this->once())->method('renderResult')->with($this->response);
  216. $this->_view->renderLayout();
  217. }
  218. }