CacheFactoryTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Drupal\Tests\Core\Cache;
  3. use Drupal\Core\DependencyInjection\ContainerBuilder;
  4. use Drupal\Core\Cache\CacheFactory;
  5. use Drupal\Core\Site\Settings;
  6. use Drupal\Tests\UnitTestCase;
  7. /**
  8. * @coversDefaultClass \Drupal\Core\Cache\CacheFactory
  9. * @group Cache
  10. */
  11. class CacheFactoryTest extends UnitTestCase {
  12. /**
  13. * Test that the cache factory falls back to the built-in default service.
  14. *
  15. * @covers ::__construct
  16. * @covers ::get
  17. */
  18. public function testCacheFactoryWithDefaultSettings() {
  19. $settings = new Settings([]);
  20. $cache_factory = new CacheFactory($settings);
  21. $container = new ContainerBuilder();
  22. $cache_factory->setContainer($container);
  23. $builtin_default_backend_factory = $this->createMock('\Drupal\Core\Cache\CacheFactoryInterface');
  24. $container->set('cache.backend.database', $builtin_default_backend_factory);
  25. $render_bin = $this->createMock('\Drupal\Core\Cache\CacheBackendInterface');
  26. $builtin_default_backend_factory->expects($this->once())
  27. ->method('get')
  28. ->with('render')
  29. ->will($this->returnValue($render_bin));
  30. $actual_bin = $cache_factory->get('render');
  31. $this->assertSame($render_bin, $actual_bin);
  32. }
  33. /**
  34. * Test that the cache factory falls back to customized default service.
  35. *
  36. * @covers ::__construct
  37. * @covers ::get
  38. */
  39. public function testCacheFactoryWithCustomizedDefaultBackend() {
  40. $settings = new Settings([
  41. 'cache' => [
  42. 'default' => 'cache.backend.custom',
  43. ],
  44. ]);
  45. $cache_factory = new CacheFactory($settings);
  46. $container = new ContainerBuilder();
  47. $cache_factory->setContainer($container);
  48. $custom_default_backend_factory = $this->createMock('\Drupal\Core\Cache\CacheFactoryInterface');
  49. $container->set('cache.backend.custom', $custom_default_backend_factory);
  50. $render_bin = $this->createMock('\Drupal\Core\Cache\CacheBackendInterface');
  51. $custom_default_backend_factory->expects($this->once())
  52. ->method('get')
  53. ->with('render')
  54. ->will($this->returnValue($render_bin));
  55. $actual_bin = $cache_factory->get('render');
  56. $this->assertSame($render_bin, $actual_bin);
  57. }
  58. /**
  59. * Test that the cache factory uses the correct default bin backend.
  60. *
  61. * @covers ::__construct
  62. * @covers ::get
  63. */
  64. public function testCacheFactoryWithDefaultBinBackend() {
  65. // Ensure the default bin backends are used before the configured default.
  66. $settings = new Settings([
  67. 'cache' => [
  68. 'default' => 'cache.backend.unused',
  69. ],
  70. ]);
  71. $default_bin_backends = [
  72. 'render' => 'cache.backend.custom',
  73. ];
  74. $cache_factory = new CacheFactory($settings, $default_bin_backends);
  75. $container = new ContainerBuilder();
  76. $cache_factory->setContainer($container);
  77. $custom_default_backend_factory = $this->createMock('\Drupal\Core\Cache\CacheFactoryInterface');
  78. $container->set('cache.backend.custom', $custom_default_backend_factory);
  79. $render_bin = $this->createMock('\Drupal\Core\Cache\CacheBackendInterface');
  80. $custom_default_backend_factory->expects($this->once())
  81. ->method('get')
  82. ->with('render')
  83. ->will($this->returnValue($render_bin));
  84. $actual_bin = $cache_factory->get('render');
  85. $this->assertSame($render_bin, $actual_bin);
  86. }
  87. /**
  88. * Test that the cache factory picks the correct per-bin service.
  89. *
  90. * @covers ::__construct
  91. * @covers ::get
  92. */
  93. public function testCacheFactoryWithSpecifiedPerBinBackend() {
  94. // Ensure the per-bin configuration is used before the configured default
  95. // and per-bin defaults.
  96. $settings = new Settings([
  97. 'cache' => [
  98. 'default' => 'cache.backend.unused',
  99. 'bins' => [
  100. 'render' => 'cache.backend.custom',
  101. ],
  102. ],
  103. ]);
  104. $default_bin_backends = [
  105. 'render' => 'cache.backend.unused',
  106. ];
  107. $cache_factory = new CacheFactory($settings, $default_bin_backends);
  108. $container = new ContainerBuilder();
  109. $cache_factory->setContainer($container);
  110. $custom_render_backend_factory = $this->createMock('\Drupal\Core\Cache\CacheFactoryInterface');
  111. $container->set('cache.backend.custom', $custom_render_backend_factory);
  112. $render_bin = $this->createMock('\Drupal\Core\Cache\CacheBackendInterface');
  113. $custom_render_backend_factory->expects($this->once())
  114. ->method('get')
  115. ->with('render')
  116. ->will($this->returnValue($render_bin));
  117. $actual_bin = $cache_factory->get('render');
  118. $this->assertSame($render_bin, $actual_bin);
  119. }
  120. }