ConfigTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\PageCache\Model\Config;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  13. */
  14. class ConfigTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\PageCache\Model\Config
  18. */
  19. private $config;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Config\ScopeConfigInterface
  22. */
  23. private $coreConfigMock;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Cache\StateInterface
  26. */
  27. private $cacheState;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Module\Dir\Reader
  30. */
  31. private $moduleReader;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject|Json
  34. */
  35. private $serializerMock;
  36. /**
  37. * setUp all mocks and data function
  38. */
  39. protected function setUp()
  40. {
  41. $objectManager = new ObjectManager($this);
  42. $readFactoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
  43. $this->coreConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  44. $this->cacheState = $this->getMockForAbstractClass(\Magento\Framework\App\Cache\StateInterface::class);
  45. $modulesDirectoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
  46. $readFactoryMock->expects(
  47. $this->any()
  48. )->method(
  49. 'create'
  50. )->will(
  51. $this->returnValue($modulesDirectoryMock)
  52. );
  53. $modulesDirectoryMock->expects(
  54. $this->any()
  55. )->method(
  56. 'readFile'
  57. )->will(
  58. $this->returnValue(file_get_contents(__DIR__ . '/_files/test.vcl'))
  59. );
  60. $this->coreConfigMock->expects(
  61. $this->any()
  62. )->method(
  63. 'getValue'
  64. )->will(
  65. $this->returnValueMap(
  66. [
  67. [
  68. \Magento\PageCache\Model\Config::XML_VARNISH_PAGECACHE_BACKEND_HOST,
  69. \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  70. null,
  71. 'example.com',
  72. ],
  73. [
  74. \Magento\PageCache\Model\Config::XML_VARNISH_PAGECACHE_BACKEND_PORT,
  75. \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  76. null,
  77. '8080'
  78. ],
  79. [
  80. \Magento\PageCache\Model\Config::XML_VARNISH_PAGECACHE_ACCESS_LIST,
  81. \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  82. null,
  83. '127.0.0.1, 192.168.0.1,127.0.0.2'
  84. ],
  85. [
  86. \Magento\PageCache\Model\Config::XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX,
  87. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  88. null,
  89. 'serializedConfig'
  90. ],
  91. [
  92. \Magento\Framework\HTTP\PhpEnvironment\Request::XML_PATH_OFFLOADER_HEADER,
  93. \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  94. null,
  95. 'X_Forwarded_Proto: https'
  96. ],
  97. [
  98. \Magento\PageCache\Model\Config::XML_VARNISH_PAGECACHE_GRACE_PERIOD,
  99. \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  100. null,
  101. 120
  102. ],
  103. ]
  104. )
  105. );
  106. $this->moduleReader = $this->createMock(\Magento\Framework\Module\Dir\Reader::class);
  107. $this->serializerMock = $this->createMock(Json::class);
  108. /** @var \PHPUnit_Framework_MockObject_MockObject $vclTemplateLocator */
  109. $vclTemplateLocator = $this->getMockBuilder(\Magento\PageCache\Model\Varnish\VclTemplateLocator::class)
  110. ->disableOriginalConstructor()
  111. ->setMethods(['getTemplate'])
  112. ->getMock();
  113. $vclTemplateLocator->expects($this->any())
  114. ->method('getTemplate')
  115. ->will($this->returnValue(file_get_contents(__DIR__ . '/_files/test.vcl')));
  116. /** @var \PHPUnit_Framework_MockObject_MockObject $vclTemplateLocator */
  117. $vclGeneratorFactory = $this->getMockBuilder(\Magento\PageCache\Model\Varnish\VclGeneratorFactory::class)
  118. ->disableOriginalConstructor()
  119. ->setMethods(['create'])
  120. ->getMock();
  121. $expectedParams = [
  122. 'backendHost' => 'example.com',
  123. 'backendPort' => '8080',
  124. 'accessList' => explode(',', '127.0.0.1, 192.168.0.1,127.0.0.2'),
  125. 'designExceptions' => [['regexp' => '(?i)pattern', 'value' => 'value_for_pattern']],
  126. 'sslOffloadedHeader' => 'X_Forwarded_Proto: https',
  127. 'gracePeriod' => 120
  128. ];
  129. $vclGeneratorFactory->expects($this->any())
  130. ->method('create')
  131. ->with($expectedParams)
  132. ->will($this->returnValue(new \Magento\PageCache\Model\Varnish\VclGenerator(
  133. $vclTemplateLocator,
  134. 'example.com',
  135. '8080',
  136. explode(',', '127.0.0.1,192.168.0.1,127.0.0.2'),
  137. 120,
  138. 'X_Forwarded_Proto: https',
  139. [['regexp' => '(?i)pattern', 'value' => 'value_for_pattern']]
  140. )));
  141. $this->config = $objectManager->getObject(
  142. \Magento\PageCache\Model\Config::class,
  143. [
  144. 'readFactory' => $readFactoryMock,
  145. 'scopeConfig' => $this->coreConfigMock,
  146. 'cacheState' => $this->cacheState,
  147. 'reader' => $this->moduleReader,
  148. 'serializer' => $this->serializerMock,
  149. 'vclGeneratorFactory' => $vclGeneratorFactory
  150. ]
  151. );
  152. }
  153. /**
  154. * test for getVcl method
  155. */
  156. public function testGetVcl()
  157. {
  158. $this->serializerMock->expects($this->once())
  159. ->method('unserialize')
  160. ->with('serializedConfig')
  161. ->willReturn([['regexp' => '(?i)pattern', 'value' => 'value_for_pattern']]);
  162. $test = $this->config->getVclFile(Config::VARNISH_5_CONFIGURATION_PATH);
  163. $this->assertEquals(file_get_contents(__DIR__ . '/_files/result.vcl'), $test);
  164. }
  165. public function testGetTll()
  166. {
  167. $this->coreConfigMock->expects($this->once())->method('getValue')->with(Config::XML_PAGECACHE_TTL);
  168. $this->config->getTtl();
  169. }
  170. /**
  171. * Whether a cache type is enabled
  172. */
  173. public function testIsEnabled()
  174. {
  175. $this->cacheState->expects($this->at(0))
  176. ->method('isEnabled')
  177. ->with(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)
  178. ->will($this->returnValue(true));
  179. $this->cacheState->expects($this->at(1))
  180. ->method('isEnabled')
  181. ->with(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)
  182. ->will($this->returnValue(false));
  183. $this->assertTrue($this->config->isEnabled());
  184. $this->assertFalse($this->config->isEnabled());
  185. }
  186. }