20 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
32 if (!extension_loaded(
'memcache') && !extension_loaded(
'memcached')) {
33 $this->markTestSkipped(
'Neither "memcache" nor "memcached" extension was available');
35 if (!getenv(
'typo3TestingMemcachedHost')) {
36 $this->markTestSkipped(
'environment variable "typo3TestingMemcachedHost" must be set to run this test');
51 $memcachedHost = getenv(
'typo3TestingMemcachedHost');
53 $env = getenv(
'typo3TestingMemcachedPort');
54 $memcachedPort = is_string($env) ? (int)$env : 11211;
56 $subject =
new MemcachedBackend(
'Testing', [
'servers' => [$memcachedHost .
':' . $memcachedPort] ]);
57 $subject->initializeObject();
68 $this->expectException(Exception::class);
69 $this->expectExceptionCode(1207149215);
71 $subject->set($this->getUniqueId(
'MyIdentifier'),
'some data');
80 $this->expectException(Exception::class);
81 $this->expectExceptionCode(1213115903);
82 $subject->initializeObject();
90 $frontendProphecy = $this->prophesize(FrontendInterface::class);
91 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
94 $subject->setCache($frontendProphecy->reveal());
96 $identifier = $this->getUniqueId(
'MyIdentifier');
97 $subject->set($identifier,
'Some data');
98 $this->assertTrue($subject->has($identifier));
106 $frontendProphecy = $this->prophesize(FrontendInterface::class);
107 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
110 $subject->setCache($frontendProphecy->reveal());
113 $identifier = $this->getUniqueId(
'MyIdentifier');
114 $subject->set($identifier, $data);
115 $this->assertEquals($data, $subject->get($identifier));
123 $frontendProphecy = $this->prophesize(FrontendInterface::class);
124 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
127 $subject->setCache($frontendProphecy->reveal());
130 'string' =>
'Serialize a string',
132 'anotherIntegerValue' => 123456,
141 $subject->set(
'myIdentifier', $data);
142 $this->assertSame($data, $subject->get(
'myIdentifier'));
152 $frontendProphecy = $this->prophesize(FrontendInterface::class);
153 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
156 $subject->setCache($frontendProphecy->reveal());
158 $data = str_repeat(
'abcde', 1024 * 1024);
159 $subject->set(
'tooLargeData', $data);
160 $this->assertTrue($subject->has(
'tooLargeData'));
161 $this->assertEquals($subject->get(
'tooLargeData'), $data);
169 $frontendProphecy = $this->prophesize(FrontendInterface::class);
170 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
173 $subject->setCache($frontendProphecy->reveal());
176 $identifier = $this->getUniqueId(
'MyIdentifier');
177 $subject->set($identifier, $data);
178 $subject->remove($identifier);
179 $this->assertFalse($subject->has($identifier));
187 $frontendProphecy = $this->prophesize(FrontendInterface::class);
188 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
191 $subject->setCache($frontendProphecy->reveal());
194 $identifier = $this->getUniqueId(
'MyIdentifier');
195 $subject->set($identifier, $data);
196 $otherData =
'some other data';
197 $subject->set($identifier, $otherData);
198 $this->assertEquals($otherData, $subject->get($identifier));
206 $frontendProphecy = $this->prophesize(FrontendInterface::class);
207 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
210 $subject->setCache($frontendProphecy->reveal());
213 $identifier = $this->getUniqueId(
'MyIdentifier');
214 $subject->set($identifier, $data, [
'UnitTestTag%tag1',
'UnitTestTag%tag2']);
215 $retrieved = $subject->findIdentifiersByTag(
'UnitTestTag%tag1');
216 $this->assertEquals($identifier, $retrieved[0]);
217 $retrieved = $subject->findIdentifiersByTag(
'UnitTestTag%tag2');
218 $this->assertEquals($identifier, $retrieved[0]);
226 $frontendProphecy = $this->prophesize(FrontendInterface::class);
227 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
230 $subject->setCache($frontendProphecy->reveal());
233 $identifier = $this->getUniqueId(
'MyIdentifier');
234 $subject->set($identifier, $data, [
'UnitTestTag%tag1',
'UnitTestTag%tag2']);
235 $subject->set($identifier, $data, [
'UnitTestTag%tag3']);
236 $this->assertEquals([], $subject->findIdentifiersByTag(
'UnitTestTag%tagX'));
244 $frontendProphecy = $this->prophesize(FrontendInterface::class);
245 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
248 $subject->setCache($frontendProphecy->reveal());
250 $identifier = $this->getUniqueId(
'NonExistingIdentifier');
251 $this->assertFalse($subject->has($identifier));
259 $frontendProphecy = $this->prophesize(FrontendInterface::class);
260 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
263 $subject->setCache($frontendProphecy->reveal());
265 $identifier = $this->getUniqueId(
'NonExistingIdentifier');
266 $this->assertFalse($subject->remove($identifier));
274 $frontendProphecy = $this->prophesize(FrontendInterface::class);
275 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
278 $subject->setCache($frontendProphecy->reveal());
280 $data =
'some data' . microtime();
281 $subject->set(
'BackendMemcacheTest1', $data, [
'UnitTestTag%test',
'UnitTestTag%boring']);
282 $subject->set(
'BackendMemcacheTest2', $data, [
'UnitTestTag%test',
'UnitTestTag%special']);
283 $subject->set(
'BackendMemcacheTest3', $data, [
'UnitTestTag%test']);
284 $subject->flushByTag(
'UnitTestTag%special');
285 $this->assertTrue($subject->has(
'BackendMemcacheTest1'));
286 $this->assertFalse($subject->has(
'BackendMemcacheTest2'));
287 $this->assertTrue($subject->has(
'BackendMemcacheTest3'));
295 $frontendProphecy = $this->prophesize(FrontendInterface::class);
296 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
299 $subject->setCache($frontendProphecy->reveal());
301 $data =
'some data' . microtime();
302 $subject->set(
'BackendMemcacheTest1', $data, [
'UnitTestTag%test',
'UnitTestTag%boring']);
303 $subject->set(
'BackendMemcacheTest2', $data, [
'UnitTestTag%test',
'UnitTestTag%special']);
304 $subject->set(
'BackendMemcacheTest3', $data, [
'UnitTestTag%test']);
305 $subject->flushByTags([
'UnitTestTag%special',
'UnitTestTag%boring']);
306 $this->assertFalse($subject->has(
'BackendMemcacheTest1'));
307 $this->assertFalse($subject->has(
'BackendMemcacheTest2'));
308 $this->assertTrue($subject->has(
'BackendMemcacheTest3'));
316 $frontendProphecy = $this->prophesize(FrontendInterface::class);
317 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
320 $subject->setCache($frontendProphecy->reveal());
322 $data =
'some data' . microtime();
323 $subject->set(
'BackendMemcacheTest1', $data);
324 $subject->set(
'BackendMemcacheTest2', $data);
325 $subject->set(
'BackendMemcacheTest3', $data);
327 $this->assertFalse($subject->has(
'BackendMemcacheTest1'));
328 $this->assertFalse($subject->has(
'BackendMemcacheTest2'));
329 $this->assertFalse($subject->has(
'BackendMemcacheTest3'));
337 $thisFrontendProphecy = $this->prophesize(FrontendInterface::class);
338 $thisFrontendProphecy->getIdentifier()->willReturn(
'thisCache');
340 $thisBackend->setCache($thisFrontendProphecy->reveal());
342 $thatFrontendProphecy = $this->prophesize(FrontendInterface::class);
343 $thatFrontendProphecy->getIdentifier()->willReturn(
'thatCache');
345 $thatBackend->setCache($thatFrontendProphecy->reveal());
347 $thisBackend->set(
'thisEntry',
'Hello');
348 $thatBackend->set(
'thatEntry',
'World!');
349 $thatBackend->flush();
351 $this->assertEquals(
'Hello', $thisBackend->get(
'thisEntry'));
352 $this->assertFalse($thatBackend->has(
'thatEntry'));