PredisShared.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. if (!file_exists(__DIR__.'/enable.tests')) {
  11. exit(
  12. "Please create an empty file named 'enable.tests' inside the test directory ".
  13. "in order to proceed.\n\n*** DO NOT *** run this test suite against servers in ".
  14. "a production environment or containing data you are interested in!\n"
  15. );
  16. }
  17. if (!function_exists('array_union')) {
  18. function array_union(Array $a, Array $b)
  19. {
  20. return array_merge($a, array_diff($b, $a));
  21. }
  22. }
  23. class RC
  24. {
  25. const SERVER_VERSION = TEST_SERVER_VERSION;
  26. const SERVER_HOST = TEST_SERVER_HOST;
  27. const SERVER_PORT = TEST_SERVER_PORT;
  28. const DEFAULT_DATABASE = TEST_SERVER_DBNUM;
  29. const WIPE_OUT = 1;
  30. const EXCEPTION_WRONG_TYPE = 'ERR Operation against a key holding the wrong kind of value';
  31. const EXCEPTION_NO_SUCH_KEY = 'ERR no such key';
  32. const EXCEPTION_OUT_OF_RANGE = 'ERR index out of range';
  33. const EXCEPTION_OFFSET_RANGE = 'ERR offset is out of range';
  34. const EXCEPTION_INVALID_DB_IDX = 'ERR invalid DB index';
  35. const EXCEPTION_VALUE_NOT_INT = 'ERR value is not an integer';
  36. const EXCEPTION_EXEC_NO_MULTI = 'ERR EXEC without MULTI';
  37. const EXCEPTION_SETEX_TTL = 'ERR invalid expire time in SETEX';
  38. const EXCEPTION_HASH_VALNOTINT = 'ERR hash value is not an integer';
  39. const EXCEPTION_BIT_VALUE = 'ERR bit is not an integer or out of range';
  40. const EXCEPTION_BIT_OFFSET = 'ERR bit offset is not an integer or out of range';
  41. private static $_connection;
  42. public static function getConnectionArguments(Array $additional = array())
  43. {
  44. return array_merge(array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT), $additional);
  45. }
  46. public static function getConnectionParameters(Array $additional = array())
  47. {
  48. return new Predis\ConnectionParameters(self::getConnectionArguments($additional));
  49. }
  50. public static function createConnection(Array $additional = array())
  51. {
  52. $serverProfile = Predis\Profiles\ServerProfile::get(self::SERVER_VERSION);
  53. $connection = new Predis\Client(RC::getConnectionArguments($additional), $serverProfile);
  54. $connection->connect();
  55. $connection->select(RC::DEFAULT_DATABASE);
  56. return $connection;
  57. }
  58. public static function getConnection($new = false)
  59. {
  60. if ($new == true) {
  61. return self::createConnection();
  62. }
  63. if (self::$_connection === null || !self::$_connection->isConnected()) {
  64. self::$_connection = self::createConnection();
  65. }
  66. return self::$_connection;
  67. }
  68. public static function resetConnection()
  69. {
  70. if (self::$_connection !== null && self::$_connection->isConnected()) {
  71. self::$_connection->disconnect();
  72. self::$_connection = self::createConnection();
  73. }
  74. }
  75. public static function helperForBlockingPops($op)
  76. {
  77. // TODO: I admit that this helper is kinda lame and it does not run
  78. // in a separate process to properly test BLPOP/BRPOP
  79. $redisUri = sprintf('tcp://%s:%d/?database=%d', RC::SERVER_HOST, RC::SERVER_PORT, RC::DEFAULT_DATABASE);
  80. $handle = popen('php', 'w');
  81. fwrite($handle, "<?php
  82. require __DIR__.'/test/bootstrap.php';
  83. \$redis = new Predis\Client('$redisUri');
  84. \$redis->rpush('{$op}1', 'a');
  85. \$redis->rpush('{$op}2', 'b');
  86. \$redis->rpush('{$op}3', 'c');
  87. \$redis->rpush('{$op}1', 'd');
  88. ?>");
  89. pclose($handle);
  90. }
  91. public static function getArrayOfNumbers()
  92. {
  93. return array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  94. }
  95. public static function getKeyValueArray()
  96. {
  97. return array(
  98. 'foo' => 'bar',
  99. 'hoge' => 'piyo',
  100. 'foofoo' => 'barbar',
  101. );
  102. }
  103. public static function getNamespacedKeyValueArray()
  104. {
  105. return array(
  106. 'metavar:foo' => 'bar',
  107. 'metavar:hoge' => 'piyo',
  108. 'metavar:foofoo' => 'barbar',
  109. );
  110. }
  111. public static function getZSetArray()
  112. {
  113. return array('a' => -10, 'b' => 0, 'c' => 10, 'd' => 20, 'e' => 20, 'f' => 30);
  114. }
  115. public static function sameValuesInArrays($arrayA, $arrayB)
  116. {
  117. if (count($arrayA) != count($arrayB)) {
  118. return false;
  119. }
  120. return count(array_diff($arrayA, $arrayB)) == 0;
  121. }
  122. public static function testForServerException($testcaseInstance, $expectedMessage, $wrapFunction)
  123. {
  124. $thrownException = null;
  125. try {
  126. $wrapFunction($testcaseInstance);
  127. }
  128. catch (Predis\ServerException $exception) {
  129. $thrownException = $exception;
  130. }
  131. $testcaseInstance->assertInstanceOf('Predis\ServerException', $thrownException);
  132. if (isset($expectedMessage)) {
  133. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  134. }
  135. }
  136. public static function testForClientException($testcaseInstance, $expectedMessage, $wrapFunction)
  137. {
  138. $thrownException = null;
  139. try {
  140. $wrapFunction($testcaseInstance);
  141. }
  142. catch (Predis\ClientException $exception) {
  143. $thrownException = $exception;
  144. }
  145. $testcaseInstance->assertInstanceOf('Predis\ClientException', $thrownException);
  146. if (isset($expectedMessage)) {
  147. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  148. }
  149. }
  150. public static function testForCommunicationException($testcaseInstance, $expectedMessage, $wrapFunction)
  151. {
  152. $thrownException = null;
  153. try {
  154. $wrapFunction($testcaseInstance);
  155. }
  156. catch (Predis\CommunicationException $exception) {
  157. $thrownException = $exception;
  158. }
  159. $testcaseInstance->assertInstanceOf('Predis\CommunicationException', $thrownException);
  160. if (isset($expectedMessage)) {
  161. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  162. }
  163. }
  164. public static function testForAbortedMultiExecException($testcaseInstance, $wrapFunction)
  165. {
  166. $thrownException = null;
  167. try {
  168. $wrapFunction($testcaseInstance);
  169. }
  170. catch (Predis\Transaction\AbortedMultiExecException $exception) {
  171. $thrownException = $exception;
  172. }
  173. $testcaseInstance->assertInstanceOf('Predis\Transaction\AbortedMultiExecException', $thrownException);
  174. }
  175. public static function pushTailAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0)
  176. {
  177. if ($wipeOut == true) {
  178. $client->del($keyName);
  179. }
  180. foreach ($values as $value) {
  181. $client->rpush($keyName, $value);
  182. }
  183. return $values;
  184. }
  185. public static function setAddAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0)
  186. {
  187. if ($wipeOut == true) {
  188. $client->del($keyName);
  189. }
  190. foreach ($values as $value) {
  191. $client->sadd($keyName, $value);
  192. }
  193. return $values;
  194. }
  195. public static function zsetAddAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0)
  196. {
  197. // $values: array(SCORE => VALUE, ...);
  198. if ($wipeOut == true) {
  199. $client->del($keyName);
  200. }
  201. foreach ($values as $value => $score) {
  202. $client->zadd($keyName, $score, $value);
  203. }
  204. return $values;
  205. }
  206. public static function getConnectionParametersArgumentsArray()
  207. {
  208. return array(
  209. 'host' => '10.0.0.1',
  210. 'port' => 6380,
  211. 'connection_timeout' => 10,
  212. 'read_write_timeout' => 30,
  213. 'database' => 5,
  214. 'password' => 'dbpassword',
  215. 'alias' => 'connection_alias',
  216. );
  217. }
  218. public static function getConnectionParametersArgumentsString($arguments = null)
  219. {
  220. // TODO: must be improved
  221. $args = $arguments ?: RC::getConnectionParametersArgumentsArray();
  222. $paramsString = "tcp://{$args['host']}:{$args['port']}/?";
  223. unset($args['host']);
  224. unset($args['port']);
  225. foreach($args as $k => $v) {
  226. $paramsString .= "$k=$v&";
  227. }
  228. return $paramsString;
  229. }
  230. }