3 namespace Drupal\Tests\Core\Cache;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Cache\CacheFactory;
7 use Drupal\Core\Site\Settings;
8 use Drupal\Tests\UnitTestCase;
11 * @coversDefaultClass \Drupal\Core\Cache\CacheFactory
14 class CacheFactoryTest extends UnitTestCase {
17 * Test that the cache factory falls back to the built-in default service.
19 * @covers ::__construct
22 public function testCacheFactoryWithDefaultSettings() {
23 $settings = new Settings([]);
24 $cache_factory = new CacheFactory($settings);
26 $container = new ContainerBuilder();
27 $cache_factory->setContainer($container);
29 $builtin_default_backend_factory = $this->getMock('\Drupal\Core\Cache\CacheFactoryInterface');
30 $container->set('cache.backend.database', $builtin_default_backend_factory);
32 $render_bin = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface');
33 $builtin_default_backend_factory->expects($this->once())
36 ->will($this->returnValue($render_bin));
38 $actual_bin = $cache_factory->get('render');
39 $this->assertSame($render_bin, $actual_bin);
43 * Test that the cache factory falls back to customized default service.
45 * @covers ::__construct
48 public function testCacheFactoryWithCustomizedDefaultBackend() {
49 $settings = new Settings([
51 'default' => 'cache.backend.custom',
54 $cache_factory = new CacheFactory($settings);
56 $container = new ContainerBuilder();
57 $cache_factory->setContainer($container);
59 $custom_default_backend_factory = $this->getMock('\Drupal\Core\Cache\CacheFactoryInterface');
60 $container->set('cache.backend.custom', $custom_default_backend_factory);
62 $render_bin = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface');
63 $custom_default_backend_factory->expects($this->once())
66 ->will($this->returnValue($render_bin));
68 $actual_bin = $cache_factory->get('render');
69 $this->assertSame($render_bin, $actual_bin);
73 * Test that the cache factory uses the correct default bin backend.
75 * @covers ::__construct
78 public function testCacheFactoryWithDefaultBinBackend() {
79 // Ensure the default bin backends are used before the configured default.
80 $settings = new Settings([
82 'default' => 'cache.backend.unused',
86 $default_bin_backends = [
87 'render' => 'cache.backend.custom',
90 $cache_factory = new CacheFactory($settings, $default_bin_backends);
92 $container = new ContainerBuilder();
93 $cache_factory->setContainer($container);
95 $custom_default_backend_factory = $this->getMock('\Drupal\Core\Cache\CacheFactoryInterface');
96 $container->set('cache.backend.custom', $custom_default_backend_factory);
98 $render_bin = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface');
99 $custom_default_backend_factory->expects($this->once())
102 ->will($this->returnValue($render_bin));
104 $actual_bin = $cache_factory->get('render');
105 $this->assertSame($render_bin, $actual_bin);
109 * Test that the cache factory picks the correct per-bin service.
111 * @covers ::__construct
114 public function testCacheFactoryWithSpecifiedPerBinBackend() {
115 // Ensure the per-bin configuration is used before the configured default
116 // and per-bin defaults.
117 $settings = new Settings([
119 'default' => 'cache.backend.unused',
121 'render' => 'cache.backend.custom',
126 $default_bin_backends = [
127 'render' => 'cache.backend.unused',
130 $cache_factory = new CacheFactory($settings, $default_bin_backends);
132 $container = new ContainerBuilder();
133 $cache_factory->setContainer($container);
135 $custom_render_backend_factory = $this->getMock('\Drupal\Core\Cache\CacheFactoryInterface');
136 $container->set('cache.backend.custom', $custom_render_backend_factory);
138 $render_bin = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface');
139 $custom_render_backend_factory->expects($this->once())
142 ->will($this->returnValue($render_bin));
144 $actual_bin = $cache_factory->get('render');
145 $this->assertSame($render_bin, $actual_bin);