DatabaseBackendTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Drupal\KernelTests\Core\Cache;
  3. use Drupal\Core\Cache\DatabaseBackend;
  4. /**
  5. * Unit test of the database backend using the generic cache unit test base.
  6. *
  7. * @group Cache
  8. */
  9. class DatabaseBackendTest extends GenericCacheBackendUnitTestBase {
  10. /**
  11. * The max rows to use for test bins.
  12. *
  13. * @var int
  14. */
  15. protected static $maxRows = 100;
  16. /**
  17. * Modules to enable.
  18. *
  19. * @var array
  20. */
  21. public static $modules = ['system'];
  22. /**
  23. * Creates a new instance of DatabaseBackend.
  24. *
  25. * @return
  26. * A new DatabaseBackend object.
  27. */
  28. protected function createCacheBackend($bin) {
  29. return new DatabaseBackend($this->container->get('database'), $this->container->get('cache_tags.invalidator.checksum'), $bin, static::$maxRows);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function testSetGet() {
  35. parent::testSetGet();
  36. $backend = $this->getCacheBackend();
  37. // Set up a cache ID that is not ASCII and longer than 255 characters so we
  38. // can test cache ID normalization.
  39. $cid_long = str_repeat('愛€', 500);
  40. $cached_value_long = $this->randomMachineName();
  41. $backend->set($cid_long, $cached_value_long);
  42. $this->assertSame($cached_value_long, $backend->get($cid_long)->data, "Backend contains the correct value for long, non-ASCII cache id.");
  43. $cid_short = '愛1€';
  44. $cached_value_short = $this->randomMachineName();
  45. $backend->set($cid_short, $cached_value_short);
  46. $this->assertSame($cached_value_short, $backend->get($cid_short)->data, "Backend contains the correct value for short, non-ASCII cache id.");
  47. }
  48. /**
  49. * Tests the row count limiting of cache bin database tables.
  50. */
  51. public function testGarbageCollection() {
  52. $backend = $this->getCacheBackend();
  53. $max_rows = static::$maxRows;
  54. $this->assertSame(0, (int) $this->getNumRows());
  55. // Fill to just the limit.
  56. for ($i = 0; $i < $max_rows; $i++) {
  57. // Ensure that each cache item created happens in a different millisecond,
  58. // by waiting 1 ms (1000 microseconds). The garbage collection might
  59. // otherwise keep less than exactly 100 records (which is acceptable for
  60. // real-world cases, but not for this test).
  61. usleep(1000);
  62. $backend->set("test$i", $i);
  63. }
  64. $this->assertSame($max_rows, $this->getNumRows());
  65. // Garbage collection has no effect.
  66. $backend->garbageCollection();
  67. $this->assertSame($max_rows, $this->getNumRows());
  68. // Go one row beyond the limit.
  69. $backend->set('test' . ($max_rows + 1), $max_rows + 1);
  70. $this->assertSame($max_rows + 1, $this->getNumRows());
  71. // Garbage collection removes one row: the oldest.
  72. $backend->garbageCollection();
  73. $this->assertSame($max_rows, $this->getNumRows());
  74. $this->assertFalse($backend->get('test0'));
  75. }
  76. /**
  77. * Gets the number of rows in the test cache bin database table.
  78. *
  79. * @return int
  80. * The number of rows in the test cache bin database table.
  81. */
  82. protected function getNumRows() {
  83. $table = 'cache_' . $this->testBin;
  84. $connection = $this->container->get('database');
  85. $query = $connection->select($table);
  86. $query->addExpression('COUNT(cid)', 'cid');
  87. return (int) $query->execute()->fetchField();
  88. }
  89. }