PredisShared.php 8.2 KB

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