You are here

class GarbageCollectionTest in Service Container 7

Same name and namespace in other branches
  1. 7.2 lib/Drupal/service_container/Tests/KeyValue/GarbageCollectionTest.php \Drupal\service_container\Tests\KeyValue\GarbageCollectionTest

Tests garbage collection for the the expirable key-value database storage.

@group KeyValueStore

Hierarchy

Expanded class hierarchy of GarbageCollectionTest

File

lib/Drupal/service_container/Tests/KeyValue/GarbageCollectionTest.php, line 17
Contains Drupal\system\Tests\KeyValueStore\GarbageCollectionTest.

Namespace

Drupal\service_container\Tests\KeyValue
View source
class GarbageCollectionTest extends ServiceContainerIntegrationTestBase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'KeyValue GarbageCollection',
      'description' => 'Tests garbage collection for the the expirable key-value database storage.',
      'group' => 'service_container',
    );
  }

  /**
   * Tests garbage collection.
   */
  public function testGarbageCollection() {
    $collection = $this
      ->randomName();
    $store = $this->container
      ->get('keyvalue.expirable.database')
      ->get($collection);

    // Insert some items and confirm that they're set.
    for ($i = 0; $i <= 3; $i++) {
      $store
        ->setWithExpire('key_' . $i, (object) array(
        'key' => $this
          ->randomName(),
      ), rand(500, 100000));
    }
    $this
      ->assertIdentical(sizeof($store
      ->getAll()), 4, 'Four items were written to the storage.');

    // Manually expire the data.
    for ($i = 0; $i <= 3; $i++) {
      db_merge('key_value_expire')
        ->key(array(
        'name' => 'key_' . $i,
        'collection' => $collection,
      ))
        ->fields(array(
        'expire' => REQUEST_TIME - 1,
      ))
        ->execute();
    }

    // Perform a new set operation and then manually destruct the object to
    // trigger garbage collection.
    $store
      ->setWithExpire('autumn', 'winter', rand(500, 1000000));
    $store
      ->destruct();

    // Query the database and confirm that the stale records were deleted.
    $result = db_query('SELECT name, value FROM {key_value_expire} WHERE collection = :collection', array(
      ':collection' => $collection,
    ))
      ->fetchAll();
    $this
      ->assertIdentical(sizeof($result), 1, 'Only one item remains after garbage collection');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GarbageCollectionTest::getInfo public static function
GarbageCollectionTest::testGarbageCollection public function Tests garbage collection.
ServiceContainerIntegrationTestBase::$container protected property The dependency injection container usable in the test.
ServiceContainerIntegrationTestBase::$profile protected property The profile to install as a basis for testing. 1
ServiceContainerIntegrationTestBase::setUp protected function 5