Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
KernelTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\App\PageCache\Kernel;
9 use \Magento\Framework\App\Http\ContextFactory;
10 use \Magento\Framework\App\Response\HttpFactory;
11 
15 class KernelTest extends \PHPUnit\Framework\TestCase
16 {
18  protected $kernel;
19 
21  protected $cacheMock;
22 
24  protected $identifierMock;
25 
27  protected $requestMock;
28 
30  protected $responseMock;
31 
33  private $fullPageCacheMock;
34 
36  private $httpResponseMock;
37 
39  private $contextFactoryMock;
40 
42  private $httpFactoryMock;
43 
45  private $serializer;
46 
48  private $contextMock;
49 
53  protected function setUp()
54  {
55  $headersMock = $this->createMock(\Zend\Http\Headers::class);
56  $this->cacheMock = $this->createMock(\Magento\Framework\App\PageCache\Cache::class);
57  $this->fullPageCacheMock = $this->createMock(\Magento\PageCache\Model\Cache\Type::class);
58  $this->contextMock = $this->createMock(\Magento\Framework\App\Http\Context::class);
59  $this->httpResponseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
60  $this->identifierMock = $this->createMock(\Magento\Framework\App\PageCache\Identifier::class);
61  $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
62  $this->serializer = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
63  $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
64  $this->contextFactoryMock = $this->createPartialMock(ContextFactory::class, ['create']);
65  $this->httpFactoryMock = $this->createPartialMock(HttpFactory::class, ['create']);
66  $this->responseMock->expects($this->any())->method('getHeaders')->willReturn($headersMock);
67 
68  $this->kernel = new Kernel(
69  $this->cacheMock,
70  $this->identifierMock,
71  $this->requestMock,
72  $this->contextMock,
73  $this->contextFactoryMock,
74  $this->httpFactoryMock,
75  $this->serializer
76  );
77 
78  $reflection = new \ReflectionClass(\Magento\Framework\App\PageCache\Kernel::class);
79  $reflectionProperty = $reflection->getProperty('fullPageCache');
80  $reflectionProperty->setAccessible(true);
81  $reflectionProperty->setValue($this->kernel, $this->fullPageCacheMock);
82  }
83 
91  public function testLoadWithCachedData($id, $cache, $isGet, $isHead)
92  {
93  $this->serializer->expects($this->once())
94  ->method('unserialize')
95  ->willReturnCallback(
96  function ($value) {
97  return json_decode($value, true);
98  }
99  );
100 
101  $this->contextFactoryMock
102  ->expects($this->once())
103  ->method('create')
104  ->with(
105  [
106  'data' => ['context_data'],
107  'default' => ['context_default_data']
108  ]
109  )
110  ->willReturn($this->contextMock);
111 
112  $this->httpFactoryMock
113  ->expects($this->once())
114  ->method('create')
115  ->with(['context' => $this->contextMock])
116  ->willReturn($this->httpResponseMock);
117 
118  $this->requestMock->expects($this->once())->method('isGet')->will($this->returnValue($isGet));
119  $this->requestMock->expects($this->any())->method('isHead')->will($this->returnValue($isHead));
120  $this->fullPageCacheMock->expects(
121  $this->any()
122  )->method(
123  'load'
124  )->with(
125  $this->equalTo($id)
126  )->will(
127  $this->returnValue(json_encode($cache))
128  );
129  $this->httpResponseMock->expects($this->once())->method('setStatusCode')->with($cache['status_code']);
130  $this->httpResponseMock->expects($this->once())->method('setContent')->with($cache['content']);
131  $this->httpResponseMock->expects($this->once())->method('setHeader')->with(0, 'header', true);
132  $this->identifierMock->expects($this->any())->method('getValue')->will($this->returnValue($id));
133  $this->assertEquals($this->httpResponseMock, $this->kernel->load());
134  }
135 
140  {
141  $data = [
142  'context' => [
143  'data' => ['context_data'],
144  'default' => ['context_default_data']
145  ],
146  'status_code' => 'status_code',
147  'content' => 'content',
148  'headers' => ['header']
149  ];
150 
151  return [
152  ['existing key', $data, true, false],
153  ['existing key', $data, false, true],
154  ];
155  }
156 
164  public function testLoadWithoutCachedData($id, $cache, $isGet, $isHead)
165  {
166  $this->requestMock->expects($this->once())->method('isGet')->will($this->returnValue($isGet));
167  $this->requestMock->expects($this->any())->method('isHead')->will($this->returnValue($isHead));
168  $this->fullPageCacheMock->expects(
169  $this->any()
170  )->method(
171  'load'
172  )->with(
173  $this->equalTo($id)
174  )->will(
175  $this->returnValue(json_encode($cache))
176  );
177  $this->identifierMock->expects($this->any())->method('getValue')->will($this->returnValue($id));
178  $this->assertEquals(false, $this->kernel->load());
179  }
180 
185  {
186  return [
187  ['existing key', [], false, false],
188  ['non existing key', false, true, false],
189  ['non existing key', false, false, false]
190  ];
191  }
192 
197  public function testProcessSaveCache($httpCode, $at)
198  {
199  $this->serializer->expects($this->once())
200  ->method('serialize')
201  ->willReturnCallback(
202  function ($value) {
203  return json_encode($value);
204  }
205  );
206 
207  $cacheControlHeader = \Zend\Http\Header\CacheControl::fromString(
208  'Cache-Control: public, max-age=100, s-maxage=100'
209  );
210 
211  $this->responseMock->expects(
212  $this->at(0)
213  )->method(
214  'getHeader'
215  )->with(
216  'Cache-Control'
217  )->will(
218  $this->returnValue($cacheControlHeader)
219  );
220  $this->responseMock->expects(
221  $this->any()
222  )->method(
223  'getHttpResponseCode'
224  )->willReturn($httpCode);
225  $this->requestMock->expects($this->once())
226  ->method('isGet')
227  ->willReturn(true);
228  $this->responseMock->expects($this->once())
229  ->method('setNoCacheHeaders');
230  $this->responseMock->expects($this->at($at[0]))
231  ->method('getHeader')
232  ->with('X-Magento-Tags');
233  $this->responseMock->expects($this->at($at[1]))
234  ->method('clearHeader')
235  ->with($this->equalTo('Set-Cookie'));
236  $this->responseMock->expects($this->at($at[2]))
237  ->method('clearHeader')
238  ->with($this->equalTo('X-Magento-Tags'));
239  $this->fullPageCacheMock->expects($this->once())
240  ->method('save');
241  $this->kernel->process($this->responseMock);
242  }
243 
248  {
249  return [
250  [200, [3, 4, 5]],
251  [404, [4, 5, 6]]
252  ];
253  }
254 
262  public function testProcessNotSaveCache($cacheControlHeader, $httpCode, $isGet, $overrideHeaders)
263  {
264  $header = \Zend\Http\Header\CacheControl::fromString("Cache-Control: $cacheControlHeader");
265  $this->responseMock->expects(
266  $this->once()
267  )->method(
268  'getHeader'
269  )->with(
270  'Cache-Control'
271  )->will(
272  $this->returnValue($header)
273  );
274  $this->responseMock->expects($this->any())->method('getHttpResponseCode')->will($this->returnValue($httpCode));
275  $this->requestMock->expects($this->any())->method('isGet')->will($this->returnValue($isGet));
276  if ($overrideHeaders) {
277  $this->responseMock->expects($this->once())->method('setNoCacheHeaders');
278  }
279  $this->fullPageCacheMock->expects($this->never())->method('save');
280  $this->kernel->process($this->responseMock);
281  }
282 
286  public function processNotSaveCacheProvider()
287  {
288  return [
289  ['private, max-age=100', 200, true, false],
290  ['private, max-age=100', 200, false, false],
291  ['private, max-age=100', 500, true, false],
292  ['no-store, no-cache, must-revalidate, max-age=0', 200, true, false],
293  ['no-store, no-cache, must-revalidate, max-age=0', 200, false, false],
294  ['no-store, no-cache, must-revalidate, max-age=0', 404, true, false],
295  ['no-store, no-cache, must-revalidate, max-age=0', 500, true, false],
296  ['public, max-age=100, s-maxage=100', 500, true, true],
297  ['public, max-age=100, s-maxage=100', 200, false, true]
298  ];
299  }
300 }
$id
Definition: fieldset.phtml:14
$value
Definition: gender.phtml:16
testLoadWithCachedData($id, $cache, $isGet, $isHead)
Definition: KernelTest.php:91
testLoadWithoutCachedData($id, $cache, $isGet, $isHead)
Definition: KernelTest.php:164
testProcessNotSaveCache($cacheControlHeader, $httpCode, $isGet, $overrideHeaders)
Definition: KernelTest.php:262