View source
<?php
namespace Drupal\Tests\Component\FileCache;
use Drupal\Component\FileCache\FileCache;
use Drupal\Component\FileCache\NullFileCache;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Utility\Random;
use PHPUnit\Framework\TestCase;
class FileCacheFactoryTest extends TestCase {
protected function setUp() : void {
parent::setUp();
$configuration = [
'test_foo_settings' => [
'collection' => 'test-23',
'cache_backend_class' => '\\Drupal\\Tests\\Component\\FileCache\\StaticFileCacheBackend',
'cache_backend_configuration' => [
'bin' => 'dog',
],
],
];
FileCacheFactory::setConfiguration($configuration);
FileCacheFactory::setPrefix('prefix');
}
public function testGet() {
$file_cache = FileCacheFactory::get('test_foo_settings', []);
$filename = __DIR__ . '/Fixtures/llama-23.txt';
$realpath = realpath($filename);
$cid = 'prefix:test-23:' . $realpath;
$file_cache
->set($filename, 23);
$static_cache = new StaticFileCacheBackend([
'bin' => 'dog',
]);
$result = $static_cache
->fetch([
$cid,
]);
$this
->assertNotEmpty($result);
$file_cache
->delete($filename);
}
public function testGetNoPrefix() {
FileCacheFactory::setPrefix(NULL);
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Required prefix configuration is missing');
FileCacheFactory::get('test_foo_settings', []);
}
public function testGetDisabledFileCache() {
$file_cache = FileCacheFactory::get('test_foo_settings', []);
$this
->assertInstanceOf(FileCache::class, $file_cache);
$configuration = FileCacheFactory::getConfiguration();
$configuration[FileCacheFactory::DISABLE_CACHE] = TRUE;
FileCacheFactory::setConfiguration($configuration);
$file_cache = FileCacheFactory::get('test_foo_settings', []);
$this
->assertInstanceOf(NullFileCache::class, $file_cache);
}
public function testGetConfigurationOverrides($configuration, $arguments, $class) {
FileCacheFactory::setConfiguration($configuration);
$file_cache = FileCacheFactory::get('test_foo_settings', $arguments);
$this
->assertInstanceOf($class, $file_cache);
}
public function configurationDataProvider() {
$data = [];
$file_cache = $this
->getMockBuilder(FileCache::class)
->disableOriginalConstructor()
->getMock();
$class = get_class($file_cache);
$data['fallback-configuration'] = [
[],
[],
FileCache::class,
];
$data['default-configuration'] = [
[
'default' => [
'class' => $class,
],
],
[],
$class,
];
$data['collection-setting'] = [
[
'test_foo_settings' => [
'class' => $class,
],
],
[],
$class,
];
$data['default-plus-collection-setting'] = [
[
'default' => [
'class' => '\\stdClass',
],
'test_foo_settings' => [
'class' => $class,
],
],
[],
$class,
];
$data['default-plus-class-override'] = [
[
'default' => [
'class' => '\\stdClass',
],
],
[
'class' => $class,
],
$class,
];
$data['default-plus-class-plus-collection-setting'] = [
[
'default' => [
'class' => '\\stdClass',
],
'test_foo_settings' => [
'class' => $class,
],
],
[
'class' => '\\stdClass',
],
$class,
];
return $data;
}
public function testGetSetConfiguration() {
$configuration = FileCacheFactory::getConfiguration();
$configuration['test_foo_bar'] = [
'bar' => 'llama',
];
FileCacheFactory::setConfiguration($configuration);
$configuration = FileCacheFactory::getConfiguration();
$this
->assertEquals([
'bar' => 'llama',
], $configuration['test_foo_bar']);
}
public function testGetSetPrefix() {
$random = new Random();
$prefix = $random
->name(8, TRUE);
FileCacheFactory::setPrefix($prefix);
$this
->assertEquals($prefix, FileCacheFactory::getPrefix());
}
}