TYPO3 CMS  TYPO3_6-2
MemcachedBackendTest.php
Go to the documentation of this file.
1 <?php
3 
18 
27 
33  public function setUp() {
34  if (!extension_loaded('memcache')) {
35  $this->markTestSkipped('memcache extension was not available');
36  }
37  try {
38  if (!@fsockopen('localhost', 11211)) {
39  $this->markTestSkipped('memcached not reachable');
40  }
41  } catch (\Exception $e) {
42  $this->markTestSkipped('memcached not reachable');
43  }
44  }
45 
51  $backendOptions = array('servers' => array('localhost:11211'));
52  $backend = new MemcachedBackend('Testing', $backendOptions);
53  $backend->initializeObject();
54  $data = 'Some data';
55  $identifier = $this->getUniqueId('MyIdentifier');
56  $backend->set($identifier, $data);
57  }
58 
64  $backend = new MemcachedBackend('Testing');
65  $backend->initializeObject();
66  }
67 
72  $backend = $this->setUpBackend();
73  $data = 'Some data';
74  $identifier = $this->getUniqueId('MyIdentifier');
75  $backend->set($identifier, $data);
76  $inCache = $backend->has($identifier);
77  $this->assertTrue($inCache, 'Memcache failed to set and check entry');
78  }
79 
83  public function itIsPossibleToSetAndGetEntry() {
84  $backend = $this->setUpBackend();
85  $data = 'Some data';
86  $identifier = $this->getUniqueId('MyIdentifier');
87  $backend->set($identifier, $data);
88  $fetchedData = $backend->get($identifier);
89  $this->assertEquals($data, $fetchedData, 'Memcache failed to set and retrieve data');
90  }
91 
96  $backend = $this->setUpBackend();
97  $data = 'Some data';
98  $identifier = $this->getUniqueId('MyIdentifier');
99  $backend->set($identifier, $data);
100  $backend->remove($identifier);
101  $inCache = $backend->has($identifier);
102  $this->assertFalse($inCache, 'Failed to set and remove data from Memcache');
103  }
104 
109  $backend = $this->setUpBackend();
110  $data = 'Some data';
111  $identifier = $this->getUniqueId('MyIdentifier');
112  $backend->set($identifier, $data);
113  $otherData = 'some other data';
114  $backend->set($identifier, $otherData);
115  $fetchedData = $backend->get($identifier);
116  $this->assertEquals($otherData, $fetchedData, 'Memcache failed to overwrite and retrieve data');
117  }
118 
123  $backend = $this->setUpBackend();
124  $data = 'Some data';
125  $identifier = $this->getUniqueId('MyIdentifier');
126  $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
127  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
128  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
129  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
130  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
131  }
132 
136  public function setRemovesTagsFromPreviousSet() {
137  $backend = $this->setUpBackend();
138  $data = 'Some data';
139  $identifier = $this->getUniqueId('MyIdentifier');
140  $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
141  $backend->set($identifier, $data, array('UnitTestTag%tag3'));
142  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
143  $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
144  }
145 
150  $backend = $this->setUpBackend();
151  $identifier = $this->getUniqueId('NonExistingIdentifier');
152  $inCache = $backend->has($identifier);
153  $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
154  }
155 
160  $backend = $this->setUpBackend();
161  $identifier = $this->getUniqueId('NonExistingIdentifier');
162  $inCache = $backend->remove($identifier);
163  $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
164  }
165 
170  $backend = $this->setUpBackend();
171  $data = 'some data' . microtime();
172  $backend->set('BackendMemcacheTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
173  $backend->set('BackendMemcacheTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
174  $backend->set('BackendMemcacheTest3', $data, array('UnitTestTag%test'));
175  $backend->flushByTag('UnitTestTag%special');
176  $this->assertTrue($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
177  $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
178  $this->assertTrue($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
179  }
180 
184  public function flushRemovesAllCacheEntries() {
185  $backend = $this->setUpBackend();
186  $data = 'some data' . microtime();
187  $backend->set('BackendMemcacheTest1', $data);
188  $backend->set('BackendMemcacheTest2', $data);
189  $backend->set('BackendMemcacheTest3', $data);
190  $backend->flush();
191  $this->assertFalse($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
192  $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
193  $this->assertFalse($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
194  }
195 
199  public function flushRemovesOnlyOwnEntries() {
200  $backendOptions = array('servers' => array('localhost:11211'));
202  $thisCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
203  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
204  $thisBackend = new MemcachedBackend('Testing', $backendOptions);
205  $thisBackend->setCache($thisCache);
206  $thisBackend->initializeObject();
207 
209  $thatCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
210  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
211  $thatBackend = new MemcachedBackend('Testing', $backendOptions);
212  $thatBackend->setCache($thatCache);
213  $thatBackend->initializeObject();
214  $thisBackend->set('thisEntry', 'Hello');
215  $thatBackend->set('thatEntry', 'World!');
216  $thatBackend->flush();
217  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
218  $this->assertFalse($thatBackend->has('thatEntry'));
219  }
220 
227  public function largeDataIsStored() {
228  $backend = $this->setUpBackend();
229  $data = str_repeat('abcde', 1024 * 1024);
230  $backend->set('tooLargeData', $data);
231  $this->assertTrue($backend->has('tooLargeData'));
232  $this->assertEquals($backend->get('tooLargeData'), $data);
233  }
234 
238  public function setTagsOnlyOnceToIdentifier() {
239  $backendOptions = array('servers' => array('localhost:11211'));
240  $identifier = $this->getUniqueId('MyIdentifier');
241  $tags = array('UnitTestTag%test', 'UnitTestTag%boring');
242 
243  $backend = $this->setUpBackend($backendOptions, TRUE);
244  $backend->_call('addIdentifierToTags', $identifier, $tags);
245  $this->assertSame(
246  $tags,
247  $backend->_call('findTagsByIdentifier', $identifier)
248  );
249 
250  $backend->_call('addIdentifierToTags', $identifier, $tags);
251  $this->assertSame(
252  $tags,
253  $backend->_call('findTagsByIdentifier', $identifier)
254  );
255  }
256 
264  protected function setUpBackend(array $backendOptions = array(), $accessible = FALSE) {
266  $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
267  if ($backendOptions == array()) {
268  $backendOptions = array('servers' => array('localhost:11211'));
269  }
270  if ($accessible) {
271  $accessibleClassName = $this->buildAccessibleProxy('\\TYPO3\\CMS\\Core\\Cache\\Backend\\MemcachedBackend');
272  $backend = new $accessibleClassName('Testing', $backendOptions);
273  } else {
274  $backend = new MemcachedBackend('Testing', $backendOptions);
275  }
276  $backend->setCache($cache);
277  $backend->initializeObject();
278  return $backend;
279  }
280 
281 }