123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\HttpKernel\Tests\Profiler\Mock;
- /**
- * RedisMock for simulating Redis extension in tests.
- *
- * @author Andrej Hudec <pulzarraider@gmail.com>
- */
- class RedisMock
- {
- private $connected = false;
- private $storage = array();
- /**
- * Add a server to connection pool.
- *
- * @param string $host
- * @param int $port
- * @param float $timeout
- *
- * @return bool
- */
- public function connect($host, $port = 6379, $timeout = 0)
- {
- if ('127.0.0.1' == $host && 6379 == $port) {
- $this->connected = true;
- return true;
- }
- return false;
- }
- /**
- * Set client option.
- *
- * @param int $name
- * @param int $value
- *
- * @return bool
- */
- public function setOption($name, $value)
- {
- if (!$this->connected) {
- return false;
- }
- return true;
- }
- /**
- * Verify if the specified key exists.
- *
- * @param string $key
- *
- * @return bool
- */
- public function exists($key)
- {
- if (!$this->connected) {
- return false;
- }
- return isset($this->storage[$key]);
- }
- /**
- * Store data at the server with expiration time.
- *
- * @param string $key
- * @param int $ttl
- * @param mixed $value
- *
- * @return bool
- */
- public function setex($key, $ttl, $value)
- {
- if (!$this->connected) {
- return false;
- }
- $this->storeData($key, $value);
- return true;
- }
- /**
- * Sets an expiration time on an item.
- *
- * @param string $key
- * @param int $ttl
- *
- * @return bool
- */
- public function setTimeout($key, $ttl)
- {
- if (!$this->connected) {
- return false;
- }
- if (isset($this->storage[$key])) {
- return true;
- }
- return false;
- }
- /**
- * Retrieve item from the server.
- *
- * @param string $key
- *
- * @return bool
- */
- public function get($key)
- {
- if (!$this->connected) {
- return false;
- }
- return $this->getData($key);
- }
- /**
- * Append data to an existing item.
- *
- * @param string $key
- * @param string $value
- *
- * @return int Size of the value after the append
- */
- public function append($key, $value)
- {
- if (!$this->connected) {
- return false;
- }
- if (isset($this->storage[$key])) {
- $this->storeData($key, $this->getData($key).$value);
- return \strlen($this->storage[$key]);
- }
- return false;
- }
- /**
- * Remove specified keys.
- *
- * @param string|array $key
- *
- * @return int
- */
- public function delete($key)
- {
- if (!$this->connected) {
- return false;
- }
- if (\is_array($key)) {
- $result = 0;
- foreach ($key as $k) {
- if (isset($this->storage[$k])) {
- unset($this->storage[$k]);
- ++$result;
- }
- }
- return $result;
- }
- if (isset($this->storage[$key])) {
- unset($this->storage[$key]);
- return 1;
- }
- return 0;
- }
- /**
- * Flush all existing items from all databases at the server.
- *
- * @return bool
- */
- public function flushAll()
- {
- if (!$this->connected) {
- return false;
- }
- $this->storage = array();
- return true;
- }
- /**
- * Close Redis server connection.
- *
- * @return bool
- */
- public function close()
- {
- $this->connected = false;
- return true;
- }
- private function getData($key)
- {
- if (isset($this->storage[$key])) {
- return unserialize($this->storage[$key]);
- }
- return false;
- }
- private function storeData($key, $value)
- {
- $this->storage[$key] = serialize($value);
- return true;
- }
- public function select($dbnum)
- {
- if (!$this->connected) {
- return false;
- }
- if (0 > $dbnum) {
- return false;
- }
- return true;
- }
- }
|