VarnishPluginTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\VarnishPlugin;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. use Magento\PageCache\Model\Config;
  10. use Magento\Framework\App\PageCache\Version;
  11. use Magento\Framework\App\State as AppState;
  12. use Magento\Framework\App\FrontControllerInterface;
  13. use Magento\Framework\App\Response\Http as ResponseHttp;
  14. use Magento\Framework\Controller\ResultInterface;
  15. /**
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class VarnishPluginTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /**
  21. * @var VarnishPlugin
  22. */
  23. private $plugin;
  24. /**
  25. * @var ObjectManagerHelper
  26. */
  27. private $objectManagerHelper;
  28. /**
  29. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $configMock;
  32. /**
  33. * @var Version|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $versionMock;
  36. /**
  37. * @var AppState|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $stateMock;
  40. /**
  41. * @var FrontControllerInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $frontControllerMock;
  44. /**
  45. * @var ResponseHttp|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $responseMock;
  48. /**
  49. * @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $resultMock;
  52. protected function setUp()
  53. {
  54. $this->configMock = $this->getMockBuilder(Config::class)
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->versionMock = $this->getMockBuilder(Version::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->stateMock = $this->getMockBuilder(AppState::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->frontControllerMock = $this->getMockBuilder(FrontControllerInterface::class)
  64. ->getMockForAbstractClass();
  65. $this->responseMock = $this->getMockBuilder(ResponseHttp::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->resultMock = $this->getMockBuilder(ResultInterface::class)
  69. ->getMockForAbstractClass();
  70. $this->objectManagerHelper = new ObjectManagerHelper($this);
  71. $this->plugin = $this->objectManagerHelper->getObject(
  72. VarnishPlugin::class,
  73. [
  74. 'config' => $this->configMock,
  75. 'version' => $this->versionMock,
  76. 'state' => $this->stateMock
  77. ]
  78. );
  79. }
  80. /**
  81. * @param string $state
  82. * @param int $countHeader
  83. *
  84. * @dataProvider afterDispatchDataProvider
  85. */
  86. public function testAfterDispatchReturnsCache($state, $countHeader)
  87. {
  88. $this->configMock->expects(static::once())
  89. ->method('isEnabled')
  90. ->willReturn(true);
  91. $this->configMock->expects(static::once())
  92. ->method('getType')
  93. ->willReturn(Config::VARNISH);
  94. $this->versionMock->expects(static::once())
  95. ->method('process');
  96. $this->stateMock->expects(static::once())
  97. ->method('getMode')
  98. ->willReturn($state);
  99. $this->responseMock->expects(static::exactly($countHeader))
  100. ->method('setHeader')
  101. ->with('X-Magento-Debug');
  102. $this->assertSame(
  103. $this->responseMock,
  104. $this->plugin->afterDispatch($this->frontControllerMock, $this->responseMock)
  105. );
  106. }
  107. public function testAfterDispatchNotResponse()
  108. {
  109. $this->configMock->expects(static::once())
  110. ->method('isEnabled')
  111. ->willReturn(true);
  112. $this->configMock->expects(static::once())
  113. ->method('getType')
  114. ->willReturn(Config::VARNISH);
  115. $this->versionMock->expects(static::never())
  116. ->method('process');
  117. $this->stateMock->expects(static::never())
  118. ->method('getMode');
  119. $this->resultMock->expects(static::never())
  120. ->method('setHeader');
  121. $this->assertSame(
  122. $this->resultMock,
  123. $this->plugin->afterDispatch($this->frontControllerMock, $this->resultMock)
  124. );
  125. }
  126. public function testAfterDispatchDisabled()
  127. {
  128. $this->configMock->expects(static::any())
  129. ->method('getType')
  130. ->willReturn(null);
  131. $this->versionMock->expects(static::never())
  132. ->method('process');
  133. $this->stateMock->expects(static::any())
  134. ->method('getMode')
  135. ->willReturn(AppState::MODE_DEVELOPER);
  136. $this->responseMock->expects(static::never())
  137. ->method('setHeader');
  138. $this->assertSame(
  139. $this->responseMock,
  140. $this->plugin->afterDispatch($this->frontControllerMock, $this->responseMock)
  141. );
  142. }
  143. /**
  144. * @return array
  145. */
  146. public function afterDispatchDataProvider()
  147. {
  148. return [
  149. 'developer_mode' => [AppState::MODE_DEVELOPER, 1],
  150. 'production' => [AppState::MODE_PRODUCTION, 0]
  151. ];
  152. }
  153. }