TYPO3 CMS  TYPO3_8-7
WincacheBackendTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
22 class WincacheBackendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
27  protected function setUp()
28  {
29  if (!extension_loaded('wincache')) {
30  $this->markTestSkipped('WinCache extension was not available');
31  }
32  }
33 
38  {
39  $this->expectException(\TYPO3\CMS\Core\Cache\Exception::class);
40  //@todo Add exception code with wincache extension
41 
42  $backend = new WincacheBackend('Testing');
43  $data = 'Some data';
44  $identifier = $this->getUniqueId('MyIdentifier');
45  $backend->set($identifier, $data);
46  }
47 
52  {
53  $backend = $this->setUpBackend();
54  $data = 'Some data';
55  $identifier = $this->getUniqueId('MyIdentifier');
56  $backend->set($identifier, $data);
57  $inCache = $backend->has($identifier);
58  $this->assertTrue($inCache, 'WinCache backend failed to set and check entry');
59  }
60 
64  public function itIsPossibleToSetAndGetEntry()
65  {
66  $backend = $this->setUpBackend();
67  $data = 'Some data';
68  $identifier = $this->getUniqueId('MyIdentifier');
69  $backend->set($identifier, $data);
70  $fetchedData = $backend->get($identifier);
71  $this->assertEquals($data, $fetchedData, 'Winache backend failed to set and retrieve data');
72  }
73 
78  {
79  $backend = $this->setUpBackend();
80  $data = 'Some data';
81  $identifier = $this->getUniqueId('MyIdentifier');
82  $backend->set($identifier, $data);
83  $backend->remove($identifier);
84  $inCache = $backend->has($identifier);
85  $this->assertFalse($inCache, 'Failed to set and remove data from WinCache backend');
86  }
87 
92  {
93  $backend = $this->setUpBackend();
94  $data = 'Some data';
95  $identifier = $this->getUniqueId('MyIdentifier');
96  $backend->set($identifier, $data);
97  $otherData = 'some other data';
98  $backend->set($identifier, $otherData);
99  $fetchedData = $backend->get($identifier);
100  $this->assertEquals($otherData, $fetchedData, 'WinCache backend failed to overwrite and retrieve data');
101  }
102 
107  {
108  $backend = $this->setUpBackend();
109  $data = 'Some data';
110  $identifier = $this->getUniqueId('MyIdentifier');
111  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
112  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
113  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
114  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
115  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
116  }
117 
122  {
123  $backend = $this->setUpBackend();
124  $data = 'Some data';
125  $identifier = $this->getUniqueId('MyIdentifier');
126  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tagX']);
127  $backend->set($identifier, $data, ['UnitTestTag%tag3']);
128  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
129  $this->assertEquals([], $retrieved, 'Found entry which should no longer exist.');
130  }
131 
136  {
137  $backend = $this->setUpBackend();
138  $identifier = $this->getUniqueId('NonExistingIdentifier');
139  $inCache = $backend->has($identifier);
140  $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
141  }
142 
147  {
148  $backend = $this->setUpBackend();
149  $identifier = $this->getUniqueId('NonExistingIdentifier');
150  $inCache = $backend->remove($identifier);
151  $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
152  }
153 
158  {
159  $backend = $this->setUpBackend();
160  $data = 'some data' . microtime();
161  $backend->set('BackendWincacheTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
162  $backend->set('BackendWincacheTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
163  $backend->set('BackendWincacheTest3', $data, ['UnitTestTag%test']);
164  $backend->flushByTag('UnitTestTag%special');
165  $this->assertTrue($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
166  $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
167  $this->assertTrue($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
168  }
169 
174  {
175  $backend = $this->setUpBackend();
176  $data = 'some data' . microtime();
177  $backend->set('BackendWincacheTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
178  $backend->set('BackendWincacheTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
179  $backend->set('BackendWincacheTest3', $data, ['UnitTestTag%test']);
180  $backend->flushByTag('UnitTestTag%special', 'UnitTestTag%boring');
181  $this->assertTrue($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
182  $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
183  $this->assertTrue($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
184  }
185 
189  public function flushRemovesAllCacheEntries()
190  {
191  $backend = $this->setUpBackend();
192  $data = 'some data' . microtime();
193  $backend->set('BackendWincacheTest1', $data);
194  $backend->set('BackendWincacheTest2', $data);
195  $backend->set('BackendWincacheTest3', $data);
196  $backend->flush();
197  $this->assertFalse($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
198  $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
199  $this->assertFalse($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
200  }
201 
205  public function flushRemovesOnlyOwnEntries()
206  {
208  $thisCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
209  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
210  $thisBackend = new WincacheBackend('Testing');
211  $thisBackend->setCache($thisCache);
212 
214  $thatCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
215  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
216  $thatBackend = new WincacheBackend('Testing');
217  $thatBackend->setCache($thatCache);
218  $thisBackend->set('thisEntry', 'Hello');
219  $thatBackend->set('thatEntry', 'World!');
220  $thatBackend->flush();
221  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
222  $this->assertFalse($thatBackend->has('thatEntry'));
223  }
224 
230  public function largeDataIsStored()
231  {
232  $backend = $this->setUpBackend();
233  $data = str_repeat('abcde', 1024 * 1024);
234  $identifier = $this->getUniqueId('tooLargeData');
235  $backend->set($identifier, $data);
236  $this->assertTrue($backend->has($identifier));
237  $this->assertEquals($backend->get($identifier), $data);
238  }
239 
243  public function setTagsOnlyOnceToIdentifier()
244  {
245  $identifier = $this->getUniqueId('MyIdentifier');
246  $tags = ['UnitTestTag%test', 'UnitTestTag%boring'];
247 
248  $backend = $this->setUpBackend(true);
249  $backend->_call('addIdentifierToTags', $identifier, $tags);
250  $this->assertSame(
251  $tags,
252  $backend->_call('findTagsByIdentifier', $identifier)
253  );
254 
255  $backend->_call('addIdentifierToTags', $identifier, $tags);
256  $this->assertSame(
257  $tags,
258  $backend->_call('findTagsByIdentifier', $identifier)
259  );
260  }
261 
268  protected function setUpBackend($accessible = false)
269  {
271  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
272  if ($accessible) {
273  $accessibleClassName = $this->buildAccessibleProxy(WincacheBackend::class);
274  $backend = new $accessibleClassName('Testing');
275  } else {
276  $backend = new WincacheBackend('Testing');
277  }
278  $backend->setCache($cache);
279  return $backend;
280  }
281 }