PredisShared.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. require_once '../lib/Predis.php';
  3. if (I_AM_AWARE_OF_THE_DESTRUCTIVE_POWER_OF_THIS_TEST_SUITE !== true) {
  4. exit('Please set the I_AM_AWARE_OF_THE_DESTRUCTIVE_POWER_OF_THIS_TEST_SUITE constant to TRUE if you want to proceed.');
  5. }
  6. if (!function_exists('array_union')) {
  7. function array_union(Array $a, Array $b) {
  8. return array_merge($a, array_diff($b, $a));
  9. }
  10. }
  11. class RC {
  12. const SERVER_HOST = '127.0.0.1';
  13. const SERVER_PORT = 6379;
  14. const DEFAULT_DATABASE = 15;
  15. const WIPE_OUT = 1;
  16. const EXCEPTION_WRONG_TYPE = 'Operation against a key holding the wrong kind of value';
  17. const EXCEPTION_NO_SUCH_KEY = 'no such key';
  18. const EXCEPTION_OUT_OF_RANGE = 'index out of range';
  19. const EXCEPTION_INVALID_DB_IDX = 'invalid DB index';
  20. const EXCEPTION_EXEC_NO_MULTI = 'EXEC without MULTI';
  21. private static $_connection;
  22. private static function createConnection() {
  23. $serverProfile = Predis\RedisServerProfile::get('dev');
  24. $connection = new Predis\Client(array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT), $serverProfile);
  25. $connection->connect();
  26. $connection->selectDatabase(RC::DEFAULT_DATABASE);
  27. return $connection;
  28. }
  29. public static function getConnection() {
  30. if (self::$_connection === null || !self::$_connection->isConnected()) {
  31. self::$_connection = self::createConnection();
  32. }
  33. return self::$_connection;
  34. }
  35. public static function resetConnection() {
  36. if (self::$_connection !== null && self::$_connection->isConnected()) {
  37. self::$_connection->disconnect();
  38. self::$_connection = self::createConnection();
  39. }
  40. }
  41. public static function helperForBlockingPops($op) {
  42. // TODO: I admit that this helper is kinda lame and it does not run
  43. // in a separate process to properly test BLPOP/BRPOP
  44. $redisUri = sprintf('redis://%s:%d/?database=%d', RC::SERVER_HOST, RC::SERVER_PORT, RC::DEFAULT_DATABASE);
  45. $handle = popen('php', 'w');
  46. fwrite($handle, "<?php
  47. require '../lib/Predis.php';
  48. \$redis = Predis\Client::create('$redisUri');
  49. \$redis->rpush('{$op}1', 'a');
  50. \$redis->rpush('{$op}2', 'b');
  51. \$redis->rpush('{$op}3', 'c');
  52. \$redis->rpush('{$op}1', 'd');
  53. ?>");
  54. pclose($handle);
  55. }
  56. public static function getArrayOfNumbers() {
  57. return array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  58. }
  59. public static function getKeyValueArray() {
  60. return array(
  61. 'foo' => 'bar',
  62. 'hoge' => 'piyo',
  63. 'foofoo' => 'barbar',
  64. );
  65. }
  66. public static function getNamespacedKeyValueArray() {
  67. return array(
  68. 'metavar:foo' => 'bar',
  69. 'metavar:hoge' => 'piyo',
  70. 'metavar:foofoo' => 'barbar',
  71. );
  72. }
  73. public static function getZSetArray() {
  74. return array(
  75. 'a' => -10, 'b' => 0, 'c' => 10, 'd' => 20, 'e' => 20, 'f' => 30
  76. );
  77. }
  78. public static function sameValuesInArrays($arrayA, $arrayB) {
  79. if (count($arrayA) != count($arrayB)) {
  80. return false;
  81. }
  82. return count(array_diff($arrayA, $arrayB)) == 0;
  83. }
  84. public static function testForServerException($testcaseInstance, $expectedMessage, $wrapFunction) {
  85. $thrownException = null;
  86. try {
  87. $wrapFunction($testcaseInstance);
  88. }
  89. catch (Predis\ServerException $exception) {
  90. $thrownException = $exception;
  91. }
  92. $testcaseInstance->assertType('Predis\ServerException', $thrownException);
  93. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  94. }
  95. public static function testForClientException($testcaseInstance, $expectedMessage, $wrapFunction) {
  96. $thrownException = null;
  97. try {
  98. $wrapFunction($testcaseInstance);
  99. }
  100. catch (Predis\ClientException $exception) {
  101. $thrownException = $exception;
  102. }
  103. $testcaseInstance->assertType('Predis\ClientException', $thrownException);
  104. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  105. }
  106. public static function pushTailAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) {
  107. if ($wipeOut == true) {
  108. $client->delete($keyName);
  109. }
  110. foreach ($values as $value) {
  111. $client->pushTail($keyName, $value);
  112. }
  113. return $values;
  114. }
  115. public static function setAddAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) {
  116. if ($wipeOut == true) {
  117. $client->delete($keyName);
  118. }
  119. foreach ($values as $value) {
  120. $client->setAdd($keyName, $value);
  121. }
  122. return $values;
  123. }
  124. public static function zsetAddAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) {
  125. // $values: array(SCORE => VALUE, ...);
  126. if ($wipeOut == true) {
  127. $client->delete($keyName);
  128. }
  129. foreach ($values as $value => $score) {
  130. $client->zsetAdd($keyName, $score, $value);
  131. }
  132. return $values;
  133. }
  134. public static function getConnectionParametersArgumentsArray() {
  135. return array(
  136. 'host' => '10.0.0.1', 'port' => 6380, 'connection_timeout' => 10, 'read_write_timeout' => 30,
  137. 'database' => 5, 'password' => 'dbpassword', 'alias' => 'connection_alias'
  138. );
  139. }
  140. public static function getConnectionParametersArgumentsString($arguments = null) {
  141. // TODO: must be improved
  142. $args = $arguments ?: RC::getConnectionParametersArgumentsArray();
  143. $paramsString = "redis://{$args['host']}:{$args['port']}/";
  144. $paramsString .= "?connection_timeout={$args['connection_timeout']}&read_write_timeout={$args['read_write_timeout']}";
  145. $paramsString .= "&database={$args['database']}&password={$args['password']}&alias={$args['alias']}";
  146. return $paramsString;
  147. }
  148. }
  149. ?>