PredisShared.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_OFFSET_RANGE = 'offset is out of range';
  20. const EXCEPTION_INVALID_DB_IDX = 'invalid DB index';
  21. const EXCEPTION_VALUE_NOT_INT = 'value is not an integer';
  22. const EXCEPTION_EXEC_NO_MULTI = 'EXEC without MULTI';
  23. const EXCEPTION_SETEX_TTL = 'invalid expire time in SETEX';
  24. const EXCEPTION_HASH_VALNOTINT = 'hash value is not an integer';
  25. const EXCEPTION_BIT_VALUE = 'bit is not an integer or out of range';
  26. const EXCEPTION_BIT_OFFSET = 'bit offset is not an integer or out of range';
  27. private static $_connection;
  28. public static function getConnectionArguments() {
  29. return array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT);
  30. }
  31. public static function getConnectionParameters() {
  32. return new Predis\ConnectionParameters(array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT));
  33. }
  34. private static function createConnection() {
  35. $serverProfile = Predis\RedisServerProfile::get('dev');
  36. $connection = new Predis\Client(RC::getConnectionArguments(), $serverProfile);
  37. $connection->connect();
  38. $connection->select(RC::DEFAULT_DATABASE);
  39. return $connection;
  40. }
  41. public static function getConnection($new = false) {
  42. if ($new == true) {
  43. return self::createConnection();
  44. }
  45. if (self::$_connection === null || !self::$_connection->isConnected()) {
  46. self::$_connection = self::createConnection();
  47. }
  48. return self::$_connection;
  49. }
  50. public static function resetConnection() {
  51. if (self::$_connection !== null && self::$_connection->isConnected()) {
  52. self::$_connection->disconnect();
  53. self::$_connection = self::createConnection();
  54. }
  55. }
  56. public static function helperForBlockingPops($op) {
  57. // TODO: I admit that this helper is kinda lame and it does not run
  58. // in a separate process to properly test BLPOP/BRPOP
  59. $redisUri = sprintf('redis://%s:%d/?database=%d', RC::SERVER_HOST, RC::SERVER_PORT, RC::DEFAULT_DATABASE);
  60. $handle = popen('php', 'w');
  61. fwrite($handle, "<?php
  62. require '../lib/Predis.php';
  63. \$redis = Predis\Client::create('$redisUri');
  64. \$redis->rpush('{$op}1', 'a');
  65. \$redis->rpush('{$op}2', 'b');
  66. \$redis->rpush('{$op}3', 'c');
  67. \$redis->rpush('{$op}1', 'd');
  68. ?>");
  69. pclose($handle);
  70. }
  71. public static function getArrayOfNumbers() {
  72. return array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  73. }
  74. public static function getKeyValueArray() {
  75. return array(
  76. 'foo' => 'bar',
  77. 'hoge' => 'piyo',
  78. 'foofoo' => 'barbar',
  79. );
  80. }
  81. public static function getNamespacedKeyValueArray() {
  82. return array(
  83. 'metavar:foo' => 'bar',
  84. 'metavar:hoge' => 'piyo',
  85. 'metavar:foofoo' => 'barbar',
  86. );
  87. }
  88. public static function getZSetArray() {
  89. return array(
  90. 'a' => -10, 'b' => 0, 'c' => 10, 'd' => 20, 'e' => 20, 'f' => 30
  91. );
  92. }
  93. public static function sameValuesInArrays($arrayA, $arrayB) {
  94. if (count($arrayA) != count($arrayB)) {
  95. return false;
  96. }
  97. return count(array_diff($arrayA, $arrayB)) == 0;
  98. }
  99. public static function testForServerException($testcaseInstance, $expectedMessage, $wrapFunction) {
  100. $thrownException = null;
  101. try {
  102. $wrapFunction($testcaseInstance);
  103. }
  104. catch (Predis\ServerException $exception) {
  105. $thrownException = $exception;
  106. }
  107. $testcaseInstance->assertType('Predis\ServerException', $thrownException);
  108. if (isset($expectedMessage)) {
  109. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  110. }
  111. }
  112. public static function testForClientException($testcaseInstance, $expectedMessage, $wrapFunction) {
  113. $thrownException = null;
  114. try {
  115. $wrapFunction($testcaseInstance);
  116. }
  117. catch (Predis\ClientException $exception) {
  118. $thrownException = $exception;
  119. }
  120. $testcaseInstance->assertType('Predis\ClientException', $thrownException);
  121. if (isset($expectedMessage)) {
  122. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  123. }
  124. }
  125. public static function testForCommunicationException($testcaseInstance, $expectedMessage, $wrapFunction) {
  126. $thrownException = null;
  127. try {
  128. $wrapFunction($testcaseInstance);
  129. }
  130. catch (Predis\CommunicationException $exception) {
  131. $thrownException = $exception;
  132. }
  133. $testcaseInstance->assertType('Predis\CommunicationException', $thrownException);
  134. if (isset($expectedMessage)) {
  135. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  136. }
  137. }
  138. public static function testForAbortedMultiExecException($testcaseInstance, $wrapFunction) {
  139. $thrownException = null;
  140. try {
  141. $wrapFunction($testcaseInstance);
  142. }
  143. catch (Predis\AbortedMultiExec $exception) {
  144. $thrownException = $exception;
  145. }
  146. $testcaseInstance->assertType('Predis\AbortedMultiExec', $thrownException);
  147. }
  148. public static function pushTailAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) {
  149. if ($wipeOut == true) {
  150. $client->del($keyName);
  151. }
  152. foreach ($values as $value) {
  153. $client->rpush($keyName, $value);
  154. }
  155. return $values;
  156. }
  157. public static function setAddAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) {
  158. if ($wipeOut == true) {
  159. $client->del($keyName);
  160. }
  161. foreach ($values as $value) {
  162. $client->sadd($keyName, $value);
  163. }
  164. return $values;
  165. }
  166. public static function zsetAddAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) {
  167. // $values: array(SCORE => VALUE, ...);
  168. if ($wipeOut == true) {
  169. $client->del($keyName);
  170. }
  171. foreach ($values as $value => $score) {
  172. $client->zadd($keyName, $score, $value);
  173. }
  174. return $values;
  175. }
  176. public static function getConnectionParametersArgumentsArray() {
  177. return array(
  178. 'host' => '10.0.0.1', 'port' => 6380, 'connection_timeout' => 10, 'read_write_timeout' => 30,
  179. 'database' => 5, 'password' => 'dbpassword', 'alias' => 'connection_alias'
  180. );
  181. }
  182. public static function getConnectionParametersArgumentsString($arguments = null) {
  183. // TODO: must be improved
  184. $args = $arguments ?: RC::getConnectionParametersArgumentsArray();
  185. $paramsString = "redis://{$args['host']}:{$args['port']}/";
  186. $paramsString .= "?connection_timeout={$args['connection_timeout']}&read_write_timeout={$args['read_write_timeout']}";
  187. $paramsString .= "&database={$args['database']}&password={$args['password']}&alias={$args['alias']}";
  188. return $paramsString;
  189. }
  190. }
  191. ?>