PredisShared.php 8.0 KB

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