CacheFactoryTest.php in Drupal 8
File
core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php
View source
<?php
namespace Drupal\Tests\Core\Cache;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Cache\CacheFactory;
use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;
class CacheFactoryTest extends UnitTestCase {
public function testCacheFactoryWithDefaultSettings() {
$settings = new Settings([]);
$cache_factory = new CacheFactory($settings);
$container = new ContainerBuilder();
$cache_factory
->setContainer($container);
$builtin_default_backend_factory = $this
->createMock('\\Drupal\\Core\\Cache\\CacheFactoryInterface');
$container
->set('cache.backend.database', $builtin_default_backend_factory);
$render_bin = $this
->createMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
$builtin_default_backend_factory
->expects($this
->once())
->method('get')
->with('render')
->will($this
->returnValue($render_bin));
$actual_bin = $cache_factory
->get('render');
$this
->assertSame($render_bin, $actual_bin);
}
public function testCacheFactoryWithCustomizedDefaultBackend() {
$settings = new Settings([
'cache' => [
'default' => 'cache.backend.custom',
],
]);
$cache_factory = new CacheFactory($settings);
$container = new ContainerBuilder();
$cache_factory
->setContainer($container);
$custom_default_backend_factory = $this
->createMock('\\Drupal\\Core\\Cache\\CacheFactoryInterface');
$container
->set('cache.backend.custom', $custom_default_backend_factory);
$render_bin = $this
->createMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
$custom_default_backend_factory
->expects($this
->once())
->method('get')
->with('render')
->will($this
->returnValue($render_bin));
$actual_bin = $cache_factory
->get('render');
$this
->assertSame($render_bin, $actual_bin);
}
public function testCacheFactoryWithDefaultBinBackend() {
$settings = new Settings([
'cache' => [
'default' => 'cache.backend.unused',
],
]);
$default_bin_backends = [
'render' => 'cache.backend.custom',
];
$cache_factory = new CacheFactory($settings, $default_bin_backends);
$container = new ContainerBuilder();
$cache_factory
->setContainer($container);
$custom_default_backend_factory = $this
->createMock('\\Drupal\\Core\\Cache\\CacheFactoryInterface');
$container
->set('cache.backend.custom', $custom_default_backend_factory);
$render_bin = $this
->createMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
$custom_default_backend_factory
->expects($this
->once())
->method('get')
->with('render')
->will($this
->returnValue($render_bin));
$actual_bin = $cache_factory
->get('render');
$this
->assertSame($render_bin, $actual_bin);
}
public function testCacheFactoryWithSpecifiedPerBinBackend() {
$settings = new Settings([
'cache' => [
'default' => 'cache.backend.unused',
'bins' => [
'render' => 'cache.backend.custom',
],
],
]);
$default_bin_backends = [
'render' => 'cache.backend.unused',
];
$cache_factory = new CacheFactory($settings, $default_bin_backends);
$container = new ContainerBuilder();
$cache_factory
->setContainer($container);
$custom_render_backend_factory = $this
->createMock('\\Drupal\\Core\\Cache\\CacheFactoryInterface');
$container
->set('cache.backend.custom', $custom_render_backend_factory);
$render_bin = $this
->createMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
$custom_render_backend_factory
->expects($this
->once())
->method('get')
->with('render')
->will($this
->returnValue($render_bin));
$actual_bin = $cache_factory
->get('render');
$this
->assertSame($render_bin, $actual_bin);
}
}