Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigTest.php
Go to the documentation of this file.
1 <?php
7 
11 
16 class ConfigTest extends \PHPUnit\Framework\TestCase
17 {
21  private $config;
22 
26  private $coreConfigMock;
27 
31  private $cacheState;
32 
36  private $moduleReader;
37 
41  private $serializerMock;
42 
46  protected function setUp()
47  {
48  $objectManager = new ObjectManager($this);
49  $readFactoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
50  $this->coreConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
51  $this->cacheState = $this->getMockForAbstractClass(\Magento\Framework\App\Cache\StateInterface::class);
52 
53  $modulesDirectoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
54  $readFactoryMock->expects(
55  $this->any()
56  )->method(
57  'create'
58  )->will(
59  $this->returnValue($modulesDirectoryMock)
60  );
61  $modulesDirectoryMock->expects(
62  $this->any()
63  )->method(
64  'readFile'
65  )->will(
66  $this->returnValue(file_get_contents(__DIR__ . '/_files/test.vcl'))
67  );
68  $this->coreConfigMock->expects(
69  $this->any()
70  )->method(
71  'getValue'
72  )->will(
73  $this->returnValueMap(
74  [
75  [
77  \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
78  null,
79  'example.com',
80  ],
81  [
83  \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
84  null,
85  '8080'
86  ],
87  [
89  \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
90  null,
91  '127.0.0.1, 192.168.0.1,127.0.0.2'
92  ],
93  [
95  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
96  null,
97  'serializedConfig'
98  ],
99  [
100  \Magento\Framework\HTTP\PhpEnvironment\Request::XML_PATH_OFFLOADER_HEADER,
101  \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
102  null,
103  'X_Forwarded_Proto: https'
104  ],
105  [
107  \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
108  null,
109  120
110  ],
111  ]
112  )
113  );
114 
115  $this->moduleReader = $this->createMock(\Magento\Framework\Module\Dir\Reader::class);
116  $this->serializerMock = $this->createMock(Json::class);
117 
119  $vclTemplateLocator = $this->getMockBuilder(\Magento\PageCache\Model\Varnish\VclTemplateLocator::class)
120  ->disableOriginalConstructor()
121  ->setMethods(['getTemplate'])
122  ->getMock();
123  $vclTemplateLocator->expects($this->any())
124  ->method('getTemplate')
125  ->will($this->returnValue(file_get_contents(__DIR__ . '/_files/test.vcl')));
127  $vclGeneratorFactory = $this->getMockBuilder(\Magento\PageCache\Model\Varnish\VclGeneratorFactory::class)
128  ->disableOriginalConstructor()
129  ->setMethods(['create'])
130  ->getMock();
131  $expectedParams = [
132  'backendHost' => 'example.com',
133  'backendPort' => '8080',
134  'accessList' => explode(',', '127.0.0.1, 192.168.0.1,127.0.0.2'),
135  'designExceptions' => [['regexp' => '(?i)pattern', 'value' => 'value_for_pattern']],
136  'sslOffloadedHeader' => 'X_Forwarded_Proto: https',
137  'gracePeriod' => 120
138  ];
139  $vclGeneratorFactory->expects($this->any())
140  ->method('create')
141  ->with($expectedParams)
142  ->will($this->returnValue(new \Magento\PageCache\Model\Varnish\VclGenerator(
143  $vclTemplateLocator,
144  'example.com',
145  '8080',
146  explode(',', '127.0.0.1,192.168.0.1,127.0.0.2'),
147  120,
148  'X_Forwarded_Proto: https',
149  [['regexp' => '(?i)pattern', 'value' => 'value_for_pattern']]
150  )));
151  $this->config = $objectManager->getObject(
152  \Magento\PageCache\Model\Config::class,
153  [
154  'readFactory' => $readFactoryMock,
155  'scopeConfig' => $this->coreConfigMock,
156  'cacheState' => $this->cacheState,
157  'reader' => $this->moduleReader,
158  'serializer' => $this->serializerMock,
159  'vclGeneratorFactory' => $vclGeneratorFactory
160  ]
161  );
162  }
163 
167  public function testGetVcl()
168  {
169  $this->serializerMock->expects($this->once())
170  ->method('unserialize')
171  ->with('serializedConfig')
172  ->willReturn([['regexp' => '(?i)pattern', 'value' => 'value_for_pattern']]);
173  $test = $this->config->getVclFile(Config::VARNISH_5_CONFIGURATION_PATH);
174  $this->assertEquals(file_get_contents(__DIR__ . '/_files/result.vcl'), $test);
175  }
176 
177  public function testGetTll()
178  {
179  $this->coreConfigMock->expects($this->once())->method('getValue')->with(Config::XML_PAGECACHE_TTL);
180  $this->config->getTtl();
181  }
182 
186  public function testIsEnabled()
187  {
188  $this->cacheState->expects($this->at(0))
189  ->method('isEnabled')
190  ->with(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)
191  ->will($this->returnValue(true));
192  $this->cacheState->expects($this->at(1))
193  ->method('isEnabled')
194  ->with(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)
195  ->will($this->returnValue(false));
196  $this->assertTrue($this->config->isEnabled());
197  $this->assertFalse($this->config->isEnabled());
198  }
199 }
$objectManager
Definition: bootstrap.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
const XML_VARNISH_PAGECACHE_BACKEND_HOST
Definition: Config.php:41
const XML_VARNISH_PAGECACHE_BACKEND_PORT
Definition: Config.php:39
const XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX
Definition: Config.php:45
const XML_VARNISH_PAGECACHE_GRACE_PERIOD
Definition: Config.php:43