TYPO3 CMS  TYPO3_6-2
ApcBackendTest.php
Go to the documentation of this file.
1 <?php
3 
30 
36  public function setUp() {
37  // Currently APCu identifies itself both as "apcu" and "apc" (for compatibility) although it doesn't provide the APC-opcache functionality
38  if (!extension_loaded('apc') || ini_get('apc.enabled') == 0 || ini_get('apc.enable_cli') == 0) {
39  $this->markTestSkipped('APC/APCu extension was not available, or it was disabled for CLI.');
40  }
41  if (ini_get('apc.slam_defense') == 1) {
42  $this->markTestSkipped('This testcase can only be executed with apc.slam_defense = Off');
43  }
44  }
45 
51  $backend = new ApcBackend('Testing');
52  $data = 'Some data';
53  $identifier = $this->getUniqueId('MyIdentifier');
54  $backend->set($identifier, $data);
55  }
56 
61  $backend = $this->setUpBackend();
62  $data = 'Some data';
63  $identifier = $this->getUniqueId('MyIdentifier');
64  $backend->set($identifier, $data);
65  $inCache = $backend->has($identifier);
66  $this->assertTrue($inCache, 'APC backend failed to set and check entry');
67  }
68 
72  public function itIsPossibleToSetAndGetEntry() {
73  $backend = $this->setUpBackend();
74  $data = 'Some data';
75  $identifier = $this->getUniqueId('MyIdentifier');
76  $backend->set($identifier, $data);
77  $fetchedData = $backend->get($identifier);
78  $this->assertEquals($data, $fetchedData, 'APC backend failed to set and retrieve data');
79  }
80 
85  $backend = $this->setUpBackend();
86  $data = 'Some data';
87  $identifier = $this->getUniqueId('MyIdentifier');
88  $backend->set($identifier, $data);
89  $backend->remove($identifier);
90  $inCache = $backend->has($identifier);
91  $this->assertFalse($inCache, 'Failed to set and remove data from APC backend');
92  }
93 
98  $backend = $this->setUpBackend();
99  $data = 'Some data';
100  $identifier = $this->getUniqueId('MyIdentifier');
101  $backend->set($identifier, $data);
102  $otherData = 'some other data';
103  $backend->set($identifier, $otherData);
104  $fetchedData = $backend->get($identifier);
105  $this->assertEquals($otherData, $fetchedData, 'APC backend failed to overwrite and retrieve data');
106  }
107 
112  $backend = $this->setUpBackend();
113  $data = 'Some data';
114  $identifier = $this->getUniqueId('MyIdentifier');
115  $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
116  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
117  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
118  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
119  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
120  }
121 
125  public function setRemovesTagsFromPreviousSet() {
126  $backend = $this->setUpBackend();
127  $data = 'Some data';
128  $identifier = $this->getUniqueId('MyIdentifier');
129  $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
130  $backend->set($identifier, $data, array('UnitTestTag%tag3'));
131  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
132  $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
133  }
134 
138  public function setCacheIsSettingIdentifierPrefixWithCacheIdentifier() {
140  $cacheMock = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
141  $cacheMock->expects($this->any())->method('getIdentifier')->will($this->returnValue(
142  'testidentifier'
143  ));
144 
146  $backendMock = $this->getMock(
147  'TYPO3\\CMS\\Core\\Cache\\Backend\\ApcBackend',
148  array('setIdentifierPrefix','getCurrentUserData','getPathSite'),
149  array('testcontext')
150  );
151 
152  $backendMock->expects($this->once())->method('getCurrentUserData')->will(
153  $this->returnValue(array('name' => 'testname'))
154  );
155 
156  $backendMock->expects($this->once())->method('getPathSite')->will(
157  $this->returnValue('testpath')
158  );
159 
160  $expectedIdentifier = 'TYPO3_' . \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testpath' . 'testname' . 'testcontext' . 'testidentifier', 12);
161  $backendMock->expects($this->once())->method('setIdentifierPrefix')->with($expectedIdentifier);
162  $backendMock->setCache($cacheMock);
163  }
164 
169  $backend = $this->setUpBackend();
170  $identifier = $this->getUniqueId('NonExistingIdentifier');
171  $inCache = $backend->has($identifier);
172  $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
173  }
174 
179  $backend = $this->setUpBackend();
180  $identifier = $this->getUniqueId('NonExistingIdentifier');
181  $inCache = $backend->remove($identifier);
182  $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
183  }
184 
189  if (version_compare(phpversion(), '5.4.0', '<')) {
190  $this->markTestSkipped('This test is not reliable with PHP version below 5.4.0');
191  }
192  $backend = $this->setUpBackend();
193  $data = 'some data' . microtime();
194  $backend->set('BackendAPCTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
195  $backend->set('BackendAPCTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
196  $backend->set('BackendAPCTest3', $data, array('UnitTestTag%test'));
197  $backend->flushByTag('UnitTestTag%special');
198  $this->assertTrue($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
199  $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
200  $this->assertTrue($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
201  }
202 
206  public function flushRemovesAllCacheEntries() {
207  $backend = $this->setUpBackend();
208  $data = 'some data' . microtime();
209  $backend->set('BackendAPCTest1', $data);
210  $backend->set('BackendAPCTest2', $data);
211  $backend->set('BackendAPCTest3', $data);
212  $backend->flush();
213  $this->assertFalse($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
214  $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
215  $this->assertFalse($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
216  }
217 
221  public function flushRemovesOnlyOwnEntries() {
223  $thisCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
224  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
225  $thisBackend = new ApcBackend('Testing');
226  $thisBackend->setCache($thisCache);
227 
229  $thatCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
230  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
231  $thatBackend = new ApcBackend('Testing');
232  $thatBackend->setCache($thatCache);
233  $thisBackend->set('thisEntry', 'Hello');
234  $thatBackend->set('thatEntry', 'World!');
235  $thatBackend->flush();
236  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
237  $this->assertFalse($thatBackend->has('thatEntry'));
238  }
239 
245  public function largeDataIsStored() {
246  $backend = $this->setUpBackend();
247  $data = str_repeat('abcde', 1024 * 1024);
248  $identifier = $this->getUniqueId('tooLargeData');
249  $backend->set($identifier, $data);
250  $this->assertTrue($backend->has($identifier));
251  $this->assertEquals($backend->get($identifier), $data);
252  }
253 
257  public function setTagsOnlyOnceToIdentifier() {
258  $identifier = $this->getUniqueId('MyIdentifier');
259  $tags = array('UnitTestTag%test', 'UnitTestTag%boring');
260 
261  $backend = $this->setUpBackend(TRUE);
262  $backend->_call('addIdentifierToTags', $identifier, $tags);
263  $this->assertSame(
264  $tags,
265  $backend->_call('findTagsByIdentifier', $identifier)
266  );
267 
268  $backend->_call('addIdentifierToTags', $identifier, $tags);
269  $this->assertSame(
270  $tags,
271  $backend->_call('findTagsByIdentifier', $identifier)
272  );
273  }
274 
281  protected function setUpBackend($accessible = FALSE) {
283  $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
284  if ($accessible) {
285  $accessibleClassName = $this->buildAccessibleProxy('\\TYPO3\\CMS\\Core\\Cache\\Backend\\ApcBackend');
286  $backend = new $accessibleClassName('Testing');
287  } else {
288  $backend = new ApcBackend('Testing');
289  }
290  $backend->setCache($cache);
291  return $backend;
292  }
293 
294 }