More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Cache / CacheFactoryTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Cache;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Cache\CacheFactory;
7 use Drupal\Core\Site\Settings;
8 use Drupal\Tests\UnitTestCase;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\Cache\CacheFactory
12  * @group Cache
13  */
14 class CacheFactoryTest extends UnitTestCase {
15
16   /**
17    * Test that the cache factory falls back to the built-in default service.
18    *
19    * @covers ::__construct
20    * @covers ::get
21    */
22   public function testCacheFactoryWithDefaultSettings() {
23     $settings = new Settings([]);
24     $cache_factory = new CacheFactory($settings);
25
26     $container = new ContainerBuilder();
27     $cache_factory->setContainer($container);
28
29     $builtin_default_backend_factory = $this->getMock('\Drupal\Core\Cache\CacheFactoryInterface');
30     $container->set('cache.backend.database', $builtin_default_backend_factory);
31
32     $render_bin = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface');
33     $builtin_default_backend_factory->expects($this->once())
34       ->method('get')
35       ->with('render')
36       ->will($this->returnValue($render_bin));
37
38     $actual_bin = $cache_factory->get('render');
39     $this->assertSame($render_bin, $actual_bin);
40   }
41
42   /**
43    * Test that the cache factory falls back to customized default service.
44    *
45    * @covers ::__construct
46    * @covers ::get
47    */
48   public function testCacheFactoryWithCustomizedDefaultBackend() {
49     $settings = new Settings([
50       'cache' => [
51         'default' => 'cache.backend.custom',
52       ],
53     ]);
54     $cache_factory = new CacheFactory($settings);
55
56     $container = new ContainerBuilder();
57     $cache_factory->setContainer($container);
58
59     $custom_default_backend_factory = $this->getMock('\Drupal\Core\Cache\CacheFactoryInterface');
60     $container->set('cache.backend.custom', $custom_default_backend_factory);
61
62     $render_bin = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface');
63     $custom_default_backend_factory->expects($this->once())
64       ->method('get')
65       ->with('render')
66       ->will($this->returnValue($render_bin));
67
68     $actual_bin = $cache_factory->get('render');
69     $this->assertSame($render_bin, $actual_bin);
70   }
71
72   /**
73    * Test that the cache factory uses the correct default bin backend.
74    *
75    * @covers ::__construct
76    * @covers ::get
77    */
78   public function testCacheFactoryWithDefaultBinBackend() {
79     // Ensure the default bin backends are used before the configured default.
80     $settings = new Settings([
81       'cache' => [
82         'default' => 'cache.backend.unused',
83       ],
84     ]);
85
86     $default_bin_backends = [
87       'render' => 'cache.backend.custom',
88     ];
89
90     $cache_factory = new CacheFactory($settings, $default_bin_backends);
91
92     $container = new ContainerBuilder();
93     $cache_factory->setContainer($container);
94
95     $custom_default_backend_factory = $this->getMock('\Drupal\Core\Cache\CacheFactoryInterface');
96     $container->set('cache.backend.custom', $custom_default_backend_factory);
97
98     $render_bin = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface');
99     $custom_default_backend_factory->expects($this->once())
100       ->method('get')
101       ->with('render')
102       ->will($this->returnValue($render_bin));
103
104     $actual_bin = $cache_factory->get('render');
105     $this->assertSame($render_bin, $actual_bin);
106   }
107
108   /**
109    * Test that the cache factory picks the correct per-bin service.
110    *
111    * @covers ::__construct
112    * @covers ::get
113    */
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([
118       'cache' => [
119         'default' => 'cache.backend.unused',
120         'bins' => [
121           'render' => 'cache.backend.custom',
122         ],
123       ],
124     ]);
125
126     $default_bin_backends = [
127       'render' => 'cache.backend.unused',
128     ];
129
130     $cache_factory = new CacheFactory($settings, $default_bin_backends);
131
132     $container = new ContainerBuilder();
133     $cache_factory->setContainer($container);
134
135     $custom_render_backend_factory = $this->getMock('\Drupal\Core\Cache\CacheFactoryInterface');
136     $container->set('cache.backend.custom', $custom_render_backend_factory);
137
138     $render_bin = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface');
139     $custom_render_backend_factory->expects($this->once())
140       ->method('get')
141       ->with('render')
142       ->will($this->returnValue($render_bin));
143
144     $actual_bin = $cache_factory->get('render');
145     $this->assertSame($render_bin, $actual_bin);
146   }
147
148 }