‪TYPO3CMS  10.4
MemcachedBackendTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
23 
27 class ‪MemcachedBackendTest extends FunctionalTestCase
28 {
32  protected function ‪setUp(): void
33  {
34  if (!extension_loaded('memcache') && !extension_loaded('memcached')) {
35  self::markTestSkipped('Neither "memcache" nor "memcached" extension was available');
36  }
37  if (!getenv('typo3TestingMemcachedHost')) {
38  self::markTestSkipped('environment variable "typo3TestingMemcachedHost" must be set to run this test');
39  }
40  // Note we assume that if that typo3TestingMemcachedHost env is set, we can use that for testing,
41  // there is no test to see if the daemon is actually up and running. Tests will fail if env
42  // is set but daemon is down.
43 
44  parent::setUp();
45  }
46 
51  {
52  // We know this env is set, otherwise setUp() would skip the tests
53  $memcachedHost = getenv('typo3TestingMemcachedHost');
54  // If typo3TestingMemcachedPort env is set, use it, otherwise fall back to standard port
55  $env = getenv('typo3TestingMemcachedPort');
56  $memcachedPort = is_string($env) ? (int)$env : 11211;
57 
58  $subject = new ‪MemcachedBackend('Testing', [ 'servers' => [$memcachedHost . ':' . $memcachedPort] ]);
59  $subject->initializeObject();
60  return $subject;
61  }
62 
67  {
68  $subject = $this->‪initializeSubject();
69 
70  $this->expectException(Exception::class);
71  $this->expectExceptionCode(1207149215);
72 
73  $subject->set(‪StringUtility::getUniqueId('MyIdentifier'), 'some data');
74  }
75 
80  {
81  $subject = new ‪MemcachedBackend('Testing');
82  $this->expectException(Exception::class);
83  $this->expectExceptionCode(1213115903);
84  $subject->initializeObject();
85  }
86 
91  {
92  $frontendProphecy = $this->prophesize(FrontendInterface::class);
93  $frontendProphecy->getIdentifier()->willReturn('pages');
94 
95  $subject = $this->‪initializeSubject();
96  $subject->setCache($frontendProphecy->reveal());
97 
98  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
99  $subject->set($identifier, 'Some data');
100  self::assertTrue($subject->has($identifier));
101  }
102 
107  {
108  $frontendProphecy = $this->prophesize(FrontendInterface::class);
109  $frontendProphecy->getIdentifier()->willReturn('pages');
110 
111  $subject = $this->‪initializeSubject();
112  $subject->setCache($frontendProphecy->reveal());
113 
114  $data = 'Some data';
115  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
116  $subject->set($identifier, $data);
117  self::assertEquals($data, $subject->get($identifier));
118  }
119 
124  {
125  $frontendProphecy = $this->prophesize(FrontendInterface::class);
126  $frontendProphecy->getIdentifier()->willReturn('pages');
127 
128  $subject = $this->‪initializeSubject();
129  $subject->setCache($frontendProphecy->reveal());
130 
131  $data = [
132  'string' => 'Serialize a string',
133  'integer' => 0,
134  'anotherIntegerValue' => 123456,
135  'float' => 12.34,
136  'bool' => true,
137  'array' => [
138  0 => 'test',
139  1 => 'another test',
140  ],
141  ];
142 
143  $subject->set('myIdentifier', $data);
144  self::assertSame($data, $subject->get('myIdentifier'));
145  }
146 
152  public function ‪largeDataIsStored()
153  {
154  $frontendProphecy = $this->prophesize(FrontendInterface::class);
155  $frontendProphecy->getIdentifier()->willReturn('pages');
156 
157  $subject = $this->‪initializeSubject();
158  $subject->setCache($frontendProphecy->reveal());
159 
160  $data = str_repeat('abcde', 1024 * 1024);
161  $subject->set('tooLargeData', $data);
162  self::assertTrue($subject->has('tooLargeData'));
163  self::assertEquals($subject->get('tooLargeData'), $data);
164  }
165 
170  {
171  $frontendProphecy = $this->prophesize(FrontendInterface::class);
172  $frontendProphecy->getIdentifier()->willReturn('pages');
173 
174  $subject = $this->‪initializeSubject();
175  $subject->setCache($frontendProphecy->reveal());
176 
177  $data = 'Some data';
178  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
179  $subject->set($identifier, $data);
180  $subject->remove($identifier);
181  self::assertFalse($subject->has($identifier));
182  }
183 
188  {
189  $frontendProphecy = $this->prophesize(FrontendInterface::class);
190  $frontendProphecy->getIdentifier()->willReturn('pages');
191 
192  $subject = $this->‪initializeSubject();
193  $subject->setCache($frontendProphecy->reveal());
194 
195  $data = 'Some data';
196  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
197  $subject->set($identifier, $data);
198  $otherData = 'some other data';
199  $subject->set($identifier, $otherData);
200  self::assertEquals($otherData, $subject->get($identifier));
201  }
202 
207  {
208  $frontendProphecy = $this->prophesize(FrontendInterface::class);
209  $frontendProphecy->getIdentifier()->willReturn('pages');
210 
211  $subject = $this->‪initializeSubject();
212  $subject->setCache($frontendProphecy->reveal());
213 
214  $data = 'Some data';
215  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
216  $subject->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
217  $retrieved = $subject->findIdentifiersByTag('UnitTestTag%tag1');
218  self::assertEquals($identifier, $retrieved[0]);
219  $retrieved = $subject->findIdentifiersByTag('UnitTestTag%tag2');
220  self::assertEquals($identifier, $retrieved[0]);
221  }
222 
227  {
228  $frontendProphecy = $this->prophesize(FrontendInterface::class);
229  $frontendProphecy->getIdentifier()->willReturn('pages');
230 
231  $subject = $this->‪initializeSubject();
232  $subject->setCache($frontendProphecy->reveal());
233 
234  $data = 'Some data';
235  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
236  $subject->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
237  $subject->set($identifier, $data, ['UnitTestTag%tag3']);
238  self::assertEquals([], $subject->findIdentifiersByTag('UnitTestTag%tagX'));
239  }
240 
245  {
246  $frontendProphecy = $this->prophesize(FrontendInterface::class);
247  $frontendProphecy->getIdentifier()->willReturn('pages');
248 
249  $subject = $this->‪initializeSubject();
250  $subject->setCache($frontendProphecy->reveal());
251 
252  $identifier = ‪StringUtility::getUniqueId('NonExistingIdentifier');
253  self::assertFalse($subject->has($identifier));
254  }
255 
260  {
261  $frontendProphecy = $this->prophesize(FrontendInterface::class);
262  $frontendProphecy->getIdentifier()->willReturn('pages');
263 
264  $subject = $this->‪initializeSubject();
265  $subject->setCache($frontendProphecy->reveal());
266 
267  $identifier = ‪StringUtility::getUniqueId('NonExistingIdentifier');
268  self::assertFalse($subject->remove($identifier));
269  }
270 
275  {
276  $frontendProphecy = $this->prophesize(FrontendInterface::class);
277  $frontendProphecy->getIdentifier()->willReturn('pages');
278 
279  $subject = $this->‪initializeSubject();
280  $subject->setCache($frontendProphecy->reveal());
281 
282  $data = 'some data' . microtime();
283  $subject->set('BackendMemcacheTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
284  $subject->set('BackendMemcacheTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
285  $subject->set('BackendMemcacheTest3', $data, ['UnitTestTag%test']);
286  $subject->flushByTag('UnitTestTag%special');
287  self::assertTrue($subject->has('BackendMemcacheTest1'));
288  self::assertFalse($subject->has('BackendMemcacheTest2'));
289  self::assertTrue($subject->has('BackendMemcacheTest3'));
290  }
291 
296  {
297  $frontendProphecy = $this->prophesize(FrontendInterface::class);
298  $frontendProphecy->getIdentifier()->willReturn('pages');
299 
300  $subject = $this->‪initializeSubject();
301  $subject->setCache($frontendProphecy->reveal());
302 
303  $data = 'some data' . microtime();
304  $subject->set('BackendMemcacheTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
305  $subject->set('BackendMemcacheTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
306  $subject->set('BackendMemcacheTest3', $data, ['UnitTestTag%test']);
307  $subject->flushByTags(['UnitTestTag%special', 'UnitTestTag%boring']);
308  self::assertFalse($subject->has('BackendMemcacheTest1'));
309  self::assertFalse($subject->has('BackendMemcacheTest2'));
310  self::assertTrue($subject->has('BackendMemcacheTest3'));
311  }
312 
317  {
318  $frontendProphecy = $this->prophesize(FrontendInterface::class);
319  $frontendProphecy->getIdentifier()->willReturn('pages');
320 
321  $subject = $this->‪initializeSubject();
322  $subject->setCache($frontendProphecy->reveal());
323 
324  $data = 'some data' . microtime();
325  $subject->set('BackendMemcacheTest1', $data);
326  $subject->set('BackendMemcacheTest2', $data);
327  $subject->set('BackendMemcacheTest3', $data);
328  $subject->flush();
329  self::assertFalse($subject->has('BackendMemcacheTest1'));
330  self::assertFalse($subject->has('BackendMemcacheTest2'));
331  self::assertFalse($subject->has('BackendMemcacheTest3'));
332  }
333 
338  {
339  $thisFrontendProphecy = $this->prophesize(FrontendInterface::class);
340  $thisFrontendProphecy->getIdentifier()->willReturn('thisCache');
341  $thisBackend = $this->‪initializeSubject();
342  $thisBackend->setCache($thisFrontendProphecy->reveal());
343 
344  $thatFrontendProphecy = $this->prophesize(FrontendInterface::class);
345  $thatFrontendProphecy->getIdentifier()->willReturn('thatCache');
346  $thatBackend = $this->‪initializeSubject();
347  $thatBackend->setCache($thatFrontendProphecy->reveal());
348 
349  $thisBackend->set('thisEntry', 'Hello');
350  $thatBackend->set('thatEntry', 'World!');
351  $thatBackend->flush();
352 
353  self::assertEquals('Hello', $thisBackend->get('thisEntry'));
354  self::assertFalse($thatBackend->has('thatEntry'));
355  }
356 }
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\itIsPossibleToSetAndGetEntry
‪itIsPossibleToSetAndGetEntry()
Definition: MemcachedBackendTest.php:106
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\initializeObjectThrowsExceptionIfNoMemcacheServerIsConfigured
‪initializeObjectThrowsExceptionIfNoMemcacheServerIsConfigured()
Definition: MemcachedBackendTest.php:79
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag
‪findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag()
Definition: MemcachedBackendTest.php:206
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\setThrowsExceptionIfNoFrontEndHasBeenSet
‪setThrowsExceptionIfNoFrontEndHasBeenSet()
Definition: MemcachedBackendTest.php:66
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\setRemovesTagsFromPreviousSet
‪setRemovesTagsFromPreviousSet()
Definition: MemcachedBackendTest.php:226
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend
Definition: MemcachedBackendTest.php:16
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\initializeSubject
‪initializeSubject()
Definition: MemcachedBackendTest.php:50
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: MemcachedBackendTest.php:316
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest
Definition: MemcachedBackendTest.php:28
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\itIsPossibleToRemoveEntryFromCache
‪itIsPossibleToRemoveEntryFromCache()
Definition: MemcachedBackendTest.php:169
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\itIsPossibleToOverwriteAnEntryInTheCache
‪itIsPossibleToOverwriteAnEntryInTheCache()
Definition: MemcachedBackendTest.php:187
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\largeDataIsStored
‪largeDataIsStored()
Definition: MemcachedBackendTest.php:152
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:16
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\hasReturnsFalseIfTheEntryDoesntExist
‪hasReturnsFalseIfTheEntryDoesntExist()
Definition: MemcachedBackendTest.php:244
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\removeReturnsFalseIfTheEntryDoesntExist
‪removeReturnsFalseIfTheEntryDoesntExist()
Definition: MemcachedBackendTest.php:259
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\flushByTagsRemovesCacheEntriesWithSpecifiedTags
‪flushByTagsRemovesCacheEntriesWithSpecifiedTags()
Definition: MemcachedBackendTest.php:295
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\getReturnsPreviouslySetDataWithVariousTypes
‪getReturnsPreviouslySetDataWithVariousTypes()
Definition: MemcachedBackendTest.php:123
‪TYPO3\CMS\Core\Cache\Backend\MemcachedBackend
Definition: MemcachedBackend.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\itIsPossibleToSetAndCheckExistenceInCache
‪itIsPossibleToSetAndCheckExistenceInCache()
Definition: MemcachedBackendTest.php:90
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\flushRemovesOnlyOwnEntries
‪flushRemovesOnlyOwnEntries()
Definition: MemcachedBackendTest.php:337
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\flushByTagRemovesCacheEntriesWithSpecifiedTag
‪flushByTagRemovesCacheEntriesWithSpecifiedTag()
Definition: MemcachedBackendTest.php:274
‪TYPO3\CMS\Core\Tests\Functional\Cache\Backend\MemcachedBackendTest\setUp
‪setUp()
Definition: MemcachedBackendTest.php:32