PredisShared.php 8.0 KB

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