TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
ApcuBackendTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Backend;
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 
23 
31 {
37  protected function setUp()
38  {
39  // APCu module is called apcu, but options are prefixed with apc
40  if (!extension_loaded('apcu') || !(bool)ini_get('apc.enabled') || !(bool)ini_get('apc.enable_cli')) {
41  $this->markTestSkipped('APCu extension was not available, or it was disabled for CLI.');
42  }
43  if ((bool)ini_get('apc.slam_defense')) {
44  $this->markTestSkipped('This testcase can only be executed with apc.slam_defense = 0');
45  }
46  }
47 
52  {
53  $backend = new ApcuBackend('Testing');
54  $data = 'Some data';
55  $identifier = $this->getUniqueId('MyIdentifier');
56  $this->expectException(Exception::class);
57  $this->expectExceptionCode(1232986118);
58  $backend->set($identifier, $data);
59  }
60 
65  {
66  $backend = $this->setUpBackend();
67  $data = 'Some data';
68  $identifier = $this->getUniqueId('MyIdentifier');
69  $backend->set($identifier, $data);
70  $this->assertTrue($backend->has($identifier));
71  }
72 
76  public function itIsPossibleToSetAndGetEntry()
77  {
78  $backend = $this->setUpBackend();
79  $data = 'Some data';
80  $identifier = $this->getUniqueId('MyIdentifier');
81  $backend->set($identifier, $data);
82  $fetchedData = $backend->get($identifier);
83  $this->assertEquals($data, $fetchedData);
84  }
85 
90  {
91  $backend = $this->setUpBackend();
92  $data = 'Some data';
93  $identifier = $this->getUniqueId('MyIdentifier');
94  $backend->set($identifier, $data);
95  $backend->remove($identifier);
96  $this->assertFalse($backend->has($identifier));
97  }
98 
103  {
104  $backend = $this->setUpBackend();
105  $data = 'Some data';
106  $identifier = $this->getUniqueId('MyIdentifier');
107  $backend->set($identifier, $data);
108  $otherData = 'some other data';
109  $backend->set($identifier, $otherData);
110  $fetchedData = $backend->get($identifier);
111  $this->assertEquals($otherData, $fetchedData);
112  }
113 
118  {
119  $backend = $this->setUpBackend();
120  $data = 'Some data';
121  $identifier = $this->getUniqueId('MyIdentifier');
122  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
123  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
124  $this->assertEquals($identifier, $retrieved[0]);
125  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
126  $this->assertEquals($identifier, $retrieved[0]);
127  }
128 
133  {
134  $backend = $this->setUpBackend();
135  $data = 'Some data';
136  $identifier = $this->getUniqueId('MyIdentifier');
137  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tagX']);
138  $backend->set($identifier, $data, ['UnitTestTag%tag3']);
139  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
140  $this->assertEquals([], $retrieved);
141  }
142 
146  public function setCacheIsSettingIdentifierPrefixWithCacheIdentifier()
147  {
149  $cacheMock = $this->createMock(FrontendInterface::class);
150  $cacheMock->expects($this->any())->method('getIdentifier')->will($this->returnValue(
151  'testidentifier'
152  ));
153 
155  $backendMock = $this->getMockBuilder(ApcuBackend::class)
156  ->setMethods(['setIdentifierPrefix', 'getCurrentUserData', 'getPathSite'])
157  ->setConstructorArgs(['testcontext'])
158  ->getMock();
159 
160  $backendMock->expects($this->once())->method('getCurrentUserData')->will(
161  $this->returnValue(['name' => 'testname'])
162  );
163 
164  $backendMock->expects($this->once())->method('getPathSite')->will(
165  $this->returnValue('testpath')
166  );
167 
168  $expectedIdentifier = 'TYPO3_' . GeneralUtility::shortMD5('testpath' . 'testname' . 'testcontext' . 'testidentifier', 12);
169  $backendMock->expects($this->once())->method('setIdentifierPrefix')->with($expectedIdentifier);
170  $backendMock->setCache($cacheMock);
171  }
172 
177  {
178  $backend = $this->setUpBackend();
179  $identifier = $this->getUniqueId('NonExistingIdentifier');
180  $this->assertFalse($backend->has($identifier));
181  }
182 
187  {
188  $backend = $this->setUpBackend();
189  $identifier = $this->getUniqueId('NonExistingIdentifier');
190  $this->assertFalse($backend->remove($identifier));
191  }
192 
197  {
198  $backend = $this->setUpBackend();
199  $data = 'some data' . microtime();
200  $backend->set('BackendAPCUTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
201  $backend->set('BackendAPCUTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
202  $backend->set('BackendAPCUTest3', $data, ['UnitTestTag%test']);
203  $backend->flushByTag('UnitTestTag%special');
204  $this->assertTrue($backend->has('BackendAPCUTest1'));
205  $this->assertFalse($backend->has('BackendAPCUTest2'));
206  $this->assertTrue($backend->has('BackendAPCUTest3'));
207  }
208 
213  {
214  $backend = $this->setUpBackend();
215  $data = 'some data' . microtime();
216  $backend->set('BackendAPCUTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
217  $backend->set('BackendAPCUTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
218  $backend->set('BackendAPCUTest3', $data, ['UnitTestTag%test']);
219  $backend->flushByTags(['UnitTestTag%special', 'UnitTestTag%boring']);
220  $this->assertFalse($backend->has('BackendAPCUTest1'), 'BackendAPCTest1');
221  $this->assertFalse($backend->has('BackendAPCUTest2'), 'BackendAPCTest2');
222  $this->assertTrue($backend->has('BackendAPCUTest3'), 'BackendAPCTest3');
223  }
224 
228  public function flushRemovesAllCacheEntries()
229  {
230  $backend = $this->setUpBackend();
231  $data = 'some data' . microtime();
232  $backend->set('BackendAPCUTest1', $data);
233  $backend->set('BackendAPCUTest2', $data);
234  $backend->set('BackendAPCUTest3', $data);
235  $backend->flush();
236  $this->assertFalse($backend->has('BackendAPCUTest1'));
237  $this->assertFalse($backend->has('BackendAPCUTest2'));
238  $this->assertFalse($backend->has('BackendAPCUTest3'));
239  }
240 
244  public function flushRemovesOnlyOwnEntries()
245  {
247  $thisCache = $this->createMock(FrontendInterface::class);
248  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
249  $thisBackend = new ApcuBackend('Testing');
250  $thisBackend->setCache($thisCache);
251 
253  $thatCache = $this->createMock(FrontendInterface::class);
254  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
255  $thatBackend = new ApcuBackend('Testing');
256  $thatBackend->setCache($thatCache);
257  $thisBackend->set('thisEntry', 'Hello');
258  $thatBackend->set('thatEntry', 'World!');
259  $thatBackend->flush();
260  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
261  $this->assertFalse($thatBackend->has('thatEntry'));
262  }
263 
269  public function largeDataIsStored()
270  {
271  $backend = $this->setUpBackend();
272  $data = str_repeat('abcde', 1024 * 1024);
273  $identifier = $this->getUniqueId('tooLargeData');
274  $backend->set($identifier, $data);
275  $this->assertTrue($backend->has($identifier));
276  $this->assertEquals($backend->get($identifier), $data);
277  }
278 
282  public function setTagsOnlyOnceToIdentifier()
283  {
284  $identifier = $this->getUniqueId('MyIdentifier');
285  $tags = ['UnitTestTag%test', 'UnitTestTag%boring'];
286 
287  $backend = $this->setUpBackend(true);
288  $backend->_call('addIdentifierToTags', $identifier, $tags);
289  $this->assertSame(
290  $tags,
291  $backend->_call('findTagsByIdentifier', $identifier)
292  );
293 
294  $backend->_call('addIdentifierToTags', $identifier, $tags);
295  $this->assertSame(
296  $tags,
297  $backend->_call('findTagsByIdentifier', $identifier)
298  );
299  }
300 
307  protected function setUpBackend($accessible = false)
308  {
310  $cache = $this->createMock(FrontendInterface::class);
311  if ($accessible) {
312  $accessibleClassName = $this->buildAccessibleProxy(ApcuBackend::class);
313  $backend = new $accessibleClassName('Testing');
314  } else {
315  $backend = new ApcuBackend('Testing');
316  }
317  $backend->setCache($cache);
318  return $backend;
319  }
320 }