TYPO3 CMS  TYPO3_7-6
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 
23 {
29  protected function setUp()
30  {
31  if (!extension_loaded('wincache')) {
32  $this->markTestSkipped('WinCache extension was not available');
33  }
34  }
35 
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 
173  public function flushRemovesAllCacheEntries()
174  {
175  $backend = $this->setUpBackend();
176  $data = 'some data' . microtime();
177  $backend->set('BackendWincacheTest1', $data);
178  $backend->set('BackendWincacheTest2', $data);
179  $backend->set('BackendWincacheTest3', $data);
180  $backend->flush();
181  $this->assertFalse($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
182  $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
183  $this->assertFalse($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
184  }
185 
189  public function flushRemovesOnlyOwnEntries()
190  {
192  $thisCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, [], [], '', false);
193  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
194  $thisBackend = new WincacheBackend('Testing');
195  $thisBackend->setCache($thisCache);
196 
198  $thatCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, [], [], '', false);
199  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
200  $thatBackend = new WincacheBackend('Testing');
201  $thatBackend->setCache($thatCache);
202  $thisBackend->set('thisEntry', 'Hello');
203  $thatBackend->set('thatEntry', 'World!');
204  $thatBackend->flush();
205  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
206  $this->assertFalse($thatBackend->has('thatEntry'));
207  }
208 
214  public function largeDataIsStored()
215  {
216  $backend = $this->setUpBackend();
217  $data = str_repeat('abcde', 1024 * 1024);
218  $identifier = $this->getUniqueId('tooLargeData');
219  $backend->set($identifier, $data);
220  $this->assertTrue($backend->has($identifier));
221  $this->assertEquals($backend->get($identifier), $data);
222  }
223 
227  public function setTagsOnlyOnceToIdentifier()
228  {
229  $identifier = $this->getUniqueId('MyIdentifier');
230  $tags = ['UnitTestTag%test', 'UnitTestTag%boring'];
231 
232  $backend = $this->setUpBackend(true);
233  $backend->_call('addIdentifierToTags', $identifier, $tags);
234  $this->assertSame(
235  $tags,
236  $backend->_call('findTagsByIdentifier', $identifier)
237  );
238 
239  $backend->_call('addIdentifierToTags', $identifier, $tags);
240  $this->assertSame(
241  $tags,
242  $backend->_call('findTagsByIdentifier', $identifier)
243  );
244  }
245 
252  protected function setUpBackend($accessible = false)
253  {
255  $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, [], [], '', false);
256  if ($accessible) {
257  $accessibleClassName = $this->buildAccessibleProxy(WincacheBackend::class);
258  $backend = new $accessibleClassName('Testing');
259  } else {
260  $backend = new WincacheBackend('Testing');
261  }
262  $backend->setCache($cache);
263  return $backend;
264  }
265 }