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

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

/**
 * Class for unit testing.
 */
class ConsistentCacheBufferTestCase extends DrupalUnitTestCase {
  use CacheConsistentTestHelper;

  protected $cache_object;
  protected $variables;
  protected $transaction;
  public $bin = 'cache_consistent_test';

  /**
   * Setup unit tests.
   */
  protected function setUp() {
    $module_path = drupal_get_path('module', 'cache_consistent');
    parent::setUp();

    require_once DRUPAL_ROOT . '/' . $module_path . '/cache_consistent.inc';
    require_once DRUPAL_ROOT . '/' . $module_path . '/tests/cache_consistent.test.inc';

    $this->variables = new VariableTestHandler();
    $this->transaction = new TransactionTestHandler();

    // God damn m*****f*****.
    $GLOBALS['cache_consistent_variable_storage_handler'] = $this->variables;

    $this->variables['consistent_cache_default_class'] = 'DrupalMemoryCacheTest';
    $this->variables['consistent_cache_default_safe'] = FALSE;

    $this->cache_memory = new DrupalMemoryCacheTest($this->bin);
    $this->cache_memory->testGroup = 'Memory cache';

    $this->cache_object = new ConsistentCache($this->bin, array(
      'variable_handler' => $this->variables,
      'transaction_handler' => $this->transaction,
    ));
    $this->cache_object->testGroup = 'Cache Consistent';
    $this->transaction->setCacheObject($this->cache_object);
  }

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

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

  /**
   * Set variable.
   */
  public function setVariable($name, $value) {
    $this->variables[$name] = $value;
  }

  /**
   * 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);
  }
  /**
   * Test cache consistent using an in-memory cache backend.
   */
  public function testConsistentCache() {
    $this->cacheTest($this->cache_object);
    $this->bufferTest($this->cache_object, 'value0');
    $this->simpleTransactionTest($this->cache_object, 'value0');
    $this->doubleTransactionTest($this->cache_object);
    $this->nestedTransactionTest($this->cache_object, 'value1');
    $this->parallelTransactionTest($this->cache_object, 'value2');
    $this->outOfOrderTransactionTest($this->cache_object, 'value3');
    $this->wildcardTest($this->cache_object);
    $this->multiClearTest($this->cache_object);
    $this->pruneTest($this->cache_object);
    $this->integerCacheIdTest($this->cache_object);
    $this->clearAllTest($this->cache_object);
  }

}
