EsiTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 EsiTest 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 \Magento\Framework\View\Layout|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $layoutMock;
  33. /**
  34. * @var \Magento\Framework\View\Layout\LayoutCacheKeyInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $layoutCacheKeyMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Translate\InlineInterface
  39. */
  40. protected $translateInline;
  41. /**
  42. * Set up before test
  43. */
  44. protected function setUp()
  45. {
  46. $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
  47. ->disableOriginalConstructor()->getMock();
  48. $this->layoutCacheKeyMock = $this->getMockForAbstractClass(
  49. \Magento\Framework\View\Layout\LayoutCacheKeyInterface::class
  50. );
  51. $contextMock =
  52. $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
  53. ->disableOriginalConstructor()->getMock();
  54. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  55. ->disableOriginalConstructor()->getMock();
  56. $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
  57. ->disableOriginalConstructor()->getMock();
  58. $this->viewMock = $this->getMockBuilder(\Magento\Framework\App\View::class)
  59. ->disableOriginalConstructor()->getMock();
  60. $contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
  61. $contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
  62. $contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock));
  63. $this->translateInline = $this->createMock(\Magento\Framework\Translate\InlineInterface::class);
  64. $helperObjectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  65. $this->action = $helperObjectManager->getObject(
  66. \Magento\PageCache\Controller\Block\Esi::class,
  67. [
  68. 'context' => $contextMock,
  69. 'translateInline' => $this->translateInline,
  70. 'jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Json(),
  71. 'base64jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Base64Json(),
  72. 'layoutCacheKey' => $this->layoutCacheKeyMock
  73. ]
  74. );
  75. }
  76. /**
  77. * @dataProvider executeDataProvider
  78. * @param string $blockClass
  79. * @param bool $shouldSetHeaders
  80. */
  81. public function testExecute($blockClass, $shouldSetHeaders)
  82. {
  83. $block = 'block';
  84. $handles = ['handle1', 'handle2'];
  85. $html = 'some-html';
  86. $mapData = [['blocks', '', json_encode([$block])], ['handles', '', base64_encode(json_encode($handles))]];
  87. $blockInstance1 = $this->createPartialMock($blockClass, ['toHtml']);
  88. $blockInstance1->expects($this->once())->method('toHtml')->will($this->returnValue($html));
  89. $blockInstance1->setTtl(360);
  90. $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap($mapData));
  91. $this->viewMock->expects($this->once())->method('loadLayout')->with($this->equalTo($handles));
  92. $this->viewMock->expects($this->once())->method('getLayout')->will($this->returnValue($this->layoutMock));
  93. $this->layoutMock->expects($this->never())
  94. ->method('getUpdate');
  95. $this->layoutCacheKeyMock->expects($this->atLeastOnce())
  96. ->method('addCacheKeys');
  97. $this->layoutMock->expects($this->once())
  98. ->method('getBlock')
  99. ->with($this->equalTo($block))
  100. ->will($this->returnValue($blockInstance1));
  101. if ($shouldSetHeaders) {
  102. $this->responseMock->expects($this->once())
  103. ->method('setHeader')
  104. ->with('X-Magento-Tags', implode(',', $blockInstance1->getIdentities()));
  105. } else {
  106. $this->responseMock->expects($this->never())
  107. ->method('setHeader');
  108. }
  109. $this->translateInline->expects($this->once())
  110. ->method('processResponseBody')
  111. ->with($html)
  112. ->willReturnSelf();
  113. $this->responseMock->expects($this->once())
  114. ->method('appendBody')
  115. ->with($this->equalTo($html));
  116. $this->action->execute();
  117. }
  118. /**
  119. * @return array
  120. */
  121. public function executeDataProvider()
  122. {
  123. return [
  124. [\Magento\PageCache\Test\Unit\Block\Controller\StubBlock::class, true],
  125. [\Magento\Framework\View\Element\AbstractBlock::class, false],
  126. ];
  127. }
  128. public function testExecuteBlockNotExists()
  129. {
  130. $handles = json_encode(['handle1', 'handle2']);
  131. $mapData = [
  132. ['blocks', '', null],
  133. ['handles', '', $handles],
  134. ];
  135. $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap($mapData));
  136. $this->viewMock->expects($this->never())->method('getLayout')->will($this->returnValue($this->layoutMock));
  137. $this->action->execute();
  138. }
  139. }