‪TYPO3CMS  10.4
WincacheBackendTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
29 class ‪WincacheBackendTest extends UnitTestCase
30 {
35  {
36  $this->expectException(Exception::class);
37  // @todo Add exception code with wincache extension
38 
39  $backend = new ‪WincacheBackend('Testing');
40  $data = 'Some data';
41  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
42  $backend->set($identifier, $data);
43  }
44 
49  {
50  $backend = $this->‪setUpBackend();
51  $data = 'Some data';
52  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
53  $backend->set($identifier, $data);
54  $inCache = $backend->has($identifier);
55  self::assertTrue($inCache, 'WinCache backend failed to set and check entry');
56  }
57 
62  {
63  $backend = $this->‪setUpBackend();
64  $data = 'Some data';
65  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
66  $backend->set($identifier, $data);
67  $fetchedData = $backend->get($identifier);
68  self::assertEquals($data, $fetchedData, 'Wincache backend failed to set and retrieve data');
69  }
70 
75  {
76  $backend = $this->‪setUpBackend();
77  $data = 'Some data';
78  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
79  $backend->set($identifier, $data);
80  $backend->remove($identifier);
81  $inCache = $backend->has($identifier);
82  self::assertFalse($inCache, 'Failed to set and remove data from WinCache backend');
83  }
84 
89  {
90  $backend = $this->‪setUpBackend();
91  $data = 'Some data';
92  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
93  $backend->set($identifier, $data);
94  $otherData = 'some other data';
95  $backend->set($identifier, $otherData);
96  $fetchedData = $backend->get($identifier);
97  self::assertEquals($otherData, $fetchedData, 'WinCache backend failed to overwrite and retrieve data');
98  }
99 
104  {
105  $backend = $this->‪setUpBackend();
106  $data = 'Some data';
107  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
108  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
109  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
110  self::assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
111  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
112  self::assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
113  }
114 
119  {
120  $backend = $this->‪setUpBackend();
121  $data = 'Some data';
122  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
123  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tagX']);
124  $backend->set($identifier, $data, ['UnitTestTag%tag3']);
125  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
126  self::assertEquals([], $retrieved, 'Found entry which should no longer exist.');
127  }
128 
133  {
134  $backend = $this->‪setUpBackend();
135  $identifier = ‪StringUtility::getUniqueId('NonExistingIdentifier');
136  $inCache = $backend->has($identifier);
137  self::assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
138  }
139 
144  {
145  $backend = $this->‪setUpBackend();
146  $identifier = ‪StringUtility::getUniqueId('NonExistingIdentifier');
147  $inCache = $backend->remove($identifier);
148  self::assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
149  }
150 
155  {
156  $backend = $this->‪setUpBackend();
157  $data = 'some data' . microtime();
158  $backend->set('BackendWincacheTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
159  $backend->set('BackendWincacheTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
160  $backend->set('BackendWincacheTest3', $data, ['UnitTestTag%test']);
161  $backend->flushByTag('UnitTestTag%special');
162  self::assertTrue($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
163  self::assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
164  self::assertTrue($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
165  }
166 
171  {
172  $backend = $this->‪setUpBackend();
173  $data = 'some data' . microtime();
174  $backend->set('BackendWincacheTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
175  $backend->set('BackendWincacheTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
176  $backend->set('BackendWincacheTest3', $data, ['UnitTestTag%test']);
177  $backend->flushByTag('UnitTestTag%special', 'UnitTestTag%boring');
178  self::assertTrue($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
179  self::assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
180  self::assertTrue($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
181  }
182 
187  {
188  $backend = $this->‪setUpBackend();
189  $data = 'some data' . microtime();
190  $backend->set('BackendWincacheTest1', $data);
191  $backend->set('BackendWincacheTest2', $data);
192  $backend->set('BackendWincacheTest3', $data);
193  $backend->flush();
194  self::assertFalse($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
195  self::assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
196  self::assertFalse($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
197  }
198 
203  {
205  $thisCache = $this->createMock(FrontendInterface::class);
206  $thisCache->expects(self::any())->method('getIdentifier')->willReturn('thisCache');
207  $thisBackend = new ‪WincacheBackend('Testing');
208  $thisBackend->setCache($thisCache);
209 
211  $thatCache = $this->createMock(FrontendInterface::class);
212  $thatCache->expects(self::any())->method('getIdentifier')->willReturn('thatCache');
213  $thatBackend = new ‪WincacheBackend('Testing');
214  $thatBackend->setCache($thatCache);
215  $thisBackend->set('thisEntry', 'Hello');
216  $thatBackend->set('thatEntry', 'World!');
217  $thatBackend->flush();
218  self::assertEquals('Hello', $thisBackend->get('thisEntry'));
219  self::assertFalse($thatBackend->has('thatEntry'));
220  }
221 
227  public function ‪largeDataIsStored()
228  {
229  $backend = $this->‪setUpBackend();
230  $data = str_repeat('abcde', 1024 * 1024);
231  $identifier = ‪StringUtility::getUniqueId('tooLargeData');
232  $backend->set($identifier, $data);
233  self::assertTrue($backend->has($identifier));
234  self::assertEquals($backend->get($identifier), $data);
235  }
236 
241  {
242  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
243  $tags = ['UnitTestTag%test', 'UnitTestTag%boring'];
244 
245  $backend = $this->‪setUpBackend(true);
246  $backend->_call('addIdentifierToTags', $identifier, $tags);
247  self::assertSame(
248  $tags,
249  $backend->_call('findTagsByIdentifier', $identifier)
250  );
251 
252  $backend->_call('addIdentifierToTags', $identifier, $tags);
253  self::assertSame(
254  $tags,
255  $backend->_call('findTagsByIdentifier', $identifier)
256  );
257  }
258 
265  protected function ‪setUpBackend($accessible = false)
266  {
268  $cache = $this->createMock(FrontendInterface::class);
269  if ($accessible) {
270  $backend = $this->getAccessibleMock(WincacheBackend::class, ['dummy'], ['Testing']);
271  } else {
272  $backend = new ‪WincacheBackend('Testing');
273  }
274  $backend->setCache($cache);
275  return $backend;
276  }
277 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\itIsPossibleToRemoveEntryFromCache
‪itIsPossibleToRemoveEntryFromCache()
Definition: WincacheBackendTest.php:74
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\largeDataIsStored
‪largeDataIsStored()
Definition: WincacheBackendTest.php:227
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\setUpBackend
‪TYPO3 TestingFramework Core AccessibleObjectInterface WincacheBackend setUpBackend($accessible=false)
Definition: WincacheBackendTest.php:265
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\flushRemovesOnlyOwnEntries
‪flushRemovesOnlyOwnEntries()
Definition: WincacheBackendTest.php:202
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest
Definition: WincacheBackendTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend
Definition: AbstractBackendTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\setThrowsExceptionIfNoFrontEndHasBeenSet
‪setThrowsExceptionIfNoFrontEndHasBeenSet()
Definition: WincacheBackendTest.php:34
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend
Definition: WincacheBackend.php:43
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\hasReturnsFalseIfTheEntryDoesntExist
‪hasReturnsFalseIfTheEntryDoesntExist()
Definition: WincacheBackendTest.php:132
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\removeReturnsFalseIfTheEntryDoesntExist
‪removeReturnsFalseIfTheEntryDoesntExist()
Definition: WincacheBackendTest.php:143
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\itIsPossibleToSetAndGetEntry
‪itIsPossibleToSetAndGetEntry()
Definition: WincacheBackendTest.php:61
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\itIsPossibleToOverwriteAnEntryInTheCache
‪itIsPossibleToOverwriteAnEntryInTheCache()
Definition: WincacheBackendTest.php:88
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\setTagsOnlyOnceToIdentifier
‪setTagsOnlyOnceToIdentifier()
Definition: WincacheBackendTest.php:240
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:16
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\flushByTagRemovesCacheEntriesWithSpecifiedTag
‪flushByTagRemovesCacheEntriesWithSpecifiedTag()
Definition: WincacheBackendTest.php:154
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\setRemovesTagsFromPreviousSet
‪setRemovesTagsFromPreviousSet()
Definition: WincacheBackendTest.php:118
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\findIdentifiersByTagFindsSetEntries
‪findIdentifiersByTagFindsSetEntries()
Definition: WincacheBackendTest.php:103
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\itIsPossibleToSetAndCheckExistenceInCache
‪itIsPossibleToSetAndCheckExistenceInCache()
Definition: WincacheBackendTest.php:48
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\flushByTagsRemovesCacheEntriesWithSpecifiedTags
‪flushByTagsRemovesCacheEntriesWithSpecifiedTags()
Definition: WincacheBackendTest.php:170
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\WincacheBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: WincacheBackendTest.php:186