PredisShared.php 7.7 KB

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