PredisShared.php 7.0 KB

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