PredisShared.php 8.4 KB

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