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