PredisShared.php 4.7 KB

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