BuiltinPluginTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\PageCache\Test\Unit\Model\App\FrontController;
  7. use Magento\PageCache\Model\App\FrontController\BuiltinPlugin;
  8. class BuiltinPluginTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var BuiltinPlugin
  12. */
  13. protected $plugin;
  14. /**
  15. * @var \Magento\PageCache\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $configMock;
  18. /**
  19. * @var \Magento\Framework\App\PageCache\Version|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $versionMock;
  22. /**
  23. * @var \Magento\Framework\App\PageCache\Kernel|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $kernelMock;
  26. /**
  27. * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $stateMock;
  30. /**
  31. * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $responseMock;
  34. /**
  35. * @var \Magento\Framework\App\FrontControllerInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $frontControllerMock;
  38. /**
  39. * @var \Closure
  40. */
  41. protected $closure;
  42. /**
  43. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $requestMock;
  46. /**
  47. * SetUp
  48. */
  49. protected function setUp()
  50. {
  51. $this->configMock = $this->createMock(\Magento\PageCache\Model\Config::class);
  52. $this->versionMock = $this->createMock(\Magento\Framework\App\PageCache\Version::class);
  53. $this->kernelMock = $this->createMock(\Magento\Framework\App\PageCache\Kernel::class);
  54. $this->stateMock = $this->createMock(\Magento\Framework\App\State::class);
  55. $this->frontControllerMock = $this->createMock(\Magento\Framework\App\FrontControllerInterface::class);
  56. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  57. $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
  58. $response = $this->responseMock;
  59. $this->closure = function () use ($response) {
  60. return $response;
  61. };
  62. $this->plugin = new \Magento\PageCache\Model\App\FrontController\BuiltinPlugin(
  63. $this->configMock,
  64. $this->versionMock,
  65. $this->kernelMock,
  66. $this->stateMock
  67. );
  68. }
  69. /**
  70. * @dataProvider dataProvider
  71. */
  72. public function testAroundDispatchProcessIfCacheMissed($state)
  73. {
  74. $header = \Zend\Http\Header\GenericHeader::fromString('Cache-Control: no-cache');
  75. $this->configMock
  76. ->expects($this->once())
  77. ->method('getType')
  78. ->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN));
  79. $this->configMock->expects($this->once())
  80. ->method('isEnabled')
  81. ->will($this->returnValue(true));
  82. $this->versionMock
  83. ->expects($this->once())
  84. ->method('process');
  85. $this->kernelMock
  86. ->expects($this->once())
  87. ->method('load')
  88. ->will($this->returnValue(false));
  89. $this->stateMock->expects($this->any())
  90. ->method('getMode')
  91. ->will($this->returnValue($state));
  92. if ($state == \Magento\Framework\App\State::MODE_DEVELOPER) {
  93. $this->responseMock->expects($this->at(1))
  94. ->method('setHeader')
  95. ->with('X-Magento-Cache-Control');
  96. $this->responseMock->expects($this->at(2))
  97. ->method('setHeader')
  98. ->with('X-Magento-Cache-Debug', 'MISS', true);
  99. } else {
  100. $this->responseMock->expects($this->never())
  101. ->method('setHeader');
  102. }
  103. $this->responseMock
  104. ->expects($this->once())
  105. ->method('getHeader')
  106. ->with('Cache-Control')
  107. ->will($this->returnValue($header));
  108. $this->kernelMock
  109. ->expects($this->once())
  110. ->method('process')
  111. ->with($this->responseMock);
  112. $this->assertSame(
  113. $this->responseMock,
  114. $this->plugin->aroundDispatch($this->frontControllerMock, $this->closure, $this->requestMock)
  115. );
  116. }
  117. /**
  118. * @dataProvider dataProvider
  119. */
  120. public function testAroundDispatchReturnsResultInterfaceProcessIfCacheMissed($state)
  121. {
  122. $this->configMock
  123. ->expects($this->once())
  124. ->method('getType')
  125. ->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN));
  126. $this->configMock->expects($this->once())
  127. ->method('isEnabled')
  128. ->will($this->returnValue(true));
  129. $this->versionMock
  130. ->expects($this->once())
  131. ->method('process');
  132. $this->kernelMock
  133. ->expects($this->once())
  134. ->method('load')
  135. ->will($this->returnValue(false));
  136. $this->stateMock->expects($this->any())
  137. ->method('getMode')
  138. ->will($this->returnValue($state));
  139. $result = $this->createMock(\Magento\Framework\Controller\ResultInterface::class);
  140. $result->expects($this->never())->method('setHeader');
  141. $closure = function () use ($result) {
  142. return $result;
  143. };
  144. $this->assertSame(
  145. $result,
  146. $this->plugin->aroundDispatch($this->frontControllerMock, $closure, $this->requestMock)
  147. );
  148. }
  149. /**
  150. * @dataProvider dataProvider
  151. */
  152. public function testAroundDispatchReturnsCache($state)
  153. {
  154. $this->configMock
  155. ->expects($this->once())
  156. ->method('getType')
  157. ->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN));
  158. $this->configMock->expects($this->once())
  159. ->method('isEnabled')
  160. ->will($this->returnValue(true));
  161. $this->versionMock
  162. ->expects($this->once())
  163. ->method('process');
  164. $this->kernelMock
  165. ->expects($this->once())
  166. ->method('load')
  167. ->will($this->returnValue($this->responseMock));
  168. $this->stateMock->expects($this->any())
  169. ->method('getMode')
  170. ->will($this->returnValue($state));
  171. if ($state == \Magento\Framework\App\State::MODE_DEVELOPER) {
  172. $this->responseMock->expects($this->once())
  173. ->method('setHeader')
  174. ->with('X-Magento-Cache-Debug');
  175. } else {
  176. $this->responseMock->expects($this->never())
  177. ->method('setHeader');
  178. }
  179. $this->assertSame(
  180. $this->responseMock,
  181. $this->plugin->aroundDispatch($this->frontControllerMock, $this->closure, $this->requestMock)
  182. );
  183. }
  184. /**
  185. * @dataProvider dataProvider
  186. */
  187. public function testAroundDispatchDisabled($state)
  188. {
  189. $this->configMock
  190. ->expects($this->any())
  191. ->method('getType')
  192. ->will($this->returnValue(null));
  193. $this->configMock->expects($this->any())
  194. ->method('isEnabled')
  195. ->will($this->returnValue(true));
  196. $this->versionMock
  197. ->expects($this->once())
  198. ->method('process');
  199. $this->stateMock->expects($this->any())
  200. ->method('getMode')
  201. ->will($this->returnValue($state));
  202. $this->responseMock->expects($this->never())
  203. ->method('setHeader');
  204. $this->assertSame(
  205. $this->responseMock,
  206. $this->plugin->aroundDispatch($this->frontControllerMock, $this->closure, $this->requestMock)
  207. );
  208. }
  209. /**
  210. * @return array
  211. */
  212. public function dataProvider()
  213. {
  214. return [
  215. 'developer_mode' => [\Magento\Framework\App\State::MODE_DEVELOPER],
  216. 'production' => [\Magento\Framework\App\State::MODE_PRODUCTION],
  217. ];
  218. }
  219. }