TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
WincacheBackendTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Backend;
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 
23 {
29  protected function setUp()
30  {
31  if (!extension_loaded('wincache')) {
32  $this->markTestSkipped('WinCache extension was not available');
33  }
34  }
35 
40  {
41  $this->expectException(\TYPO3\CMS\Core\Cache\Exception::class);
42  //@todo Add exception code with wincache extension
43 
44  $backend = new WincacheBackend('Testing');
45  $data = 'Some data';
46  $identifier = $this->getUniqueId('MyIdentifier');
47  $backend->set($identifier, $data);
48  }
49 
54  {
55  $backend = $this->setUpBackend();
56  $data = 'Some data';
57  $identifier = $this->getUniqueId('MyIdentifier');
58  $backend->set($identifier, $data);
59  $inCache = $backend->has($identifier);
60  $this->assertTrue($inCache, 'WinCache backend failed to set and check entry');
61  }
62 
66  public function itIsPossibleToSetAndGetEntry()
67  {
68  $backend = $this->setUpBackend();
69  $data = 'Some data';
70  $identifier = $this->getUniqueId('MyIdentifier');
71  $backend->set($identifier, $data);
72  $fetchedData = $backend->get($identifier);
73  $this->assertEquals($data, $fetchedData, 'Winache backend failed to set and retrieve data');
74  }
75 
80  {
81  $backend = $this->setUpBackend();
82  $data = 'Some data';
83  $identifier = $this->getUniqueId('MyIdentifier');
84  $backend->set($identifier, $data);
85  $backend->remove($identifier);
86  $inCache = $backend->has($identifier);
87  $this->assertFalse($inCache, 'Failed to set and remove data from WinCache backend');
88  }
89 
94  {
95  $backend = $this->setUpBackend();
96  $data = 'Some data';
97  $identifier = $this->getUniqueId('MyIdentifier');
98  $backend->set($identifier, $data);
99  $otherData = 'some other data';
100  $backend->set($identifier, $otherData);
101  $fetchedData = $backend->get($identifier);
102  $this->assertEquals($otherData, $fetchedData, 'WinCache backend failed to overwrite and retrieve data');
103  }
104 
109  {
110  $backend = $this->setUpBackend();
111  $data = 'Some data';
112  $identifier = $this->getUniqueId('MyIdentifier');
113  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
114  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
115  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
116  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
117  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
118  }
119 
124  {
125  $backend = $this->setUpBackend();
126  $data = 'Some data';
127  $identifier = $this->getUniqueId('MyIdentifier');
128  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tagX']);
129  $backend->set($identifier, $data, ['UnitTestTag%tag3']);
130  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
131  $this->assertEquals([], $retrieved, 'Found entry which should no longer exist.');
132  }
133 
138  {
139  $backend = $this->setUpBackend();
140  $identifier = $this->getUniqueId('NonExistingIdentifier');
141  $inCache = $backend->has($identifier);
142  $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
143  }
144 
149  {
150  $backend = $this->setUpBackend();
151  $identifier = $this->getUniqueId('NonExistingIdentifier');
152  $inCache = $backend->remove($identifier);
153  $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
154  }
155 
160  {
161  $backend = $this->setUpBackend();
162  $data = 'some data' . microtime();
163  $backend->set('BackendWincacheTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
164  $backend->set('BackendWincacheTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
165  $backend->set('BackendWincacheTest3', $data, ['UnitTestTag%test']);
166  $backend->flushByTag('UnitTestTag%special');
167  $this->assertTrue($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
168  $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
169  $this->assertTrue($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
170  }
171 
176  {
177  $backend = $this->setUpBackend();
178  $data = 'some data' . microtime();
179  $backend->set('BackendWincacheTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
180  $backend->set('BackendWincacheTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
181  $backend->set('BackendWincacheTest3', $data, ['UnitTestTag%test']);
182  $backend->flushByTag('UnitTestTag%special', 'UnitTestTag%boring');
183  $this->assertTrue($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
184  $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
185  $this->assertTrue($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
186  }
187 
191  public function flushRemovesAllCacheEntries()
192  {
193  $backend = $this->setUpBackend();
194  $data = 'some data' . microtime();
195  $backend->set('BackendWincacheTest1', $data);
196  $backend->set('BackendWincacheTest2', $data);
197  $backend->set('BackendWincacheTest3', $data);
198  $backend->flush();
199  $this->assertFalse($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
200  $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
201  $this->assertFalse($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
202  }
203 
207  public function flushRemovesOnlyOwnEntries()
208  {
210  $thisCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
211  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
212  $thisBackend = new WincacheBackend('Testing');
213  $thisBackend->setCache($thisCache);
214 
216  $thatCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
217  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
218  $thatBackend = new WincacheBackend('Testing');
219  $thatBackend->setCache($thatCache);
220  $thisBackend->set('thisEntry', 'Hello');
221  $thatBackend->set('thatEntry', 'World!');
222  $thatBackend->flush();
223  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
224  $this->assertFalse($thatBackend->has('thatEntry'));
225  }
226 
232  public function largeDataIsStored()
233  {
234  $backend = $this->setUpBackend();
235  $data = str_repeat('abcde', 1024 * 1024);
236  $identifier = $this->getUniqueId('tooLargeData');
237  $backend->set($identifier, $data);
238  $this->assertTrue($backend->has($identifier));
239  $this->assertEquals($backend->get($identifier), $data);
240  }
241 
245  public function setTagsOnlyOnceToIdentifier()
246  {
247  $identifier = $this->getUniqueId('MyIdentifier');
248  $tags = ['UnitTestTag%test', 'UnitTestTag%boring'];
249 
250  $backend = $this->setUpBackend(true);
251  $backend->_call('addIdentifierToTags', $identifier, $tags);
252  $this->assertSame(
253  $tags,
254  $backend->_call('findTagsByIdentifier', $identifier)
255  );
256 
257  $backend->_call('addIdentifierToTags', $identifier, $tags);
258  $this->assertSame(
259  $tags,
260  $backend->_call('findTagsByIdentifier', $identifier)
261  );
262  }
263 
270  protected function setUpBackend($accessible = false)
271  {
273  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
274  if ($accessible) {
275  $accessibleClassName = $this->buildAccessibleProxy(WincacheBackend::class);
276  $backend = new $accessibleClassName('Testing');
277  } else {
278  $backend = new WincacheBackend('Testing');
279  }
280  $backend->setCache($cache);
281  return $backend;
282  }
283 }