Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EsiTest.php
Go to the documentation of this file.
1 <?php
9 
13 class EsiTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $requestMock;
19 
23  protected $responseMock;
24 
28  protected $viewMock;
29 
33  protected $action;
34 
38  protected $layoutMock;
39 
44 
48  protected $translateInline;
49 
53  protected function setUp()
54  {
55  $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
56  ->disableOriginalConstructor()->getMock();
57 
58  $this->layoutCacheKeyMock = $this->getMockForAbstractClass(
59  \Magento\Framework\View\Layout\LayoutCacheKeyInterface::class
60  );
61 
62  $contextMock =
63  $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
64  ->disableOriginalConstructor()->getMock();
65 
66  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
67  ->disableOriginalConstructor()->getMock();
68  $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
69  ->disableOriginalConstructor()->getMock();
70  $this->viewMock = $this->getMockBuilder(\Magento\Framework\App\View::class)
71  ->disableOriginalConstructor()->getMock();
72 
73  $contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
74  $contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
75  $contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock));
76 
77  $this->translateInline = $this->createMock(\Magento\Framework\Translate\InlineInterface::class);
78 
79  $helperObjectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
80  $this->action = $helperObjectManager->getObject(
81  \Magento\PageCache\Controller\Block\Esi::class,
82  [
83  'context' => $contextMock,
84  'translateInline' => $this->translateInline,
85  'jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Json(),
86  'base64jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Base64Json(),
87  'layoutCacheKey' => $this->layoutCacheKeyMock
88  ]
89  );
90  }
91 
97  public function testExecute($blockClass, $shouldSetHeaders)
98  {
99  $block = 'block';
100  $handles = ['handle1', 'handle2'];
101  $html = 'some-html';
102  $mapData = [['blocks', '', json_encode([$block])], ['handles', '', base64_encode(json_encode($handles))]];
103 
104  $blockInstance1 = $this->createPartialMock($blockClass, ['toHtml']);
105 
106  $blockInstance1->expects($this->once())->method('toHtml')->will($this->returnValue($html));
107  $blockInstance1->setTtl(360);
108 
109  $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap($mapData));
110 
111  $this->viewMock->expects($this->once())->method('loadLayout')->with($this->equalTo($handles));
112 
113  $this->viewMock->expects($this->once())->method('getLayout')->will($this->returnValue($this->layoutMock));
114 
115  $this->layoutMock->expects($this->never())
116  ->method('getUpdate');
117  $this->layoutCacheKeyMock->expects($this->atLeastOnce())
118  ->method('addCacheKeys');
119 
120  $this->layoutMock->expects($this->once())
121  ->method('getBlock')
122  ->with($this->equalTo($block))
123  ->will($this->returnValue($blockInstance1));
124 
125  if ($shouldSetHeaders) {
126  $this->responseMock->expects($this->once())
127  ->method('setHeader')
128  ->with('X-Magento-Tags', implode(',', $blockInstance1->getIdentities()));
129  } else {
130  $this->responseMock->expects($this->never())
131  ->method('setHeader');
132  }
133 
134  $this->translateInline->expects($this->once())
135  ->method('processResponseBody')
136  ->with($html)
137  ->willReturnSelf();
138 
139  $this->responseMock->expects($this->once())
140  ->method('appendBody')
141  ->with($this->equalTo($html));
142 
143  $this->action->execute();
144  }
145 
149  public function executeDataProvider()
150  {
151  return [
152  [\Magento\PageCache\Test\Unit\Block\Controller\StubBlock::class, true],
153  [\Magento\Framework\View\Element\AbstractBlock::class, false],
154  ];
155  }
156 
157  public function testExecuteBlockNotExists()
158  {
159  $handles = json_encode(['handle1', 'handle2']);
160  $mapData = [
161  ['blocks', '', null],
162  ['handles', '', $handles],
163  ];
164 
165  $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap($mapData));
166  $this->viewMock->expects($this->never())->method('getLayout')->will($this->returnValue($this->layoutMock));
167 
168  $this->action->execute();
169  }
170 }
$block
Definition: block.php:8
testExecute($blockClass, $shouldSetHeaders)
Definition: EsiTest.php:97