PredisShared.php 7.1 KB

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