17 use Psr\Log\LoggerInterface;
21 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
39 if (!getenv(
'typo3TestingRedisHost')) {
40 $this->markTestSkipped(
'environment variable "typo3TestingRedisHost" must be set to run this test');
55 $backendOptions[
'hostname'] = getenv(
'typo3TestingRedisHost');
57 $env = getenv(
'typo3TestingRedisPort');
58 $backendOptions[
'port'] = is_string($env) ? (int)$env : 6379;
60 $frontendProphecy = $this->prophesize(FrontendInterface::class);
61 $frontendProphecy->getIdentifier()->willReturn(
'cache_pages');
63 $GLOBALS[
'TYPO3_CONF_VARS'][
'LOG'] =
'only needed for logger initialisation';
65 $subject->setLogger($this->prophesize(LoggerInterface::class)->reveal());
66 $subject->setCache($frontendProphecy->reveal());
67 $subject->initializeObject();
78 $redisHost = getenv(
'typo3TestingRedisHost');
80 $env = getenv(
'typo3TestingRedisPort');
81 $redisPort = is_string($env) ? (int)$env : 6379;
83 $redis = new \Redis();
84 $redis->connect($redisHost, $redisPort);
93 $this->expectException(\InvalidArgumentException::class);
94 $this->expectExceptionCode(1279763057);
104 $this->expectException(\InvalidArgumentException::class);
105 $this->expectExceptionCode(1279763534);
115 $this->expectException(\InvalidArgumentException::class);
116 $this->expectExceptionCode(1289679153);
126 $this->expectException(\InvalidArgumentException::class);
127 $this->expectExceptionCode(1289679154);
137 $this->expectException(\InvalidArgumentException::class);
138 $this->expectExceptionCode(1289679155);
148 $this->expectException(\InvalidArgumentException::class);
149 $this->expectExceptionCode(1487849315);
159 $this->expectException(\InvalidArgumentException::class);
160 $this->expectExceptionCode(1487849326);
170 $this->expectException(\InvalidArgumentException::class);
171 $this->expectExceptionCode(1377006651);
174 $subject->set([],
'data');
182 $this->expectException(InvalidDataException::class);
183 $this->expectExceptionCode(1279469941);
186 $subject->set($this->getUniqueId(
'identifier'), []);
194 $this->expectException(\InvalidArgumentException::class);
195 $this->expectExceptionCode(1279487573);
198 $subject->set($this->getUniqueId(
'identifier'),
'data', [], -42);
206 $this->expectException(\InvalidArgumentException::class);
207 $this->expectExceptionCode(1279488008);
210 $subject->set($this->getUniqueId(
'identifier'),
'data', [], []);
221 $identifier = $this->getUniqueId(
'identifier');
222 $subject->set($identifier,
'data');
223 $result = $redis->exists(
'identData:' . $identifier);
224 if (is_int($result)) {
226 $result = (bool)$result;
228 $this->assertTrue($result);
238 $identifier = $this->getUniqueId(
'identifier');
239 $subject->set($identifier,
'data');
240 $this->assertSame(\Redis::REDIS_STRING, $redis->type(
'identData:' . $identifier));
250 $identifier = $this->getUniqueId(
'identifier');
251 $defaultLifetime = 42;
252 $subject->setDefaultLifetime($defaultLifetime);
253 $subject->set($identifier,
'data');
254 $lifetimeRegisteredInBackend = $redis->ttl(
'identData:' . $identifier);
255 $this->assertSame($defaultLifetime, $lifetimeRegisteredInBackend);
265 $identifier = $this->getUniqueId(
'identifier');
267 $subject->set($identifier,
'data', [], $lifetime);
268 $lifetimeRegisteredInBackend = $redis->ttl(
'identData:' . $identifier);
269 $this->assertSame($lifetime, $lifetimeRegisteredInBackend);
279 $identifier = $this->getUniqueId(
'identifier');
280 $subject->set($identifier,
'data', [], 0);
281 $lifetimeRegisteredInBackend = $redis->ttl(
'identData:' . $identifier);
282 $this->assertSame(31536000, $lifetimeRegisteredInBackend);
292 $identifier = $this->getUniqueId(
'identifier');
293 $subject->set($identifier, $data);
294 $otherData =
'data 2';
295 $subject->set($identifier, $otherData);
296 $fetchedData = $subject->get($identifier);
297 $this->assertSame($otherData, $fetchedData);
308 $identifier = $this->getUniqueId(
'identifier');
309 $subject->set($identifier, $data);
311 $subject->set($identifier, $data, [], $lifetime);
312 $lifetimeRegisteredInBackend = $redis->ttl(
'identData:' . $identifier);
313 $this->assertSame($lifetime, $lifetimeRegisteredInBackend);
324 $identifier = $this->getUniqueId(
'identifier');
326 $subject->set($identifier, $data, [], $lifetime);
327 $newDefaultLifetime = 43;
328 $subject->setDefaultLifetime($newDefaultLifetime);
329 $subject->set($identifier, $data, [], $newDefaultLifetime);
330 $lifetimeRegisteredInBackend = $redis->ttl(
'identData:' . $identifier);
331 $this->assertSame($newDefaultLifetime, $lifetimeRegisteredInBackend);
342 $identifier = $this->getUniqueId(
'identifier');
344 $subject->set($identifier, $data, [], $lifetime);
345 $subject->set($identifier, $data, [], 0);
346 $lifetimeRegisteredInBackend = $redis->ttl(
'identData:' . $identifier);
347 $this->assertSame(31536000, $lifetimeRegisteredInBackend);
357 $identifier = $this->getUniqueId(
'identifier');
358 $subject->set($identifier,
'data', [
'tag']);
359 $this->assertSame(\Redis::REDIS_SET, $redis->type(
'identTags:' . $identifier));
369 $identifier = $this->getUniqueId(
'identifier');
370 $tags = [
'thatTag',
'thisTag'];
371 $subject->set($identifier,
'data', $tags);
372 $savedTags = $redis->sMembers(
'identTags:' . $identifier);
374 $this->assertSame($tags, $savedTags);
384 $identifier = $this->getUniqueId(
'identifier');
385 $tags = [
'fooTag',
'barTag'];
386 $subject->set($identifier,
'data', $tags);
387 $subject->set($identifier,
'data', []);
388 $this->assertSame([], $redis->sMembers(
'identTags:' . $identifier));
398 $identifier = $this->getUniqueId(
'identifier');
399 $firstTagSet = [
'tag1',
'tag2',
'tag3',
'tag4'];
400 $subject->set($identifier,
'data', $firstTagSet);
401 $secondTagSet = [
'tag1',
'tag3'];
402 $subject->set($identifier,
'data', $secondTagSet);
403 $actualTagSet = $redis->sMembers(
'identTags:' . $identifier);
405 $this->assertSame($secondTagSet, $actualTagSet);
415 $identifier = $this->getUniqueId(
'identifier');
417 $subject->set($identifier,
'data', [$tag]);
418 $this->assertSame(\Redis::REDIS_SET, $redis->type(
'tagIdents:' . $tag));
428 $identifier = $this->getUniqueId(
'identifier');
430 $subject->set($identifier,
'data', [$tag]);
431 $savedTagToIdentifiersMemberArray = $redis->sMembers(
'tagIdents:' . $tag);
432 $this->assertSame([$identifier], $savedTagToIdentifiersMemberArray);
442 $firstIdentifier = $this->getUniqueId(
'identifier1-');
444 $subject->set($firstIdentifier,
'data', [$tag]);
445 $secondIdentifier = $this->getUniqueId(
'identifier2-');
446 $subject->set($secondIdentifier,
'data', [$tag]);
447 $savedTagToIdentifiersMemberArray = $redis->sMembers(
'tagIdents:' . $tag);
448 sort($savedTagToIdentifiersMemberArray);
449 $identifierArray = [$firstIdentifier, $secondIdentifier];
450 sort($identifierArray);
451 $this->assertSame([$firstIdentifier, $secondIdentifier], $savedTagToIdentifiersMemberArray);
461 $identifier = $this->getUniqueId(
'identifier');
463 $subject->set($identifier,
'data', [$tag]);
464 $subject->set($identifier,
'data', []);
465 $savedTagToIdentifiersMemberArray = $redis->sMembers(
'tagIdents:' . $tag);
466 $this->assertSame([], $savedTagToIdentifiersMemberArray);
476 $identifier = $this->getUniqueId(
'identifier');
477 $subject->set($identifier,
'data');
479 $subject->set($identifier,
'data', [$tag]);
480 $savedTagToIdentifiersMemberArray = $redis->sMembers(
'tagIdents:' . $tag);
481 $this->assertSame([$identifier], $savedTagToIdentifiersMemberArray);
490 'compression' =>
true
493 $identifier = $this->getUniqueId(
'identifier');
494 $data =
'some data ' . microtime();
495 $subject->set($identifier, $data);
496 $uncompresedStoredData =
'';
498 $uncompresedStoredData = @gzuncompress($redis->get(
'identData:' . $identifier));
501 $this->assertEquals($data, $uncompresedStoredData,
'Original and compressed data don\'t match');
510 'compression' =>
true,
511 'compressionLevel' => 0
514 $identifier = $this->getUniqueId(
'identifier');
515 $data =
'some data ' . microtime();
516 $subject->set($identifier, $data);
517 $this->assertGreaterThan(0, substr_count($redis->get(
'identData:' . $identifier), $data),
'Plaintext data not found');
525 $this->expectException(\InvalidArgumentException::class);
526 $this->expectExceptionCode(1377006653);
538 $identifier = $this->getUniqueId(
'identifier');
539 $this->assertFalse($subject->has($identifier));
548 $identifier = $this->getUniqueId(
'identifier');
549 $subject->set($identifier,
'data');
550 $this->assertTrue($subject->has($identifier));
558 $this->expectException(\InvalidArgumentException::class);
571 'compression' =>
true
574 $identifier = $this->getUniqueId(
'identifier');
575 $subject->set($identifier, $data);
576 $fetchedData = $subject->get($identifier);
577 $this->assertSame($data, $fetchedData);
587 $identifier = $this->getUniqueId(
'identifier');
588 $subject->set($identifier, $data);
589 $fetchedData = $subject->get($identifier);
590 $this->assertSame($data, $fetchedData);
598 $this->expectException(\InvalidArgumentException::class);
599 $this->expectExceptionCode(1377006654);
602 $subject->remove([]);
611 $this->assertFalse($subject->remove($this->getUniqueId(
'identifier')));
620 $identifier = $this->getUniqueId(
'identifier');
621 $subject->set($identifier,
'data');
622 $this->assertTrue($subject->remove($identifier));
631 $identifier = $this->getUniqueId(
'identifier');
632 $subject->set($identifier,
'data');
633 $subject->remove($identifier);
634 $this->assertFalse($subject->has($identifier));
644 $identifier = $this->getUniqueId(
'identifier');
646 $subject->set($identifier,
'data', [$tag]);
647 $subject->remove($identifier);
648 $result = $redis->exists(
'identTags:' . $identifier);
649 if (is_int($result)) {
651 $result = (bool)$result;
653 $this->assertFalse($result);
663 $identifier = $this->getUniqueId(
'identifier');
665 $subject->set($identifier,
'data', [$tag]);
666 $subject->remove($identifier);
667 $tagToIdentifiersMemberArray = $redis->sMembers(
'tagIdents:' . $tag);
668 $this->assertSame([], $tagToIdentifiersMemberArray);
678 $firstIdentifier = $this->getUniqueId(
'identifier');
679 $secondIdentifier = $this->getUniqueId(
'identifier');
681 $subject->set($firstIdentifier,
'data', [$tag]);
682 $subject->set($secondIdentifier,
'data', [$tag]);
683 $subject->remove($firstIdentifier);
684 $tagToIdentifiersMemberArray = $redis->sMembers(
'tagIdents:' . $tag);
685 $this->assertSame([$secondIdentifier], $tagToIdentifiersMemberArray);
693 $this->expectException(\InvalidArgumentException::class);
694 $this->expectExceptionCode(1377006655);
697 $subject->findIdentifiersByTag([]);
706 $this->assertSame([], $subject->findIdentifiersByTag(
'thisTag'));
715 $firstIdentifier = $this->getUniqueId(
'identifier1-');
716 $secondIdentifier = $this->getUniqueId(
'identifier2-');
717 $thirdIdentifier = $this->getUniqueId(
'identifier3-');
718 $tagsForFirstIdentifier = [
'thisTag'];
719 $tagsForSecondIdentifier = [
'thatTag'];
720 $tagsForThirdIdentifier = [
'thisTag',
'thatTag'];
721 $subject->set($firstIdentifier,
'data', $tagsForFirstIdentifier);
722 $subject->set($secondIdentifier,
'data', $tagsForSecondIdentifier);
723 $subject->set($thirdIdentifier,
'data', $tagsForThirdIdentifier);
724 $expectedResult = [$firstIdentifier, $thirdIdentifier];
725 $actualResult = $subject->findIdentifiersByTag(
'thisTag');
727 $this->assertSame($expectedResult, $actualResult);
737 $identifier = $this->getUniqueId(
'identifier');
738 $subject->set($identifier,
'data');
740 $this->assertSame([], $redis->getKeys(
'*'));
748 $this->expectException(\InvalidArgumentException::class);
749 $this->expectExceptionCode(1377006656);
752 $subject->flushByTag([]);
761 $identifier = $this->getUniqueId(
'identifier');
762 $subject->set($identifier .
'A',
'data', [
'tag1']);
763 $subject->set($identifier .
'B',
'data', [
'tag2']);
764 $subject->set($identifier .
'C',
'data', [
'tag1',
'tag2']);
765 $subject->flushByTag(
'tag1');
766 $expectedResult = [
false,
true,
false];
768 $subject->has($identifier .
'A'),
769 $subject->has($identifier .
'B'),
770 $subject->has($identifier .
'C')
772 $this->assertSame($expectedResult, $actualResult);
781 $identifier = $this->getUniqueId(
'identifier');
782 $subject->set($identifier .
'A',
'data', [
'tag1']);
783 $subject->set($identifier .
'B',
'data', [
'tag2']);
784 $subject->set($identifier .
'C',
'data', [
'tag1',
'tag2']);
785 $subject->set($identifier .
'D',
'data', [
'tag3']);
786 $subject->flushByTags([
'tag1',
'tag2']);
787 $expectedResult = [
false,
false,
false,
true];
789 $subject->has($identifier .
'A'),
790 $subject->has($identifier .
'B'),
791 $subject->has($identifier .
'C'),
792 $subject->has($identifier .
'D')
794 $this->assertSame($expectedResult, $actualResult);
804 $identifier = $this->getUniqueId(
'identifier');
805 $subject->set($identifier .
'A',
'data', [
'tag1']);
806 $subject->set($identifier .
'C',
'data', [
'tag1',
'tag2']);
807 $subject->flushByTag(
'tag1');
808 $this->assertSame([], $redis->getKeys(
'temp*'));
818 $identifier = $this->getUniqueId(
'identifier');
820 $subject->set($identifier,
'data', [$tag]);
821 $subject->flushByTag($tag);
822 $result = $redis->exists(
'identTags:' . $identifier);
823 if (is_int($result)) {
825 $result = (bool)$result;
827 $this->assertFalse($result);
837 $identifierToBeRemoved = $this->getUniqueId(
'identifier');
838 $tagToRemove =
'tag1';
839 $subject->set($identifierToBeRemoved,
'data', [$tagToRemove]);
840 $identifierNotToBeRemoved = $this->getUniqueId(
'identifier');
841 $tagNotToRemove =
'tag2';
842 $subject->set($identifierNotToBeRemoved,
'data', [$tagNotToRemove]);
843 $subject->flushByTag($tagToRemove);
844 $this->assertSame([$tagNotToRemove], $redis->sMembers(
'identTags:' . $identifierNotToBeRemoved));
854 $identifier = $this->getUniqueId(
'identifier');
856 $subject->set($identifier,
'data', [$tag]);
857 $subject->flushByTag($tag);
858 $result = $redis->exists(
'tagIdents:' . $tag);
859 if (is_int($result)) {
861 $result = (bool)$result;
863 $this->assertFalse($result);
873 $identifier = $this->getUniqueId(
'identifier');
874 $subject->set($identifier .
'A',
'data', [
'tag1',
'tag2']);
875 $subject->set($identifier .
'B',
'data', [
'tag1',
'tag2']);
876 $subject->set($identifier .
'C',
'data', [
'tag2']);
877 $subject->flushByTag(
'tag1');
878 $this->assertSame([$identifier .
'C'], $redis->sMembers(
'tagIdents:tag2'));
888 $identifier = $this->getUniqueId(
'identifier');
889 $subject->set($identifier .
'A',
'data', [
'tag']);
890 $subject->set($identifier .
'B',
'data', [
'tag']);
891 $redis->del(
'identData:' . $identifier .
'A');
892 $subject->collectGarbage();
893 $result = $redis->exists(
'identData:' . $identifier .
'B');
894 if (is_int($result)) {
896 $result = (bool)$result;
898 $this->assertTrue($result);
908 $identifier = $this->getUniqueId(
'identifier');
909 $subject->set($identifier .
'A',
'data', [
'tag']);
910 $subject->set($identifier .
'B',
'data', [
'tag']);
911 $redis->del(
'identData:' . $identifier .
'A');
912 $subject->collectGarbage();
913 $expectedResult = [
false,
true];
914 $resultA = $redis->exists(
'identTags:' . $identifier .
'A');
915 $resultB = $redis->exists(
'identTags:' . $identifier .
'B');
916 if (is_int($resultA)) {
918 $resultA = (bool)$resultA;
920 if (is_int($resultB)) {
922 $resultB = (bool)$resultB;
928 $this->assertSame($expectedResult, $actualResult);
938 $identifier = $this->getUniqueId(
'identifier');
939 $subject->set($identifier .
'A',
'data', [
'tag1',
'tag2']);
940 $subject->set($identifier .
'B',
'data', [
'tag2']);
941 $redis->del(
'identData:' . $identifier .
'A');
942 $subject->collectGarbage();
948 $redis->sMembers(
'tagIdents:tag1'),
949 $redis->sMembers(
'tagIdents:tag2')
951 $this->assertSame($expectedResult, $actualResult);