BuiltinPluginTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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\Controller\Result;
  7. use Magento\PageCache\Model\Controller\Result\BuiltinPlugin;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. use Magento\PageCache\Model\Config;
  10. use Magento\Framework\App\PageCache\Kernel;
  11. use Magento\Framework\App\State as AppState;
  12. use Magento\Framework\Registry;
  13. use Magento\Framework\Controller\ResultInterface;
  14. use Magento\Framework\App\Response\Http as ResponseHttp;
  15. use Zend\Http\Header\HeaderInterface as HttpHeaderInterface;
  16. use Magento\PageCache\Model\Cache\Type as CacheType;
  17. /**
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class BuiltinPluginTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var BuiltinPlugin
  24. */
  25. private $plugin;
  26. /**
  27. * @var ObjectManagerHelper
  28. */
  29. private $objectManagerHelper;
  30. /**
  31. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $configMock;
  34. /**
  35. * @var Kernel|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $kernelMock;
  38. /**
  39. * @var AppState|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $stateMock;
  42. /**
  43. * @var Registry|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $registryMock;
  46. /**
  47. * @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $resultMock;
  50. /**
  51. * @var ResponseHttp|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $responseMock;
  54. /**
  55. * @var HttpHeaderInterface|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. private $httpHeaderMock;
  58. protected function setUp()
  59. {
  60. $this->configMock = $this->getMockBuilder(Config::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->kernelMock = $this->getMockBuilder(Kernel::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->stateMock = $this->getMockBuilder(AppState::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->registryMock = $this->getMockBuilder(Registry::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->resultMock = $this->getMockBuilder(ResultInterface::class)
  73. ->getMockForAbstractClass();
  74. $this->responseMock = $this->getMockBuilder(ResponseHttp::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->httpHeaderMock = $this->getMockBuilder(HttpHeaderInterface::class)
  78. ->getMockForAbstractClass();
  79. $this->responseMock->expects(static::any())
  80. ->method('getHeader')
  81. ->willReturnMap(
  82. [
  83. ['X-Magento-Tags', $this->httpHeaderMock],
  84. ['Cache-Control', $this->httpHeaderMock]
  85. ]
  86. );
  87. $this->configMock->expects(static::any())
  88. ->method('isEnabled')
  89. ->willReturn(true);
  90. $this->configMock->expects(static::any())
  91. ->method('getType')
  92. ->willReturn(Config::BUILT_IN);
  93. $this->objectManagerHelper = new ObjectManagerHelper($this);
  94. $this->plugin = $this->objectManagerHelper->getObject(
  95. BuiltinPlugin::class,
  96. [
  97. 'registry' => $this->registryMock,
  98. 'config' => $this->configMock,
  99. 'kernel' => $this->kernelMock,
  100. 'state' => $this->stateMock
  101. ]
  102. );
  103. }
  104. public function testAfterResultWithoutPlugin()
  105. {
  106. $this->registryMock->expects(static::once())
  107. ->method('registry')
  108. ->with('use_page_cache_plugin')
  109. ->willReturn(false);
  110. $this->kernelMock->expects(static::never())
  111. ->method('process')
  112. ->with($this->responseMock);
  113. $this->assertSame(
  114. $this->resultMock,
  115. $this->plugin->afterRenderResult($this->resultMock, $this->resultMock, $this->responseMock)
  116. );
  117. }
  118. public function testAfterResultWithPlugin()
  119. {
  120. $this->registryMock->expects(static::once())
  121. ->method('registry')
  122. ->with('use_page_cache_plugin')
  123. ->willReturn(true);
  124. $this->stateMock->expects(static::once())
  125. ->method('getMode')
  126. ->willReturn(null);
  127. $this->httpHeaderMock->expects(static::any())
  128. ->method('getFieldValue')
  129. ->willReturn('tag,tag');
  130. $this->responseMock->expects(static::once())
  131. ->method('clearHeader')
  132. ->with('X-Magento-Tags');
  133. $this->responseMock->expects(static::once())
  134. ->method('setHeader')
  135. ->with('X-Magento-Tags', 'tag,' . CacheType::CACHE_TAG);
  136. $this->kernelMock->expects(static::once())
  137. ->method('process')
  138. ->with($this->responseMock);
  139. $this->assertSame(
  140. $this->resultMock,
  141. $this->plugin->afterRenderResult($this->resultMock, $this->resultMock, $this->responseMock)
  142. );
  143. }
  144. public function testAfterResultWithPluginDeveloperMode()
  145. {
  146. $this->registryMock->expects(static::once())
  147. ->method('registry')
  148. ->with('use_page_cache_plugin')
  149. ->willReturn(true);
  150. $this->stateMock->expects(static::once())
  151. ->method('getMode')
  152. ->willReturn(AppState::MODE_DEVELOPER);
  153. $this->httpHeaderMock->expects(static::any())
  154. ->method('getFieldValue')
  155. ->willReturnOnConsecutiveCalls('test', 'tag,tag2');
  156. $this->responseMock->expects(static::any())
  157. ->method('setHeader')
  158. ->withConsecutive(
  159. ['X-Magento-Cache-Control', 'test'],
  160. ['X-Magento-Cache-Debug', 'MISS', true],
  161. ['X-Magento-Tags', 'tag,tag2,' . CacheType::CACHE_TAG]
  162. );
  163. $this->responseMock->expects(static::once())
  164. ->method('clearHeader')
  165. ->with('X-Magento-Tags');
  166. $this->registryMock->expects(static::once())
  167. ->method('registry')
  168. ->with('use_page_cache_plugin')
  169. ->willReturn(true);
  170. $this->kernelMock->expects(static::once())
  171. ->method('process')
  172. ->with($this->responseMock);
  173. $this->assertSame(
  174. $this->resultMock,
  175. $this->plugin->afterRenderResult($this->resultMock, $this->resultMock, $this->responseMock)
  176. );
  177. }
  178. }