CachedStorageTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config\Storage;
  3. use Drupal\Core\Config\FileStorage;
  4. use Drupal\Core\Config\CachedStorage;
  5. use Drupal\Core\DependencyInjection\ContainerBuilder;
  6. use Drupal\Core\StreamWrapper\PublicStream;
  7. use Symfony\Component\DependencyInjection\Reference;
  8. /**
  9. * Tests CachedStorage operations.
  10. *
  11. * @group config
  12. */
  13. class CachedStorageTest extends ConfigStorageTestBase {
  14. /**
  15. * The cache backend the cached storage is using.
  16. *
  17. * @var \Drupal\Core\Cache\CacheBackendInterface
  18. */
  19. protected $cache;
  20. /**
  21. * The file storage the cached storage is using.
  22. *
  23. * @var \Drupal\Core\Config\FileStorage
  24. */
  25. protected $fileStorage;
  26. protected function setUp() {
  27. parent::setUp();
  28. // Create a directory.
  29. $dir = PublicStream::basePath() . '/config';
  30. $this->fileStorage = new FileStorage($dir);
  31. $this->storage = new CachedStorage($this->fileStorage, \Drupal::service('cache.config'));
  32. $this->cache = \Drupal::service('cache_factory')->get('config');
  33. // ::listAll() verifications require other configuration data to exist.
  34. $this->storage->write('system.performance', []);
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function testInvalidStorage() {
  40. $this->markTestSkipped('No-op as this test does not make sense');
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function read($name) {
  46. $data = $this->cache->get($name);
  47. // Cache misses fall through to the underlying storage.
  48. return $data ? $data->data : $this->fileStorage->read($name);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function insert($name, $data) {
  54. $this->fileStorage->write($name, $data);
  55. $this->cache->set($name, $data);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. protected function update($name, $data) {
  61. $this->fileStorage->write($name, $data);
  62. $this->cache->set($name, $data);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function delete($name) {
  68. $this->cache->delete($name);
  69. unlink($this->fileStorage->getFilePath($name));
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function containerBuild(ContainerBuilder $container) {
  75. parent::containerBuild($container);
  76. // Use the regular database cache backend to aid testing.
  77. $container->register('cache_factory', 'Drupal\Core\Cache\DatabaseBackendFactory')
  78. ->addArgument(new Reference('database'))
  79. ->addArgument(new Reference('cache_tags.invalidator.checksum'));
  80. }
  81. }