TYPO3 CMS  TYPO3_7-6
XcacheBackendTest.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('xcache')) {
32  $this->markTestSkipped('xcache extension was not available');
33  }
34  if (php_sapi_name() === 'cli') {
35  $this->markTestSkipped('XCache is not supported in CLI mode.');
36  }
37  }
38 
44  {
45  $backend = new XcacheBackend('Testing');
46  $data = 'Some data';
47  $identifier = $this->getUniqueId('MyIdentifier');
48  $backend->set($identifier, $data);
49  }
50 
55  {
56  $backend = $this->setUpBackend();
57  $data = 'Some data';
58  $identifier = $this->getUniqueId('MyIdentifier');
59  $backend->set($identifier, $data);
60  $inCache = $backend->has($identifier);
61  $this->assertTrue($inCache, 'xcache backend failed to set and check entry');
62  }
63 
67  public function itIsPossibleToSetAndGetEntry()
68  {
69  $backend = $this->setUpBackend();
70  $data = 'Some data';
71  $identifier = $this->getUniqueId('MyIdentifier');
72  $backend->set($identifier, $data);
73  $fetchedData = $backend->get($identifier);
74  $this->assertEquals($data, $fetchedData, 'xcache backend failed to set and retrieve data');
75  }
76 
81  {
82  $backend = $this->setUpBackend();
83  $data = 'Some data';
84  $identifier = $this->getUniqueId('MyIdentifier');
85  $backend->set($identifier, $data);
86  $backend->remove($identifier);
87  $inCache = $backend->has($identifier);
88  $this->assertFalse($inCache, 'Failed to set and remove data from xcache backend');
89  }
90 
95  {
96  $backend = $this->setUpBackend();
97  $data = 'Some data';
98  $identifier = $this->getUniqueId('MyIdentifier');
99  $backend->set($identifier, $data);
100  $otherData = 'some other data';
101  $backend->set($identifier, $otherData);
102  $fetchedData = $backend->get($identifier);
103  $this->assertEquals($otherData, $fetchedData, 'xcache backend failed to overwrite and retrieve data');
104  }
105 
110  {
111  $backend = $this->setUpBackend();
112  $data = 'Some data';
113  $identifier = $this->getUniqueId('MyIdentifier');
114  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
115  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
116  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
117  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
118  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
119  }
120 
125  {
126  $backend = $this->setUpBackend();
127  $data = 'Some data';
128  $identifier = $this->getUniqueId('MyIdentifier');
129  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tagX']);
130  $backend->set($identifier, $data, ['UnitTestTag%tag3']);
131  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
132  $this->assertEquals([], $retrieved, 'Found entry which should no longer exist.');
133  }
134 
139  {
140  $backend = $this->setUpBackend();
141  $identifier = $this->getUniqueId('NonExistingIdentifier');
142  $inCache = $backend->has($identifier);
143  $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
144  }
145 
150  {
151  $backend = $this->setUpBackend();
152  $identifier = $this->getUniqueId('NonExistingIdentifier');
153  $inCache = $backend->remove($identifier);
154  $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
155  }
156 
161  {
162  $backend = $this->setUpBackend();
163  $data = 'some data' . microtime();
164  $backend->set('BackendXcacheTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
165  $backend->set('BackendXcacheTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
166  $backend->set('BackendXcacheTest3', $data, ['UnitTestTag%test']);
167  $backend->flushByTag('UnitTestTag%special');
168  $this->assertTrue($backend->has('BackendXcacheTest1'), 'BackendXcacheTest1');
169  $this->assertFalse($backend->has('BackendXcacheTest2'), 'BackendXcacheTest2');
170  $this->assertTrue($backend->has('BackendXcacheTest3'), 'BackendXcacheTest3');
171  }
172 
176  public function flushRemovesAllCacheEntries()
177  {
178  $backend = $this->setUpBackend();
179  $data = 'some data' . microtime();
180  $backend->set('BackendXcacheTest1', $data);
181  $backend->set('BackendXcacheTest2', $data);
182  $backend->set('BackendXcacheTest3', $data);
183  $backend->flush();
184  $this->assertFalse($backend->has('BackendXcacheTest1'), 'BackendXcacheTest1');
185  $this->assertFalse($backend->has('BackendXcacheTest2'), 'BackendXcacheTest2');
186  $this->assertFalse($backend->has('BackendXcacheTest3'), 'BackendXcacheTest3');
187  }
188 
192  public function flushRemovesOnlyOwnEntries()
193  {
195  $thisCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, [], [], '', false);
196  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
197  $thisBackend = new XcacheBackend('Testing');
198  $thisBackend->setCache($thisCache);
199 
201  $thatCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, [], [], '', false);
202  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
203  $thatBackend = new XcacheBackend('Testing');
204  $thatBackend->setCache($thatCache);
205  $thisBackend->set('thisEntry', 'Hello');
206  $thatBackend->set('thatEntry', 'World!');
207  $thatBackend->flush();
208  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
209  $this->assertFalse($thatBackend->has('thatEntry'));
210  }
211 
217  public function largeDataIsStored()
218  {
219  $backend = $this->setUpBackend();
220  $data = str_repeat('abcde', 1024 * 1024);
221  $identifier = $this->getUniqueId('tooLargeData');
222  $backend->set($identifier, $data);
223  $this->assertTrue($backend->has($identifier));
224  $this->assertEquals($backend->get($identifier), $data);
225  }
226 
230  public function setTagsOnlyOnceToIdentifier()
231  {
232  $identifier = $this->getUniqueId('MyIdentifier');
233  $tags = ['UnitTestTag%test', 'UnitTestTag%boring'];
234 
235  $backend = $this->setUpBackend(true);
236  $backend->_call('addIdentifierToTags', $identifier, $tags);
237  $this->assertSame(
238  $tags,
239  $backend->_call('findTagsByIdentifier', $identifier)
240  );
241 
242  $backend->_call('addIdentifierToTags', $identifier, $tags);
243  $this->assertSame(
244  $tags,
245  $backend->_call('findTagsByIdentifier', $identifier)
246  );
247  }
248 
255  protected function setUpBackend($accessible = false)
256  {
258  $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, [], [], '', false);
259  if ($accessible) {
260  $accessibleClassName = $this->buildAccessibleProxy(XcacheBackend::class);
261  $backend = new $accessibleClassName('Testing');
262  } else {
263  $backend = new XcacheBackend('Testing');
264  }
265  $backend->setCache($cache);
266  return $backend;
267  }
268 }