/integration-tests/performance-test-engine/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/RedisMock.php

https://github.com/societies/SOCIETIES-Platform · PHP · 241 lines · 116 code · 39 blank · 86 comment · 18 complexity · e362582df430ba561cc6ee51b1f7e6b4 MD5 · raw file

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