TYPO3 CMS  TYPO3_6-2
WincacheBackendTest.php
Go to the documentation of this file.
1 <?php
3 
18 
25 
31  public function setUp() {
32  if (!extension_loaded('wincache')) {
33  $this->markTestSkipped('WinCache extension was not available');
34  }
35  }
36 
42  $backend = new WincacheBackend('Testing');
43  $data = 'Some data';
44  $identifier = $this->getUniqueId('MyIdentifier');
45  $backend->set($identifier, $data);
46  }
47 
52  $backend = $this->setUpBackend();
53  $data = 'Some data';
54  $identifier = $this->getUniqueId('MyIdentifier');
55  $backend->set($identifier, $data);
56  $inCache = $backend->has($identifier);
57  $this->assertTrue($inCache, 'WinCache backend failed to set and check entry');
58  }
59 
63  public function itIsPossibleToSetAndGetEntry() {
64  $backend = $this->setUpBackend();
65  $data = 'Some data';
66  $identifier = $this->getUniqueId('MyIdentifier');
67  $backend->set($identifier, $data);
68  $fetchedData = $backend->get($identifier);
69  $this->assertEquals($data, $fetchedData, 'Winache backend failed to set and retrieve data');
70  }
71 
76  $backend = $this->setUpBackend();
77  $data = 'Some data';
78  $identifier = $this->getUniqueId('MyIdentifier');
79  $backend->set($identifier, $data);
80  $backend->remove($identifier);
81  $inCache = $backend->has($identifier);
82  $this->assertFalse($inCache, 'Failed to set and remove data from WinCache backend');
83  }
84 
89  $backend = $this->setUpBackend();
90  $data = 'Some data';
91  $identifier = $this->getUniqueId('MyIdentifier');
92  $backend->set($identifier, $data);
93  $otherData = 'some other data';
94  $backend->set($identifier, $otherData);
95  $fetchedData = $backend->get($identifier);
96  $this->assertEquals($otherData, $fetchedData, 'WinCache backend failed to overwrite and retrieve data');
97  }
98 
103  $backend = $this->setUpBackend();
104  $data = 'Some data';
105  $identifier = $this->getUniqueId('MyIdentifier');
106  $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
107  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
108  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
109  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
110  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
111  }
112 
116  public function setRemovesTagsFromPreviousSet() {
117  $backend = $this->setUpBackend();
118  $data = 'Some data';
119  $identifier = $this->getUniqueId('MyIdentifier');
120  $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
121  $backend->set($identifier, $data, array('UnitTestTag%tag3'));
122  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
123  $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
124  }
125 
130  $backend = $this->setUpBackend();
131  $identifier = $this->getUniqueId('NonExistingIdentifier');
132  $inCache = $backend->has($identifier);
133  $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
134  }
135 
140  $backend = $this->setUpBackend();
141  $identifier = $this->getUniqueId('NonExistingIdentifier');
142  $inCache = $backend->remove($identifier);
143  $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
144  }
145 
150  $backend = $this->setUpBackend();
151  $data = 'some data' . microtime();
152  $backend->set('BackendWincacheTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
153  $backend->set('BackendWincacheTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
154  $backend->set('BackendWincacheTest3', $data, array('UnitTestTag%test'));
155  $backend->flushByTag('UnitTestTag%special');
156  $this->assertTrue($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
157  $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
158  $this->assertTrue($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
159  }
160 
164  public function flushRemovesAllCacheEntries() {
165  $backend = $this->setUpBackend();
166  $data = 'some data' . microtime();
167  $backend->set('BackendWincacheTest1', $data);
168  $backend->set('BackendWincacheTest2', $data);
169  $backend->set('BackendWincacheTest3', $data);
170  $backend->flush();
171  $this->assertFalse($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
172  $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
173  $this->assertFalse($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
174  }
175 
179  public function flushRemovesOnlyOwnEntries() {
181  $thisCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
182  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
183  $thisBackend = new WincacheBackend('Testing');
184  $thisBackend->setCache($thisCache);
185 
187  $thatCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
188  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
189  $thatBackend = new WincacheBackend('Testing');
190  $thatBackend->setCache($thatCache);
191  $thisBackend->set('thisEntry', 'Hello');
192  $thatBackend->set('thatEntry', 'World!');
193  $thatBackend->flush();
194  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
195  $this->assertFalse($thatBackend->has('thatEntry'));
196  }
197 
203  public function largeDataIsStored() {
204  $backend = $this->setUpBackend();
205  $data = str_repeat('abcde', 1024 * 1024);
206  $identifier = $this->getUniqueId('tooLargeData');
207  $backend->set($identifier, $data);
208  $this->assertTrue($backend->has($identifier));
209  $this->assertEquals($backend->get($identifier), $data);
210  }
211 
215  public function setTagsOnlyOnceToIdentifier() {
216  $identifier = $this->getUniqueId('MyIdentifier');
217  $tags = array('UnitTestTag%test', 'UnitTestTag%boring');
218 
219  $backend = $this->setUpBackend(TRUE);
220  $backend->_call('addIdentifierToTags', $identifier, $tags);
221  $this->assertSame(
222  $tags,
223  $backend->_call('findTagsByIdentifier', $identifier)
224  );
225 
226  $backend->_call('addIdentifierToTags', $identifier, $tags);
227  $this->assertSame(
228  $tags,
229  $backend->_call('findTagsByIdentifier', $identifier)
230  );
231  }
232 
239  protected function setUpBackend($accessible = FALSE) {
241  $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
242  if ($accessible) {
243  $accessibleClassName = $this->buildAccessibleProxy('\\TYPO3\\CMS\\Core\\Cache\\Backend\\WincacheBackend');
244  $backend = new $accessibleClassName('Testing');
245  } else {
246  $backend = new WincacheBackend('Testing');
247  }
248  $backend->setCache($cache);
249  return $backend;
250  }
251 
252 }