[TASK] Deprecate StringFrontend cache frontend 84/53084/15
authorClaus Due <claus@namelesscoder.net>
Sat, 3 Jun 2017 12:10:26 +0000 (14:10 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 12 Feb 2018 17:11:56 +0000 (18:11 +0100)
Can be drop-in replaced by VariableFrontend. There are no
occurrences in core code except for in tests and comments.
Configurations using this frontend can be automatically
migrated on-the-fly. Such auto-migration is not part of this
patch - this patch only touches the PHPDOC and documents
the deprecation.

Change-Id: I3b4b4979534857fa30b56d9967e8f919fc046ab9
Resolves: #81434
References: #81432
Releases: master
Reviewed-on: https://review.typo3.org/53084
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Classes/Cache/Frontend/PhpFrontend.php
typo3/sysext/core/Classes/Cache/Frontend/StringFrontend.php
typo3/sysext/core/Documentation/Changelog/master/Deprecation-81434-StringCacheFrontendDeprecated.rst [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Cache/Frontend/AbstractFrontendTest.php
typo3/sysext/core/Tests/Unit/Cache/Frontend/PhpFrontendTest.php
typo3/sysext/core/Tests/Unit/Cache/Frontend/StringFrontendTest.php [deleted file]
typo3/sysext/core/Tests/Unit/Cache/Frontend/VariableFrontendTest.php
typo3/sysext/core/Tests/Unit/Localization/LocalizationFactoryTest.php
typo3/sysext/core/Tests/UnitDeprecated/Cache/Frontend/StringFrontendTest.php [new file with mode: 0644]
typo3/sysext/scheduler/Tests/Unit/Task/CachingFrameworkGarbageCollectionTest.php

index 84d1a1c..8c2cd7e 100644 (file)
@@ -23,7 +23,7 @@ use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
  * This file is a backport from FLOW3
  * @api
  */
-class PhpFrontend extends StringFrontend
+class PhpFrontend extends AbstractFrontend
 {
     /**
      * Constructs the cache
@@ -65,6 +65,45 @@ class PhpFrontend extends StringFrontend
     }
 
     /**
+     * Finds and returns a variable value from the cache.
+     *
+     * @param string $entryIdentifier Identifier of the cache entry to fetch
+     * @return string The value
+     * @throws \InvalidArgumentException if the cache identifier is not valid
+     * @api
+     */
+    public function get($entryIdentifier)
+    {
+        if (!$this->isValidEntryIdentifier($entryIdentifier)) {
+            throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233057753);
+        }
+        return $this->backend->get($entryIdentifier);
+    }
+
+    /**
+     * Finds and returns all cache entries which are tagged by the specified tag.
+     *
+     * @param string $tag The tag to search for
+     * @return array An array with the content of all matching entries. An empty array if no entries matched
+     * @throws \InvalidArgumentException if the tag is not valid
+     * @api
+     * @deprecated since TYPO3 v9, Avoid using this method since it is not compliant to PSR-6
+     */
+    public function getByTag($tag)
+    {
+        trigger_error('This method will be removed in TYPO3 v10. Avoid using this method since it is not compliant to PSR-6.', E_USER_DEPRECATED);
+        if (!$this->isValidTag($tag)) {
+            throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233057773);
+        }
+        $entries = [];
+        $identifiers = $this->backend->findIdentifiersByTag($tag);
+        foreach ($identifiers as $identifier) {
+            $entries[] = $this->backend->get($identifier);
+        }
+        return $entries;
+    }
+
+    /**
      * Loads PHP code from the cache and require_onces it right away.
      *
      * @param string $entryIdentifier An identifier which describes the cache entry to load
index 3fb376a..8d6803a 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Core\Cache\Frontend;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Cache\Backend\BackendInterface;
 use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
 
 /**
@@ -21,10 +22,28 @@ use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
  *
  * This file is a backport from FLOW3
  * @api
+ * @deprecated since TYPO3 v9, will be removed in TYPO3 v10 - use VariableFrontend instead.
  */
 class StringFrontend extends AbstractFrontend
 {
     /**
+     * @param string $identifier
+     * @param BackendInterface $backend
+     */
+    public function __construct($identifier, BackendInterface $backend)
+    {
+        trigger_error(
+            sprintf(
+                'Usage of class %s will be removed in TYPO3 v10.0, use %s instead',
+                static::class,
+                VariableFrontend::class
+            ),
+            E_USER_DEPRECATED
+        );
+        parent::__construct($identifier, $backend);
+    }
+
+    /**
      * Saves the value of a PHP variable in the cache.
      *
      * @param string $entryIdentifier An identifier used for this cache entry
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-81434-StringCacheFrontendDeprecated.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-81434-StringCacheFrontendDeprecated.rst
new file mode 100644 (file)
index 0000000..d59abeb
--- /dev/null
@@ -0,0 +1,32 @@
+.. include:: ../../Includes.txt
+
+======================================================
+Deprecation: #81434 - String Cache Frontend Deprecated
+======================================================
+
+See :issue:`81434`
+
+Description
+===========
+
+The ``StringFrontend`` cache frontend has been deprecated in favor of VariableFrontend.
+
+
+Impact
+======
+
+The ``TYPO3\CMS\Core\Cache\Frontend\StringFrontend`` class is deprecated.
+
+
+Affected Installations
+======================
+
+Any TYPO3 installation which defines any custom cache using ``StringFrontend``.
+
+
+Migration
+=========
+
+Replace ``TYPO3\CMS\Core\Cache\Frontend\StringFrontend`` occurrences in cache configurations with ``TYPO3\CMS\Core\Cache\Frontend\VariableFrontend``.
+
+.. index:: PHP-API, NotScanned
\ No newline at end of file
index 7a6839d..1ec7fae 100644 (file)
@@ -31,7 +31,7 @@ class AbstractFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
             ->disableOriginalConstructor()
             ->getMock();
         foreach (['x', 'someValue', '123fivesixseveneight', 'some&', 'ab_cd%', rawurlencode('resource://some/äöü$&% sadf'), str_repeat('x', 250)] as $identifier) {
-            $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+            $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
                 ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage'])
                 ->setConstructorArgs([$identifier, $mockBackend])
                 ->getMock();
@@ -49,7 +49,7 @@ class AbstractFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
             ->getMock();
         foreach (['', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#'] as $identifier) {
             try {
-                $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+                $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
                     ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage'])
                     ->setConstructorArgs([$identifier, $mockBackend])
                     ->getMock();
@@ -70,7 +70,7 @@ class AbstractFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
             ->disableOriginalConstructor()
             ->getMock();
         $backend->expects($this->once())->method('flush');
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
             ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
             ->setConstructorArgs([$identifier, $backend])
             ->getMock();
@@ -88,7 +88,7 @@ class AbstractFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
         $identifier = 'someCacheIdentifier';
         $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface::class);
         $backend->expects($this->never())->method('flushByTag');
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
             ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
             ->setConstructorArgs([$identifier, $backend])
             ->getMock();
@@ -107,7 +107,7 @@ class AbstractFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
             ->disableOriginalConstructor()
             ->getMock();
         $backend->expects($this->once())->method('flushByTag')->with($tag);
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
             ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
             ->setConstructorArgs([$identifier, $backend])
             ->getMock();
@@ -126,7 +126,7 @@ class AbstractFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
             ->disableOriginalConstructor()
             ->getMock();
         $backend->expects($this->once())->method('flushByTags')->with([$tag]);
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
             ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
             ->setConstructorArgs([$identifier, $backend])
             ->getMock();
@@ -144,7 +144,7 @@ class AbstractFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
             ->disableOriginalConstructor()
             ->getMock();
         $backend->expects($this->once())->method('collectGarbage');
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
             ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
             ->setConstructorArgs([$identifier, $backend])
             ->getMock();
@@ -158,7 +158,7 @@ class AbstractFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
     {
         $identifier = 'someCacheIdentifier';
         $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class);
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
             ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
             ->setConstructorArgs([$identifier, $backend])
             ->getMock();
@@ -174,7 +174,7 @@ class AbstractFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
     {
         $identifier = 'someCacheIdentifier';
         $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class);
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
             ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
             ->setConstructorArgs([$identifier, $backend])
             ->getMock();
@@ -190,7 +190,7 @@ class AbstractFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
     {
         $identifier = 'someCacheIdentifier';
         $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class);
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
             ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
             ->setConstructorArgs([$identifier, $backend])
             ->getMock();
@@ -206,7 +206,7 @@ class AbstractFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
     {
         $identifier = 'someCacheIdentifier';
         $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class);
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
             ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag'])
             ->setConstructorArgs([$identifier, $backend])
             ->getMock();
index f06b688..73f4586 100644 (file)
@@ -28,9 +28,9 @@ class PhpFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
     public function setChecksIfTheIdentifierIsValid()
     {
         $this->expectException(\InvalidArgumentException::class);
-        $this->expectExceptionCode(1233057566);
+        $this->expectExceptionCode(1264023823);
 
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\PhpFrontend::class)
             ->setMethods(['isValidEntryIdentifier'])
             ->disableOriginalConstructor()
             ->getMock();
diff --git a/typo3/sysext/core/Tests/Unit/Cache/Frontend/StringFrontendTest.php b/typo3/sysext/core/Tests/Unit/Cache/Frontend/StringFrontendTest.php
deleted file mode 100644 (file)
index 4410141..0000000
+++ /dev/null
@@ -1,130 +0,0 @@
-<?php
-namespace TYPO3\CMS\Core\Tests\Unit\Cache\Frontend;
-
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
-
-/**
- * Testcase for the string cache frontend
- *
- * This file is a backport from FLOW3
- */
-class StringFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
-{
-    /**
-     * @test
-     */
-    public function setChecksIfTheIdentifierIsValid()
-    {
-        $this->expectException(\InvalidArgumentException::class);
-        $this->expectExceptionCode(1233057566);
-
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
-            ->setMethods(['isValidEntryIdentifier'])
-            ->disableOriginalConstructor()
-            ->getMock();
-        $cache->expects($this->once())->method('isValidEntryIdentifier')->with('foo')->will($this->returnValue(false));
-        $cache->set('foo', 'bar');
-    }
-
-    /**
-     * @test
-     */
-    public function setPassesStringToBackend()
-    {
-        $theString = 'Just some value';
-        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
-            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
-            ->disableOriginalConstructor()
-            ->getMock();
-        $backend->expects($this->once())->method('set')->with($this->equalTo('StringCacheTest'), $this->equalTo($theString));
-        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
-        $cache->set('StringCacheTest', $theString);
-    }
-
-    /**
-     * @test
-     */
-    public function setPassesLifetimeToBackend()
-    {
-        $theString = 'Just some value';
-        $theLifetime = 1234;
-        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
-            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
-            ->disableOriginalConstructor()
-            ->getMock();
-        $backend->expects($this->once())->method('set')->with($this->equalTo('StringCacheTest'), $this->equalTo($theString), $this->equalTo([]), $this->equalTo($theLifetime));
-        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
-        $cache->set('StringCacheTest', $theString, [], $theLifetime);
-    }
-
-    /**
-     * @test
-     */
-    public function setThrowsInvalidDataExceptionOnNonStringValues()
-    {
-        $this->expectException(InvalidDataException::class);
-        $this->expectExceptionCode(1222808333);
-
-        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
-            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
-            ->disableOriginalConstructor()
-            ->getMock();
-        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
-        $cache->set('StringCacheTest', []);
-    }
-
-    /**
-     * @test
-     */
-    public function getFetchesStringValueFromBackend()
-    {
-        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
-            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
-            ->disableOriginalConstructor()
-            ->getMock();
-        $backend->expects($this->once())->method('get')->will($this->returnValue('Just some value'));
-        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
-        $this->assertEquals('Just some value', $cache->get('StringCacheTest'), 'The returned value was not the expected string.');
-    }
-
-    /**
-     * @test
-     */
-    public function hasReturnsResultFromBackend()
-    {
-        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
-            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
-            ->disableOriginalConstructor()
-            ->getMock();
-        $backend->expects($this->once())->method('has')->with($this->equalTo('StringCacheTest'))->will($this->returnValue(true));
-        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
-        $this->assertTrue($cache->has('StringCacheTest'), 'has() did not return TRUE.');
-    }
-
-    /**
-     * @test
-     */
-    public function removeCallsBackend()
-    {
-        $cacheIdentifier = 'someCacheIdentifier';
-        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
-            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
-            ->disableOriginalConstructor()
-            ->getMock();
-        $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(true));
-        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
-        $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
-    }
-}
index aa53969..83d2e61 100644 (file)
@@ -14,12 +14,12 @@ namespace TYPO3\CMS\Core\Tests\Unit\Cache\Frontend;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
+
 /**
- * Testcase for the variable cache frontend
- *
- * This file is a backport from FLOW3
+ * Test case
  */
-class VariableFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
+class VariableFrontendTest extends UnitTestCase
 {
     /**
      * @test
@@ -27,9 +27,9 @@ class VariableFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
     public function setChecksIfTheIdentifierIsValid()
     {
         $this->expectException(\InvalidArgumentException::class);
-        $this->expectExceptionCode(1233057566);
+        $this->expectExceptionCode(1233058264);
 
-        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
             ->setMethods(['isValidEntryIdentifier'])
             ->disableOriginalConstructor()
             ->getMock();
index 18f2447..f0edd74 100644 (file)
@@ -71,7 +71,7 @@ class LocalizationFactoryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTest
         $languageStore = $this->getMockBuilder(\TYPO3\CMS\Core\Localization\LanguageStore::class)
             ->setMethods(['hasData', 'setConfiguration', 'getData', 'setData'])
             ->getMock();
-        $cacheInstance = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+        $cacheInstance = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
             ->setMethods(['get', 'set'])
             ->disableOriginalConstructor()
             ->getMock();
diff --git a/typo3/sysext/core/Tests/UnitDeprecated/Cache/Frontend/StringFrontendTest.php b/typo3/sysext/core/Tests/UnitDeprecated/Cache/Frontend/StringFrontendTest.php
new file mode 100644 (file)
index 0000000..7cd7b21
--- /dev/null
@@ -0,0 +1,130 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Frontend;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
+
+/**
+ * Testcase for the string cache frontend
+ *
+ * This file is a backport from FLOW3
+ */
+class StringFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
+{
+    /**
+     * @test
+     */
+    public function setChecksIfTheIdentifierIsValid()
+    {
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionCode(1233057566);
+
+        $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
+            ->setMethods(['isValidEntryIdentifier'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $cache->expects($this->once())->method('isValidEntryIdentifier')->with('foo')->will($this->returnValue(false));
+        $cache->set('foo', 'bar');
+    }
+
+    /**
+     * @test
+     */
+    public function setPassesStringToBackend()
+    {
+        $theString = 'Just some value';
+        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
+            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $backend->expects($this->once())->method('set')->with($this->equalTo('StringCacheTest'), $this->equalTo($theString));
+        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
+        $cache->set('StringCacheTest', $theString);
+    }
+
+    /**
+     * @test
+     */
+    public function setPassesLifetimeToBackend()
+    {
+        $theString = 'Just some value';
+        $theLifetime = 1234;
+        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
+            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $backend->expects($this->once())->method('set')->with($this->equalTo('StringCacheTest'), $this->equalTo($theString), $this->equalTo([]), $this->equalTo($theLifetime));
+        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
+        $cache->set('StringCacheTest', $theString, [], $theLifetime);
+    }
+
+    /**
+     * @test
+     */
+    public function setThrowsInvalidDataExceptionOnNonStringValues()
+    {
+        $this->expectException(InvalidDataException::class);
+        $this->expectExceptionCode(1222808333);
+
+        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
+            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
+        $cache->set('StringCacheTest', []);
+    }
+
+    /**
+     * @test
+     */
+    public function getFetchesStringValueFromBackend()
+    {
+        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
+            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $backend->expects($this->once())->method('get')->will($this->returnValue('Just some value'));
+        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
+        $this->assertEquals('Just some value', $cache->get('StringCacheTest'), 'The returned value was not the expected string.');
+    }
+
+    /**
+     * @test
+     */
+    public function hasReturnsResultFromBackend()
+    {
+        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
+            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $backend->expects($this->once())->method('has')->with($this->equalTo('StringCacheTest'))->will($this->returnValue(true));
+        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
+        $this->assertTrue($cache->has('StringCacheTest'), 'has() did not return TRUE.');
+    }
+
+    /**
+     * @test
+     */
+    public function removeCallsBackend()
+    {
+        $cacheIdentifier = 'someCacheIdentifier';
+        $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
+            ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(true));
+        $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
+        $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
+    }
+}
index 790b332..b0ad4b4 100644 (file)
@@ -48,7 +48,7 @@ class CachingFrameworkGarbageCollectionTest extends \TYPO3\TestingFramework\Core
      */
     public function executeCallsCollectGarbageOfConfiguredBackend()
     {
-        $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class);
+        $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class);
         $cache->expects($this->any())->method('getIdentifier')->will($this->returnValue('cache'));
         $cache->expects($this->atLeastOnce())->method('collectGarbage');
         $mockCacheManager = new \TYPO3\CMS\Core\Cache\CacheManager();
@@ -56,7 +56,7 @@ class CachingFrameworkGarbageCollectionTest extends \TYPO3\TestingFramework\Core
         GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Cache\CacheManager::class, $mockCacheManager);
         $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] = [
             'cache' => [
-                'frontend' => \TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class,
+                'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class,
                 'backend' => \TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class,
             ]
         ];
@@ -74,7 +74,7 @@ class CachingFrameworkGarbageCollectionTest extends \TYPO3\TestingFramework\Core
      */
     public function executeDoesNotCallCollectGarbageOfNotConfiguredBackend()
     {
-        $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class);
+        $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class);
         $cache->expects($this->any())->method('getIdentifier')->will($this->returnValue('cache'));
         $cache->expects($this->never())->method('collectGarbage');
         $mockCacheManager = new \TYPO3\CMS\Core\Cache\CacheManager();
@@ -82,7 +82,7 @@ class CachingFrameworkGarbageCollectionTest extends \TYPO3\TestingFramework\Core
         GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Cache\CacheManager::class, $mockCacheManager);
         $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] = [
             'cache' => [
-                'frontend' => \TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class,
+                'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class,
                 'backend' => \TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class,
             ]
         ];