Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BuiltinPluginTest.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class BuiltinPluginTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $plugin;
17 
21  protected $configMock;
22 
26  protected $versionMock;
27 
31  protected $kernelMock;
32 
36  protected $stateMock;
37 
41  protected $responseMock;
42 
47 
51  protected $closure;
52 
56  protected $requestMock;
57 
61  protected function setUp()
62  {
63  $this->configMock = $this->createMock(\Magento\PageCache\Model\Config::class);
64  $this->versionMock = $this->createMock(\Magento\Framework\App\PageCache\Version::class);
65  $this->kernelMock = $this->createMock(\Magento\Framework\App\PageCache\Kernel::class);
66  $this->stateMock = $this->createMock(\Magento\Framework\App\State::class);
67  $this->frontControllerMock = $this->createMock(\Magento\Framework\App\FrontControllerInterface::class);
68  $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
69  $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
71  $this->closure = function () use ($response) {
72  return $response;
73  };
74  $this->plugin = new \Magento\PageCache\Model\App\FrontController\BuiltinPlugin(
75  $this->configMock,
76  $this->versionMock,
77  $this->kernelMock,
78  $this->stateMock
79  );
80  }
81 
86  {
87  $header = \Zend\Http\Header\GenericHeader::fromString('Cache-Control: no-cache');
88  $this->configMock
89  ->expects($this->once())
90  ->method('getType')
91  ->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN));
92  $this->configMock->expects($this->once())
93  ->method('isEnabled')
94  ->will($this->returnValue(true));
95  $this->versionMock
96  ->expects($this->once())
97  ->method('process');
98  $this->kernelMock
99  ->expects($this->once())
100  ->method('load')
101  ->will($this->returnValue(false));
102  $this->stateMock->expects($this->any())
103  ->method('getMode')
104  ->will($this->returnValue($state));
105  if ($state == \Magento\Framework\App\State::MODE_DEVELOPER) {
106  $this->responseMock->expects($this->at(1))
107  ->method('setHeader')
108  ->with('X-Magento-Cache-Control');
109  $this->responseMock->expects($this->at(2))
110  ->method('setHeader')
111  ->with('X-Magento-Cache-Debug', 'MISS', true);
112  } else {
113  $this->responseMock->expects($this->never())
114  ->method('setHeader');
115  }
116  $this->responseMock
117  ->expects($this->once())
118  ->method('getHeader')
119  ->with('Cache-Control')
120  ->will($this->returnValue($header));
121  $this->kernelMock
122  ->expects($this->once())
123  ->method('process')
124  ->with($this->responseMock);
125  $this->assertSame(
126  $this->responseMock,
127  $this->plugin->aroundDispatch($this->frontControllerMock, $this->closure, $this->requestMock)
128  );
129  }
130 
135  {
136  $this->configMock
137  ->expects($this->once())
138  ->method('getType')
139  ->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN));
140  $this->configMock->expects($this->once())
141  ->method('isEnabled')
142  ->will($this->returnValue(true));
143  $this->versionMock
144  ->expects($this->once())
145  ->method('process');
146  $this->kernelMock
147  ->expects($this->once())
148  ->method('load')
149  ->will($this->returnValue(false));
150  $this->stateMock->expects($this->any())
151  ->method('getMode')
152  ->will($this->returnValue($state));
153 
154  $result = $this->createMock(\Magento\Framework\Controller\ResultInterface::class);
155  $result->expects($this->never())->method('setHeader');
156  $closure = function () use ($result) {
157  return $result;
158  };
159 
160  $this->assertSame(
161  $result,
162  $this->plugin->aroundDispatch($this->frontControllerMock, $closure, $this->requestMock)
163  );
164  }
165 
169  public function testAroundDispatchReturnsCache($state)
170  {
171  $this->configMock
172  ->expects($this->once())
173  ->method('getType')
174  ->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN));
175  $this->configMock->expects($this->once())
176  ->method('isEnabled')
177  ->will($this->returnValue(true));
178  $this->versionMock
179  ->expects($this->once())
180  ->method('process');
181  $this->kernelMock
182  ->expects($this->once())
183  ->method('load')
184  ->will($this->returnValue($this->responseMock));
185 
186  $this->stateMock->expects($this->any())
187  ->method('getMode')
188  ->will($this->returnValue($state));
189  if ($state == \Magento\Framework\App\State::MODE_DEVELOPER) {
190  $this->responseMock->expects($this->once())
191  ->method('setHeader')
192  ->with('X-Magento-Cache-Debug');
193  } else {
194  $this->responseMock->expects($this->never())
195  ->method('setHeader');
196  }
197  $this->assertSame(
198  $this->responseMock,
199  $this->plugin->aroundDispatch($this->frontControllerMock, $this->closure, $this->requestMock)
200  );
201  }
202 
206  public function testAroundDispatchDisabled($state)
207  {
208  $this->configMock
209  ->expects($this->any())
210  ->method('getType')
211  ->will($this->returnValue(null));
212  $this->configMock->expects($this->any())
213  ->method('isEnabled')
214  ->will($this->returnValue(true));
215  $this->versionMock
216  ->expects($this->once())
217  ->method('process');
218  $this->stateMock->expects($this->any())
219  ->method('getMode')
220  ->will($this->returnValue($state));
221  $this->responseMock->expects($this->never())
222  ->method('setHeader');
223  $this->assertSame(
224  $this->responseMock,
225  $this->plugin->aroundDispatch($this->frontControllerMock, $this->closure, $this->requestMock)
226  );
227  }
228 
232  public function dataProvider()
233  {
234  return [
235  'developer_mode' => [\Magento\Framework\App\State::MODE_DEVELOPER],
237  ];
238  }
239 }
$response
Definition: 404.php:11