AbstractUnitTestCase.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. abstract class Redis_Tests_AbstractUnitTestCase extends DrupalUnitTestCase
  3. {
  4. /**
  5. * @var boolean
  6. */
  7. static protected $loaderEnabled = false;
  8. /**
  9. * Enable the autoloader
  10. *
  11. * This exists in this class in case the autoloader is not set into the
  12. * settings.php file or another way
  13. *
  14. * @return void|boolean
  15. */
  16. static protected function enableAutoload()
  17. {
  18. if (self::$loaderEnabled) {
  19. return;
  20. }
  21. if (class_exists('Redis_Client')) {
  22. return;
  23. }
  24. spl_autoload_register(function ($className) {
  25. $parts = explode('_', $className);
  26. if ('Redis' === $parts[0]) {
  27. $filename = __DIR__ . '/../lib/' . implode('/', $parts) . '.php';
  28. return (bool) include_once $filename;
  29. }
  30. return false;
  31. }, null, true);
  32. self::$loaderEnabled = true;
  33. }
  34. /**
  35. * Drupal $conf array backup
  36. *
  37. * @var array
  38. */
  39. private $originalConf = array(
  40. 'cache_lifetime' => null,
  41. 'cache_prefix' => null,
  42. 'redis_client_interface' => null,
  43. 'redis_eval_enabled' => null,
  44. 'redis_flush_mode' => null,
  45. 'redis_perm_ttl' => null,
  46. );
  47. /**
  48. * Prepare Drupal environmment for testing
  49. */
  50. final private function prepareDrupalEnvironment()
  51. {
  52. // Site on which the tests are running may define this variable
  53. // in their own settings.php file case in which it will be merged
  54. // with testing site
  55. global $conf;
  56. foreach (array_keys($this->originalConf) as $key) {
  57. if (isset($conf[$key])) {
  58. $this->originalConf[$key] = $conf[$key];
  59. unset($conf[$key]);
  60. }
  61. }
  62. $conf['cache_prefix'] = $this->testId;
  63. }
  64. /**
  65. * Restore Drupal environment after testing.
  66. */
  67. final private function restoreDrupalEnvironment()
  68. {
  69. $GLOBALS['conf'] = $this->originalConf + $GLOBALS['conf'];
  70. }
  71. /**
  72. * Prepare client manager
  73. */
  74. final private function prepareClientManager()
  75. {
  76. $interface = $this->getClientInterface();
  77. if (null === $interface) {
  78. throw new \Exception("Test skipped due to missing driver");
  79. }
  80. $GLOBALS['conf']['redis_client_interface'] = $interface;
  81. Redis_Client::reset();
  82. }
  83. /**
  84. * Restore client manager
  85. */
  86. final private function restoreClientManager()
  87. {
  88. Redis_Client::reset();
  89. }
  90. /**
  91. * Set up the Redis configuration.
  92. *
  93. * Set up the needed variables using variable_set() if necessary.
  94. *
  95. * @return string
  96. * Client interface or null if not exists
  97. */
  98. abstract protected function getClientInterface();
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function setUp()
  103. {
  104. self::enableAutoload();
  105. $this->prepareDrupalEnvironment();
  106. $this->prepareClientManager();
  107. parent::setUp();
  108. drupal_install_schema('system');
  109. drupal_install_schema('locale');
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function tearDown()
  115. {
  116. drupal_uninstall_schema('locale');
  117. drupal_uninstall_schema('system');
  118. $this->restoreDrupalEnvironment();
  119. $this->restoreClientManager();
  120. parent::tearDown();
  121. }
  122. }