PredisShared.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. private static $_connection;
  21. private static function createConnection() {
  22. $serverProfile = new Predis\RedisServer__Futures();
  23. $connection = new Predis\Client(array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT), $serverProfile);
  24. $connection->connect();
  25. $connection->selectDatabase(RC::DEFAULT_DATABASE);
  26. return $connection;
  27. }
  28. public static function getConnection() {
  29. if (self::$_connection === null || !self::$_connection->isConnected()) {
  30. self::$_connection = self::createConnection();
  31. }
  32. return self::$_connection;
  33. }
  34. public static function resetConnection() {
  35. if (self::$_connection !== null && self::$_connection->isConnected()) {
  36. self::$_connection->disconnect();
  37. self::$_connection = self::createConnection();
  38. }
  39. }
  40. public static function helperForBlockingPops($op) {
  41. // TODO: I admit that this helper is kinda lame and it does not run
  42. // in a separate process to properly test BLPOP/BRPOP
  43. $redisUri = sprintf('redis://%s:%d/?database=%d', RC::SERVER_HOST, RC::SERVER_PORT, RC::DEFAULT_DATABASE);
  44. $handle = popen('php', 'w');
  45. fwrite($handle, "<?php
  46. require '../lib/Predis.php';
  47. \$redis = Predis\Client::create('$redisUri');
  48. \$redis->rpush('{$op}1', 'a');
  49. \$redis->rpush('{$op}2', 'b');
  50. \$redis->rpush('{$op}3', 'c');
  51. \$redis->rpush('{$op}1', 'd');
  52. ?>");
  53. pclose($handle);
  54. }
  55. public static function getArrayOfNumbers() {
  56. return array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  57. }
  58. public static function getKeyValueArray() {
  59. return array(
  60. 'foo' => 'bar',
  61. 'hoge' => 'piyo',
  62. 'foofoo' => 'barbar',
  63. );
  64. }
  65. public static function getNamespacedKeyValueArray() {
  66. return array(
  67. 'metavar:foo' => 'bar',
  68. 'metavar:hoge' => 'piyo',
  69. 'metavar:foofoo' => 'barbar',
  70. );
  71. }
  72. public static function getZSetArray() {
  73. return array(
  74. 'a' => -10, 'b' => 0, 'c' => 10, 'd' => 20, 'e' => 20, 'f' => 30
  75. );
  76. }
  77. public static function sameValuesInArrays($arrayA, $arrayB) {
  78. if (count($arrayA) != count($arrayB)) {
  79. return false;
  80. }
  81. return count(array_diff($arrayA, $arrayB)) == 0;
  82. }
  83. public static function testForServerException($testcaseInstance, $expectedMessage, $wrapFunction) {
  84. $thrownException = null;
  85. try {
  86. $wrapFunction($testcaseInstance);
  87. }
  88. catch (Predis\ServerException $exception) {
  89. $thrownException = $exception;
  90. }
  91. $testcaseInstance->assertType('Predis\ServerException', $thrownException);
  92. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  93. }
  94. public static function pushTailAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) {
  95. if ($wipeOut == true) {
  96. $client->delete($keyName);
  97. }
  98. foreach ($values as $value) {
  99. $client->pushTail($keyName, $value);
  100. }
  101. return $values;
  102. }
  103. public static function setAddAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) {
  104. if ($wipeOut == true) {
  105. $client->delete($keyName);
  106. }
  107. foreach ($values as $value) {
  108. $client->setAdd($keyName, $value);
  109. }
  110. return $values;
  111. }
  112. public static function zsetAddAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) {
  113. // $values: array(SCORE => VALUE, ...);
  114. if ($wipeOut == true) {
  115. $client->delete($keyName);
  116. }
  117. foreach ($values as $value => $score) {
  118. $client->zsetAdd($keyName, $score, $value);
  119. }
  120. return $values;
  121. }
  122. }
  123. ?>