TYPO3 CMS  TYPO3_6-2
XcacheBackendTest.php
Go to the documentation of this file.
1 <?php
3 
18 
25 
31  public function setUp() {
32  if (!extension_loaded('xcache')) {
33  $this->markTestSkipped('xcache extension was not available');
34  }
35  if (php_sapi_name() === 'cli') {
36  $this->markTestSkipped('XCache is not supported in CLI mode.');
37  }
38  }
39 
45  $backend = new XcacheBackend('Testing');
46  $data = 'Some data';
47  $identifier = $this->getUniqueId('MyIdentifier');
48  $backend->set($identifier, $data);
49  }
50 
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, 'xcache backend failed to set and check entry');
61  }
62 
66  public function itIsPossibleToSetAndGetEntry() {
67  $backend = $this->setUpBackend();
68  $data = 'Some data';
69  $identifier = $this->getUniqueId('MyIdentifier');
70  $backend->set($identifier, $data);
71  $fetchedData = $backend->get($identifier);
72  $this->assertEquals($data, $fetchedData, 'xcache backend failed to set and retrieve data');
73  }
74 
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 xcache backend');
86  }
87 
92  $backend = $this->setUpBackend();
93  $data = 'Some data';
94  $identifier = $this->getUniqueId('MyIdentifier');
95  $backend->set($identifier, $data);
96  $otherData = 'some other data';
97  $backend->set($identifier, $otherData);
98  $fetchedData = $backend->get($identifier);
99  $this->assertEquals($otherData, $fetchedData, 'xcache backend failed to overwrite and retrieve data');
100  }
101 
106  $backend = $this->setUpBackend();
107  $data = 'Some data';
108  $identifier = $this->getUniqueId('MyIdentifier');
109  $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
110  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
111  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
112  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
113  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
114  }
115 
119  public function setRemovesTagsFromPreviousSet() {
120  $backend = $this->setUpBackend();
121  $data = 'Some data';
122  $identifier = $this->getUniqueId('MyIdentifier');
123  $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
124  $backend->set($identifier, $data, array('UnitTestTag%tag3'));
125  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
126  $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
127  }
128 
133  $backend = $this->setUpBackend();
134  $identifier = $this->getUniqueId('NonExistingIdentifier');
135  $inCache = $backend->has($identifier);
136  $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
137  }
138 
143  $backend = $this->setUpBackend();
144  $identifier = $this->getUniqueId('NonExistingIdentifier');
145  $inCache = $backend->remove($identifier);
146  $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
147  }
148 
153  $backend = $this->setUpBackend();
154  $data = 'some data' . microtime();
155  $backend->set('BackendXcacheTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
156  $backend->set('BackendXcacheTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
157  $backend->set('BackendXcacheTest3', $data, array('UnitTestTag%test'));
158  $backend->flushByTag('UnitTestTag%special');
159  $this->assertTrue($backend->has('BackendXcacheTest1'), 'BackendXcacheTest1');
160  $this->assertFalse($backend->has('BackendXcacheTest2'), 'BackendXcacheTest2');
161  $this->assertTrue($backend->has('BackendXcacheTest3'), 'BackendXcacheTest3');
162  }
163 
167  public function flushRemovesAllCacheEntries() {
168  $backend = $this->setUpBackend();
169  $data = 'some data' . microtime();
170  $backend->set('BackendXcacheTest1', $data);
171  $backend->set('BackendXcacheTest2', $data);
172  $backend->set('BackendXcacheTest3', $data);
173  $backend->flush();
174  $this->assertFalse($backend->has('BackendXcacheTest1'), 'BackendXcacheTest1');
175  $this->assertFalse($backend->has('BackendXcacheTest2'), 'BackendXcacheTest2');
176  $this->assertFalse($backend->has('BackendXcacheTest3'), 'BackendXcacheTest3');
177  }
178 
182  public function flushRemovesOnlyOwnEntries() {
184  $thisCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
185  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
186  $thisBackend = new XcacheBackend('Testing');
187  $thisBackend->setCache($thisCache);
188 
190  $thatCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
191  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
192  $thatBackend = new XcacheBackend('Testing');
193  $thatBackend->setCache($thatCache);
194  $thisBackend->set('thisEntry', 'Hello');
195  $thatBackend->set('thatEntry', 'World!');
196  $thatBackend->flush();
197  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
198  $this->assertFalse($thatBackend->has('thatEntry'));
199  }
200 
206  public function largeDataIsStored() {
207  $backend = $this->setUpBackend();
208  $data = str_repeat('abcde', 1024 * 1024);
209  $identifier = $this->getUniqueId('tooLargeData');
210  $backend->set($identifier, $data);
211  $this->assertTrue($backend->has($identifier));
212  $this->assertEquals($backend->get($identifier), $data);
213  }
214 
218  public function setTagsOnlyOnceToIdentifier() {
219  $identifier = $this->getUniqueId('MyIdentifier');
220  $tags = array('UnitTestTag%test', 'UnitTestTag%boring');
221 
222  $backend = $this->setUpBackend(TRUE);
223  $backend->_call('addIdentifierToTags', $identifier, $tags);
224  $this->assertSame(
225  $tags,
226  $backend->_call('findTagsByIdentifier', $identifier)
227  );
228 
229  $backend->_call('addIdentifierToTags', $identifier, $tags);
230  $this->assertSame(
231  $tags,
232  $backend->_call('findTagsByIdentifier', $identifier)
233  );
234  }
235 
242  protected function setUpBackend($accessible = FALSE) {
244  $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
245  if ($accessible) {
246  $accessibleClassName = $this->buildAccessibleProxy('\\TYPO3\\CMS\\Core\\Cache\\Backend\\XcacheBackend');
247  $backend = new $accessibleClassName('Testing');
248  } else {
249  $backend = new XcacheBackend('Testing');
250  }
251  $backend->setCache($cache);
252  return $backend;
253  }
254 
255 }