RedisMock.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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. namespace Symfony\Component\HttpKernel\Tests\Profiler\Mock;
  11. /**
  12. * RedisMock for simulating Redis extension in tests.
  13. *
  14. * @author Andrej Hudec <pulzarraider@gmail.com>
  15. */
  16. class RedisMock
  17. {
  18. private $connected = false;
  19. private $storage = array();
  20. /**
  21. * Add a server to connection pool.
  22. *
  23. * @param string $host
  24. * @param int $port
  25. * @param float $timeout
  26. *
  27. * @return bool
  28. */
  29. public function connect($host, $port = 6379, $timeout = 0)
  30. {
  31. if ('127.0.0.1' == $host && 6379 == $port) {
  32. $this->connected = true;
  33. return true;
  34. }
  35. return false;
  36. }
  37. /**
  38. * Set client option.
  39. *
  40. * @param int $name
  41. * @param int $value
  42. *
  43. * @return bool
  44. */
  45. public function setOption($name, $value)
  46. {
  47. if (!$this->connected) {
  48. return false;
  49. }
  50. return true;
  51. }
  52. /**
  53. * Verify if the specified key exists.
  54. *
  55. * @param string $key
  56. *
  57. * @return bool
  58. */
  59. public function exists($key)
  60. {
  61. if (!$this->connected) {
  62. return false;
  63. }
  64. return isset($this->storage[$key]);
  65. }
  66. /**
  67. * Store data at the server with expiration time.
  68. *
  69. * @param string $key
  70. * @param int $ttl
  71. * @param mixed $value
  72. *
  73. * @return bool
  74. */
  75. public function setex($key, $ttl, $value)
  76. {
  77. if (!$this->connected) {
  78. return false;
  79. }
  80. $this->storeData($key, $value);
  81. return true;
  82. }
  83. /**
  84. * Sets an expiration time on an item.
  85. *
  86. * @param string $key
  87. * @param int $ttl
  88. *
  89. * @return bool
  90. */
  91. public function setTimeout($key, $ttl)
  92. {
  93. if (!$this->connected) {
  94. return false;
  95. }
  96. if (isset($this->storage[$key])) {
  97. return true;
  98. }
  99. return false;
  100. }
  101. /**
  102. * Retrieve item from the server.
  103. *
  104. * @param string $key
  105. *
  106. * @return bool
  107. */
  108. public function get($key)
  109. {
  110. if (!$this->connected) {
  111. return false;
  112. }
  113. return $this->getData($key);
  114. }
  115. /**
  116. * Append data to an existing item.
  117. *
  118. * @param string $key
  119. * @param string $value
  120. *
  121. * @return int Size of the value after the append
  122. */
  123. public function append($key, $value)
  124. {
  125. if (!$this->connected) {
  126. return false;
  127. }
  128. if (isset($this->storage[$key])) {
  129. $this->storeData($key, $this->getData($key).$value);
  130. return \strlen($this->storage[$key]);
  131. }
  132. return false;
  133. }
  134. /**
  135. * Remove specified keys.
  136. *
  137. * @param string|array $key
  138. *
  139. * @return int
  140. */
  141. public function delete($key)
  142. {
  143. if (!$this->connected) {
  144. return false;
  145. }
  146. if (\is_array($key)) {
  147. $result = 0;
  148. foreach ($key as $k) {
  149. if (isset($this->storage[$k])) {
  150. unset($this->storage[$k]);
  151. ++$result;
  152. }
  153. }
  154. return $result;
  155. }
  156. if (isset($this->storage[$key])) {
  157. unset($this->storage[$key]);
  158. return 1;
  159. }
  160. return 0;
  161. }
  162. /**
  163. * Flush all existing items from all databases at the server.
  164. *
  165. * @return bool
  166. */
  167. public function flushAll()
  168. {
  169. if (!$this->connected) {
  170. return false;
  171. }
  172. $this->storage = array();
  173. return true;
  174. }
  175. /**
  176. * Close Redis server connection.
  177. *
  178. * @return bool
  179. */
  180. public function close()
  181. {
  182. $this->connected = false;
  183. return true;
  184. }
  185. private function getData($key)
  186. {
  187. if (isset($this->storage[$key])) {
  188. return unserialize($this->storage[$key]);
  189. }
  190. return false;
  191. }
  192. private function storeData($key, $value)
  193. {
  194. $this->storage[$key] = serialize($value);
  195. return true;
  196. }
  197. public function select($dbnum)
  198. {
  199. if (!$this->connected) {
  200. return false;
  201. }
  202. if (0 > $dbnum) {
  203. return false;
  204. }
  205. return true;
  206. }
  207. }