13 class EsiTest extends \PHPUnit\Framework\TestCase
55 $this->layoutMock = $this->getMockBuilder(\
Magento\Framework\View\Layout::class)
56 ->disableOriginalConstructor()->getMock();
58 $this->layoutCacheKeyMock = $this->getMockForAbstractClass(
59 \
Magento\Framework\View\Layout\LayoutCacheKeyInterface::class
63 $this->getMockBuilder(\
Magento\Framework\
App\Action\Context::class)
64 ->disableOriginalConstructor()->getMock();
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();
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));
77 $this->translateInline = $this->createMock(\
Magento\Framework\Translate\InlineInterface::class);
79 $helperObjectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
80 $this->action = $helperObjectManager->getObject(
81 \
Magento\PageCache\Controller\Block\Esi::class,
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
100 $handles = [
'handle1',
'handle2'];
102 $mapData = [[
'blocks',
'', json_encode([
$block])], [
'handles',
'', base64_encode(json_encode($handles))]];
104 $blockInstance1 = $this->createPartialMock($blockClass, [
'toHtml']);
106 $blockInstance1->expects($this->once())->method(
'toHtml')->will($this->returnValue($html));
107 $blockInstance1->setTtl(360);
109 $this->requestMock->expects($this->any())->method(
'getParam')->will($this->returnValueMap($mapData));
111 $this->viewMock->expects($this->once())->method(
'loadLayout')->with($this->equalTo($handles));
113 $this->viewMock->expects($this->once())->method(
'getLayout')->will($this->returnValue($this->layoutMock));
115 $this->layoutMock->expects($this->never())
116 ->method(
'getUpdate');
117 $this->layoutCacheKeyMock->expects($this->atLeastOnce())
118 ->method(
'addCacheKeys');
120 $this->layoutMock->expects($this->once())
122 ->with($this->equalTo(
$block))
123 ->will($this->returnValue($blockInstance1));
125 if ($shouldSetHeaders) {
126 $this->responseMock->expects($this->once())
127 ->method(
'setHeader')
128 ->with(
'X-Magento-Tags', implode(
',', $blockInstance1->getIdentities()));
130 $this->responseMock->expects($this->never())
131 ->method(
'setHeader');
134 $this->translateInline->expects($this->once())
135 ->method(
'processResponseBody')
139 $this->responseMock->expects($this->once())
140 ->method(
'appendBody')
141 ->with($this->equalTo($html));
143 $this->action->execute();
152 [\Magento\PageCache\Test\Unit\Block\Controller\StubBlock::class,
true],
153 [\Magento\Framework\View\Element\AbstractBlock::class,
false],
159 $handles = json_encode([
'handle1',
'handle2']);
161 [
'blocks',
'',
null],
162 [
'handles',
'', $handles],
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));
168 $this->action->execute();
testExecuteBlockNotExists()
testExecute($blockClass, $shouldSetHeaders)