PredisShared.php 8.0 KB

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