summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-06-30 17:11:40 (GMT)
committerAlex Pott2016-06-30 17:11:40 (GMT)
commit57ad987f45761c7f6df868f716826d6b28dac8f0 (patch)
treee59202fd6478fec66928228d80abadc5c9f54acf
parent9a0f369bfc0fb4f401844dd8dd3982e1129dc58b (diff)
Issue #2756307 by Fabianx, dawehner: Provide a setting to disable FileCache completely for unit tests
-rw-r--r--core/lib/Drupal/Component/FileCache/FileCacheFactory.php10
-rw-r--r--core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php37
-rw-r--r--core/tests/Drupal/Tests/UnitTestCase.php2
3 files changed, 38 insertions, 11 deletions
diff --git a/core/lib/Drupal/Component/FileCache/FileCacheFactory.php b/core/lib/Drupal/Component/FileCache/FileCacheFactory.php
index d0860b4..60cfea5 100644
--- a/core/lib/Drupal/Component/FileCache/FileCacheFactory.php
+++ b/core/lib/Drupal/Component/FileCache/FileCacheFactory.php
@@ -8,6 +8,11 @@ namespace Drupal\Component\FileCache;
class FileCacheFactory {
/**
+ * The configuration key to disable FileCache completely.
+ */
+ const DISABLE_CACHE = 'file_cache_disable';
+
+ /**
* The configuration used to create FileCache objects.
*
* @var array $configuration
@@ -34,6 +39,11 @@ class FileCacheFactory {
* The initialized FileCache object.
*/
public static function get($collection, $default_configuration = []) {
+ // If there is a special key in the configuration, disable FileCache completely.
+ if (!empty(static::$configuration[static::DISABLE_CACHE])) {
+ return new NullFileCache('', '');
+ }
+
$default_configuration += [
'class' => '\Drupal\Component\FileCache\FileCache',
'collection' => $collection,
diff --git a/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php b/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php
index 2b1edb2..327ef27 100644
--- a/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php
+++ b/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php
@@ -2,6 +2,8 @@
namespace Drupal\Tests\Component\FileCache;
+use Drupal\Component\FileCache\FileCache;
+use Drupal\Component\FileCache\NullFileCache;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Tests\UnitTestCase;
@@ -17,18 +19,15 @@ class FileCacheFactoryTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
- $settings = [
- 'collection' => 'test-23',
- 'cache_backend_class' => '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend',
- 'cache_backend_configuration' => [
- 'bin' => 'dog',
+ $configuration = [
+ 'test_foo_settings' => [
+ 'collection' => 'test-23',
+ 'cache_backend_class' => '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend',
+ 'cache_backend_configuration' => [
+ 'bin' => 'dog',
+ ],
],
];
- $configuration = FileCacheFactory::getConfiguration();
- if (!$configuration) {
- $configuration = [];
- }
- $configuration += [ 'test_foo_settings' => $settings ];
FileCacheFactory::setConfiguration($configuration);
FileCacheFactory::setPrefix('prefix');
}
@@ -66,6 +65,24 @@ class FileCacheFactoryTest extends UnitTestCase {
}
/**
+ * @covers ::get
+ */
+ public function testGetDisabledFileCache() {
+ // Ensure the returned FileCache is an instance of FileCache::class.
+ $file_cache = FileCacheFactory::get('test_foo_settings', []);
+ $this->assertInstanceOf(FileCache::class, $file_cache);
+
+ $configuration = FileCacheFactory::getConfiguration();
+ $configuration[FileCacheFactory::DISABLE_CACHE] = TRUE;
+ FileCacheFactory::setConfiguration($configuration);
+
+ // Ensure the returned FileCache is now an instance of NullFileCache::class.
+ $file_cache = FileCacheFactory::get('test_foo_settings', []);
+ $this->assertInstanceOf(NullFileCache::class, $file_cache);
+ }
+
+
+ /**
* @covers ::getConfiguration
* @covers ::setConfiguration
*/
diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php
index d8fbebc..085181c 100644
--- a/core/tests/Drupal/Tests/UnitTestCase.php
+++ b/core/tests/Drupal/Tests/UnitTestCase.php
@@ -42,7 +42,7 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase {
// Ensure that the NullFileCache implementation is used for the FileCache as
// unit tests should not be relying on caches implicitly.
- FileCacheFactory::setConfiguration(['default' => ['class' => '\Drupal\Component\FileCache\NullFileCache']]);
+ FileCacheFactory::setConfiguration([FileCacheFactory::DISABLE_CACHE => TRUE]);
// Ensure that FileCacheFactory has a prefix.
FileCacheFactory::setPrefix('prefix');