RenderTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\PageCache\Test\Unit\Controller\Block;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class RenderTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $requestMock;
  17. /**
  18. * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $responseMock;
  21. /**
  22. * @var \Magento\Framework\App\View|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $viewMock;
  25. /**
  26. * @var \Magento\PageCache\Controller\Block
  27. */
  28. protected $action;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Translate\InlineInterface
  31. */
  32. protected $translateInline;
  33. /**
  34. * @var \Magento\Framework\View\Layout|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $layoutMock;
  37. /**
  38. * @var \Magento\Framework\View\Layout\ProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $layoutProcessorMock;
  41. /**
  42. * @var \Magento\Framework\View\Layout\LayoutCacheKeyInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $layoutCacheKeyMock;
  45. /**
  46. * Set up before test
  47. */
  48. protected function setUp()
  49. {
  50. $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
  51. ->disableOriginalConstructor()->getMock();
  52. $this->layoutProcessorMock = $this->getMockForAbstractClass(
  53. \Magento\Framework\View\Layout\ProcessorInterface::class
  54. );
  55. $this->layoutCacheKeyMock = $this->getMockForAbstractClass(
  56. \Magento\Framework\View\Layout\LayoutCacheKeyInterface::class
  57. );
  58. $contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
  59. ->disableOriginalConstructor()->getMock();
  60. $this->requestMock = $this->getMockBuilder(
  61. \Magento\Framework\App\Request\Http::class
  62. )->disableOriginalConstructor()->getMock();
  63. $this->responseMock = $this->getMockBuilder(
  64. \Magento\Framework\App\Response\Http::class
  65. )->disableOriginalConstructor()->getMock();
  66. $this->viewMock = $this->getMockBuilder(\Magento\Framework\App\View::class)
  67. ->disableOriginalConstructor()->getMock();
  68. $this->layoutMock->expects($this->any())
  69. ->method('getUpdate')
  70. ->will($this->returnValue($this->layoutProcessorMock));
  71. $contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
  72. $contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
  73. $contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock));
  74. $this->translateInline = $this->createMock(\Magento\Framework\Translate\InlineInterface::class);
  75. $helperObjectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  76. $this->action = $helperObjectManager->getObject(
  77. \Magento\PageCache\Controller\Block\Render::class,
  78. [
  79. 'context' => $contextMock,
  80. 'translateInline' => $this->translateInline,
  81. 'jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Json(),
  82. 'base64jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Base64Json(),
  83. 'layoutCacheKey' => $this->layoutCacheKeyMock
  84. ]
  85. );
  86. }
  87. public function testExecuteNotAjax()
  88. {
  89. $this->requestMock->expects($this->once())->method('isAjax')->will($this->returnValue(false));
  90. $this->requestMock->expects($this->once())->method('setActionName')->will($this->returnValue('noroute'));
  91. $this->requestMock->expects($this->once())->method('setDispatched')->will($this->returnValue(false));
  92. $this->layoutCacheKeyMock->expects($this->never())
  93. ->method('addCacheKeys');
  94. $this->action->execute();
  95. }
  96. /**
  97. * Test no params: blocks, handles
  98. */
  99. public function testExecuteNoParams()
  100. {
  101. $this->requestMock->expects($this->once())->method('isAjax')->will($this->returnValue(true));
  102. $this->requestMock->expects($this->at(10))
  103. ->method('getParam')
  104. ->with($this->equalTo('blocks'), $this->equalTo(''))
  105. ->will($this->returnValue(''));
  106. $this->requestMock->expects($this->at(11))
  107. ->method('getParam')
  108. ->with($this->equalTo('handles'), $this->equalTo(''))
  109. ->will($this->returnValue(''));
  110. $this->layoutCacheKeyMock->expects($this->never())
  111. ->method('addCacheKeys');
  112. $this->action->execute();
  113. }
  114. public function testExecute()
  115. {
  116. $blocks = ['block1', 'block2'];
  117. $handles = ['handle1', 'handle2'];
  118. $originalRequest = '{"route":"route","controller":"controller","action":"action","uri":"uri"}';
  119. $expectedData = ['block1' => 'data1', 'block2' => 'data2'];
  120. $blockInstance1 = $this->createPartialMock(
  121. \Magento\PageCache\Test\Unit\Block\Controller\StubBlock::class,
  122. ['toHtml']
  123. );
  124. $blockInstance1->expects($this->once())->method('toHtml')->will($this->returnValue($expectedData['block1']));
  125. $blockInstance2 = $this->createPartialMock(
  126. \Magento\PageCache\Test\Unit\Block\Controller\StubBlock::class,
  127. ['toHtml']
  128. );
  129. $blockInstance2->expects($this->once())->method('toHtml')->will($this->returnValue($expectedData['block2']));
  130. $this->requestMock->expects($this->once())->method('isAjax')->will($this->returnValue(true));
  131. $this->requestMock->expects($this->at(1))
  132. ->method('getRouteName')
  133. ->will($this->returnValue('magento_pagecache'));
  134. $this->requestMock->expects($this->at(2))
  135. ->method('getControllerName')
  136. ->will($this->returnValue('block'));
  137. $this->requestMock->expects($this->at(3))
  138. ->method('getActionName')
  139. ->will($this->returnValue('render'));
  140. $this->requestMock->expects($this->at(4))
  141. ->method('getRequestUri')
  142. ->will($this->returnValue('uri'));
  143. $this->requestMock->expects($this->at(5))
  144. ->method('getParam')
  145. ->with($this->equalTo('originalRequest'))
  146. ->will($this->returnValue($originalRequest));
  147. $this->requestMock->expects($this->at(10))
  148. ->method('getParam')
  149. ->with($this->equalTo('blocks'), $this->equalTo(''))
  150. ->will($this->returnValue(json_encode($blocks)));
  151. $this->requestMock->expects($this->at(11))
  152. ->method('getParam')
  153. ->with($this->equalTo('handles'), $this->equalTo(''))
  154. ->will($this->returnValue(base64_encode(json_encode($handles))));
  155. $this->viewMock->expects($this->once())->method('loadLayout')->with($this->equalTo($handles));
  156. $this->viewMock->expects($this->any())->method('getLayout')->will($this->returnValue($this->layoutMock));
  157. $this->layoutMock->expects($this->never())
  158. ->method('getUpdate');
  159. $this->layoutCacheKeyMock->expects($this->atLeastOnce())
  160. ->method('addCacheKeys');
  161. $this->layoutMock->expects($this->at(0))
  162. ->method('getBlock')
  163. ->with($this->equalTo($blocks[0]))
  164. ->will($this->returnValue($blockInstance1));
  165. $this->layoutMock->expects($this->at(1))
  166. ->method('getBlock')
  167. ->with($this->equalTo($blocks[1]))
  168. ->will($this->returnValue($blockInstance2));
  169. $this->translateInline->expects($this->once())
  170. ->method('processResponseBody')
  171. ->with($expectedData)
  172. ->willReturnSelf();
  173. $this->responseMock->expects($this->once())
  174. ->method('appendBody')
  175. ->with($this->equalTo(json_encode($expectedData)));
  176. $this->action->execute();
  177. }
  178. }