<?php
/**
 * @file
 * Web test cases for Cache Consistent.
 */

// Make sure the ConsistentCacheTestCaseBase class is available.
require_once dirname(__FILE__) . '/cache_consistent.test.inc';
require_once dirname(__FILE__) . '/cache_consistent.tests.inc';

/**
 * Web test cases for Cache Consisten.
 */
class ConsistentCacheTestCase extends ConsistentCacheTestCaseBase {

  // Import test cases.
  use CacheConsistentTestHelper;

  /**
   * Info for these test cases.
   */
  public static function getInfo() {
    return array(
      'name' => 'Cache Consistent',
      'description' => 'Test the consistent cache functionality.',
      'group' => 'Cache Consistent',
    );
  }

  /**
   * Handle transaction callbacks to cache.
   */
  public function db_transaction($cache_object) {
    return new ConsistentCacheTestTransaction($cache_object);
  }

  /**
   * Set variable.
   */
  public function setVariable($name, $value) {
    variable_set($name, $value);
  }

  /**
   * Run test cases on a database cache backend.
   */
  public function testDatabaseCache() {
    $this->cacheTest($this->cache_default);
    $this->bufferTest($this->cache_default, 'value0');
    $this->simpleTransactionTest($this->cache_default, 'value0');
    $this->doubleTransactionTest($this->cache_default);
    $this->nestedTransactionTest($this->cache_default, 'value1');
    $this->parallelTransactionTest($this->cache_default, 'value2');
    $this->outOfOrderTransactionTest($this->cache_default, 'value3');
    $this->wildcardTest($this->cache_default);
    $this->multiClearTest($this->cache_default);
    $this->integerCacheIdTest($this->cache_default);
    $this->clearAllTest($this->cache_default);
  }

  /**
   * Run test cases on a non-database cache backend.
   */
  public function testNonDatabaseCache() {
    $this->cacheTest($this->cache_test);
    $this->bufferTest($this->cache_test, 'value1');
    $this->simpleTransactionTest($this->cache_test, 'value1');
    $this->doubleTransactionTest($this->cache_test);

    // The correct value for a working cache backend should be 'value1'.
    $this->nestedTransactionTest($this->cache_test, 'value2');

    // The correct value for a working cache backend should be 'value2'.
    $this->parallelTransactionTest($this->cache_test, 'value3');

    $this->outOfOrderTransactionTest($this->cache_test, 'value3');
    $this->wildcardTest($this->cache_test);
    $this->multiClearTest($this->cache_test);
    $this->integerCacheIdTest($this->cache_test);
    $this->clearAllTest($this->cache_test);
  }

  /**
   * Run test cases on a memory cache backend.
   */
  public function testMemoryCache() {
    $this->cacheTest($this->cache_memory);
    $this->bufferTest($this->cache_memory, 'value1');
    $this->simpleTransactionTest($this->cache_memory, 'value1');
    $this->doubleTransactionTest($this->cache_memory);

    // The correct value for a working cache backend should be 'value1'.
    $this->nestedTransactionTest($this->cache_memory, 'value2');

    // The correct value for a working cache backend should be 'value2'.
    $this->parallelTransactionTest($this->cache_memory, 'value3');

    $this->outOfOrderTransactionTest($this->cache_memory, 'value3');
    $this->wildcardTest($this->cache_memory);
    $this->multiClearTest($this->cache_memory);
    $this->integerCacheIdTest($this->cache_memory);
    $this->clearAllTest($this->cache_memory);
  }

  /**
   * Run test cases on a non-database cache backend through Cache Consistent.
   */
  public function testConsistentCache() {
    $this->cacheTest($this->cache_consistent);
    $this->bufferTest($this->cache_consistent, 'value0');
    $this->simpleTransactionTest($this->cache_consistent, 'value0');
    $this->doubleTransactionTest($this->cache_consistent);
    $this->nestedTransactionTest($this->cache_consistent, 'value1');
    $this->parallelTransactionTest($this->cache_consistent, 'value2');
    $this->outOfOrderTransactionTest($this->cache_consistent, 'value3');
    $this->wildcardTest($this->cache_consistent);
    $this->multiClearTest($this->cache_consistent);
    $this->pruneTest($this->cache_consistent);
    $this->integerCacheIdTest($this->cache_consistent);
    $this->clearAllTest($this->cache_consistent);
  }

  /**
   * Run test cases on a non-database cache backend through Cache Consistent.
   */
  public function testConsistentCacheMemory() {
    $this->cacheTest($this->cache_consistent_memory);
    $this->bufferTest($this->cache_consistent_memory, 'value0');
    $this->simpleTransactionTest($this->cache_consistent_memory, 'value0');
    $this->doubleTransactionTest($this->cache_consistent_memory);
    $this->nestedTransactionTest($this->cache_consistent_memory, 'value1');
    $this->parallelTransactionTest($this->cache_consistent_memory, 'value2');
    $this->outOfOrderTransactionTest($this->cache_consistent_memory, 'value3');
    $this->wildcardTest($this->cache_consistent_memory);
    $this->multiClearTest($this->cache_consistent_memory);
    $this->pruneTest($this->cache_consistent_memory);
    $this->integerCacheIdTest($this->cache_consistent_memory);
    $this->clearAllTest($this->cache_consistent_memory);
  }

}
