[HttpKernel] Added mock objects for Memcache(d) and Redis
This commit is contained in:
parent
e17217b14a
commit
1f6c8d5c7e
|
@ -22,7 +22,6 @@
|
||||||
<groups>
|
<groups>
|
||||||
<exclude>
|
<exclude>
|
||||||
<group>benchmark</group>
|
<group>benchmark</group>
|
||||||
<group>memcached</group>
|
|
||||||
</exclude>
|
</exclude>
|
||||||
</groups>
|
</groups>
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,16 @@ class MemcacheProfilerStorage extends BaseMemcacheProfilerStorage
|
||||||
return $this->memcache;
|
return $this->memcache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set instance of the Memcache
|
||||||
|
*
|
||||||
|
* @param Memcache $memcache
|
||||||
|
*/
|
||||||
|
public function setMemcache($memcache)
|
||||||
|
{
|
||||||
|
$this->memcache = $memcache;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -54,6 +54,16 @@ class MemcachedProfilerStorage extends BaseMemcacheProfilerStorage
|
||||||
return $this->memcached;
|
return $this->memcached;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set instance of the Memcached
|
||||||
|
*
|
||||||
|
* @param Memcached $memcached
|
||||||
|
*/
|
||||||
|
public function setMemcached($memcached)
|
||||||
|
{
|
||||||
|
$this->memcached = $memcached;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -211,6 +211,16 @@ class RedisProfilerStorage implements ProfilerStorageInterface
|
||||||
return $this->redis;
|
return $this->redis;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set instance of the Redis
|
||||||
|
*
|
||||||
|
* @param Redis $redis
|
||||||
|
*/
|
||||||
|
public function setRedis($redis)
|
||||||
|
{
|
||||||
|
$this->redis = $redis;
|
||||||
|
}
|
||||||
|
|
||||||
private function createProfileFromData($token, $data, $parent = null)
|
private function createProfileFromData($token, $data, $parent = null)
|
||||||
{
|
{
|
||||||
$profile = new Profile($token);
|
$profile = new Profile($token);
|
||||||
|
|
|
@ -12,51 +12,33 @@
|
||||||
namespace Symfony\Component\HttpKernel\Tests\Profiler;
|
namespace Symfony\Component\HttpKernel\Tests\Profiler;
|
||||||
|
|
||||||
use Symfony\Component\HttpKernel\Profiler\MemcacheProfilerStorage;
|
use Symfony\Component\HttpKernel\Profiler\MemcacheProfilerStorage;
|
||||||
|
use Symfony\Component\HttpKernel\Tests\Profiler\Mock\MemcacheMock;
|
||||||
|
|
||||||
class DummyMemcacheProfilerStorage extends MemcacheProfilerStorage
|
|
||||||
{
|
|
||||||
public function getMemcache()
|
|
||||||
{
|
|
||||||
return parent::getMemcache();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @group memcached
|
|
||||||
*/
|
|
||||||
class MemcacheProfilerStorageTest extends AbstractProfilerStorageTest
|
class MemcacheProfilerStorageTest extends AbstractProfilerStorageTest
|
||||||
{
|
{
|
||||||
protected static $storage;
|
protected static $storage;
|
||||||
|
|
||||||
public static function tearDownAfterClass()
|
|
||||||
{
|
|
||||||
if (self::$storage) {
|
|
||||||
self::$storage->purge();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
if (!extension_loaded('memcache')) {
|
$memcacheMock = new MemcacheMock();
|
||||||
$this->markTestSkipped('MemcacheProfilerStorageTest requires that the extension memcache is loaded');
|
$memcacheMock->addServer('127.0.0.1', 11211);
|
||||||
}
|
|
||||||
|
|
||||||
self::$storage = new DummyMemcacheProfilerStorage('memcache://127.0.0.1:11211', '', '', 86400);
|
self::$storage = new MemcacheProfilerStorage('memcache://127.0.0.1:11211', '', '', 86400);
|
||||||
try {
|
self::$storage->setMemcache($memcacheMock);
|
||||||
self::$storage->getMemcache();
|
|
||||||
$stats = self::$storage->getMemcache()->getExtendedStats();
|
|
||||||
if (!isset($stats['127.0.0.1:11211']) || $stats['127.0.0.1:11211'] === false) {
|
|
||||||
throw new \Exception();
|
|
||||||
}
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$this->markTestSkipped('MemcacheProfilerStorageTest requires that there is a Memcache server present on localhost');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (self::$storage) {
|
if (self::$storage) {
|
||||||
self::$storage->purge();
|
self::$storage->purge();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function tearDown()
|
||||||
|
{
|
||||||
|
if (self::$storage) {
|
||||||
|
self::$storage->purge();
|
||||||
|
self::$storage = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
|
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -12,47 +12,33 @@
|
||||||
namespace Symfony\Component\HttpKernel\Tests\Profiler;
|
namespace Symfony\Component\HttpKernel\Tests\Profiler;
|
||||||
|
|
||||||
use Symfony\Component\HttpKernel\Profiler\MemcachedProfilerStorage;
|
use Symfony\Component\HttpKernel\Profiler\MemcachedProfilerStorage;
|
||||||
|
use Symfony\Component\HttpKernel\Tests\Profiler\Mock\MemcachedMock;
|
||||||
|
|
||||||
class DummyMemcachedProfilerStorage extends MemcachedProfilerStorage
|
|
||||||
{
|
|
||||||
public function getMemcached()
|
|
||||||
{
|
|
||||||
return parent::getMemcached();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @group memcached
|
|
||||||
*/
|
|
||||||
class MemcachedProfilerStorageTest extends AbstractProfilerStorageTest
|
class MemcachedProfilerStorageTest extends AbstractProfilerStorageTest
|
||||||
{
|
{
|
||||||
protected static $storage;
|
protected static $storage;
|
||||||
|
|
||||||
public static function tearDownAfterClass()
|
|
||||||
{
|
|
||||||
if (self::$storage) {
|
|
||||||
self::$storage->purge();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
if (!extension_loaded('memcached')) {
|
$memcachedMock = new MemcachedMock();
|
||||||
$this->markTestSkipped('MemcachedProfilerStorageTest requires that the extension memcached is loaded');
|
$memcachedMock->addServer('127.0.0.1', 11211);
|
||||||
}
|
|
||||||
|
|
||||||
self::$storage = new DummyMemcachedProfilerStorage('memcached://127.0.0.1:11211', '', '', 86400);
|
self::$storage = new MemcachedProfilerStorage('memcached://127.0.0.1:11211', '', '', 86400);
|
||||||
try {
|
self::$storage->setMemcached($memcachedMock);
|
||||||
self::$storage->getMemcached();
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$this->markTestSkipped('MemcachedProfilerStorageTest requires that there is a Memcache server present on localhost');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (self::$storage) {
|
if (self::$storage) {
|
||||||
self::$storage->purge();
|
self::$storage->purge();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function tearDown()
|
||||||
|
{
|
||||||
|
if (self::$storage) {
|
||||||
|
self::$storage->purge();
|
||||||
|
self::$storage = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
|
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,242 @@
|
||||||
|
<?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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MemcacheMock for simulating Memcache extension in tests.
|
||||||
|
*
|
||||||
|
* @author Andrej Hudec <pulzarraider@gmail.com>
|
||||||
|
*/
|
||||||
|
class MemcacheMock
|
||||||
|
{
|
||||||
|
private $connected;
|
||||||
|
private $storage;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->connected = false;
|
||||||
|
$this->storage = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open memcached server connection
|
||||||
|
*
|
||||||
|
* @param string $host
|
||||||
|
* @param integer $port
|
||||||
|
* @param integer $timeout
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function connect($host, $port = null, $timeout = null)
|
||||||
|
{
|
||||||
|
if ('127.0.0.1' == $host && 11211 == $port) {
|
||||||
|
$this->connected = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open memcached server persistent connection
|
||||||
|
*
|
||||||
|
* @param string $host
|
||||||
|
* @param integer $port
|
||||||
|
* @param integer $timeout
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function pconnect($host, $port = null, $timeout = null)
|
||||||
|
{
|
||||||
|
if ('127.0.0.1' == $host && 11211 == $port) {
|
||||||
|
$this->connected = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a memcached server to connection pool
|
||||||
|
*
|
||||||
|
* @param string $host
|
||||||
|
* @param integer $port
|
||||||
|
* @param boolean $persistent
|
||||||
|
* @param integer $weight
|
||||||
|
* @param integer $timeout
|
||||||
|
* @param integer $retry_interval
|
||||||
|
* @param boolean $status
|
||||||
|
* @param callable $failure_callback
|
||||||
|
* @param integer $timeoutms
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function addServer($host, $port = 11211, $persistent = null, $weight = null, $timeout = null, $retry_interval = null, $status = null, $failure_callback = null, $timeoutms = null)
|
||||||
|
{
|
||||||
|
if ('127.0.0.1' == $host && 11211 == $port) {
|
||||||
|
$this->connected = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add an item to the server only if such key doesn't exist at the server yet.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $var
|
||||||
|
* @param integer $flag
|
||||||
|
* @param integer $expire
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function add($key, $var, $flag = null, $expire = null)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($this->storage[$key])) {
|
||||||
|
$this->storeData($key, $var);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store data at the server.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param string $var
|
||||||
|
* @param integer $flag
|
||||||
|
* @param integer $expire
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function set($key, $var, $flag = null, $expire = null)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->storeData($key, $var);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace value of the existing item.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $var
|
||||||
|
* @param integer $flag
|
||||||
|
* @param integer $expire
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function replace($key, $var, $flag = null, $expire = null)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->storage[$key])) {
|
||||||
|
$this->storeData($key, $var);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve item from the server.
|
||||||
|
*
|
||||||
|
* @param string|array $key
|
||||||
|
* @param integer|array $flags
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function get($key, &$flags = null)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($key)) {
|
||||||
|
$result = array();
|
||||||
|
foreach ($key as $k) {
|
||||||
|
if (isset($this->storage[$k])) {
|
||||||
|
$result[] = $this->getData($k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->getData($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete item from the server
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function delete($key)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->storage[$key])) {
|
||||||
|
unset($this->storage[$key]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flush all existing items at the server
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function flush()
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->storage = array();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close memcached server connection
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,211 @@
|
||||||
|
<?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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MemcachedMock for simulating Memcached extension in tests.
|
||||||
|
*
|
||||||
|
* @author Andrej Hudec <pulzarraider@gmail.com>
|
||||||
|
*/
|
||||||
|
class MemcachedMock
|
||||||
|
{
|
||||||
|
private $connected;
|
||||||
|
private $storage;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->connected = false;
|
||||||
|
$this->storage = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a Memcached option
|
||||||
|
*
|
||||||
|
* @param integer $option
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function setOption($option, $value)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a memcached server to connection pool
|
||||||
|
*
|
||||||
|
* @param string $host
|
||||||
|
* @param integer $port
|
||||||
|
* @param integer $weight
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function addServer($host, $port = 11211, $weight = 0)
|
||||||
|
{
|
||||||
|
if ('127.0.0.1' == $host && 11211 == $port) {
|
||||||
|
$this->connected = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add an item to the server only if such key doesn't exist at the server yet.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $value
|
||||||
|
* @param integer $expiration
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function add($key, $value, $expiration = 0)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($this->storage[$key])) {
|
||||||
|
$this->storeData($key, $value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store data at the server.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $value
|
||||||
|
* @param integer $expiration
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function set($key, $value, $expiration = null)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->storeData($key, $value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace value of the existing item.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $value
|
||||||
|
* @param integer $expiration
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function replace($key, $value, $expiration = null)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->storage[$key])) {
|
||||||
|
$this->storeData($key, $value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve item from the server.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param callable $cache_cb
|
||||||
|
* @param float $cas_token
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function get($key, $cache_cb = null, &$cas_token = null)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->getData($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append data to an existing item
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param string $value
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function append($key, $value)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->storage[$key])) {
|
||||||
|
$this->storeData($key, $this->getData($key).$value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete item from the server
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function delete($key)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->storage[$key])) {
|
||||||
|
unset($this->storage[$key]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flush all existing items at the server
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function flush()
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->storage = array();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,228 @@
|
||||||
|
<?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;
|
||||||
|
private $storage;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->connected = false;
|
||||||
|
$this->storage = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a memcached server to connection pool
|
||||||
|
*
|
||||||
|
* @param string $host
|
||||||
|
* @param integer $port
|
||||||
|
* @param float $timeout
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
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 integer $name
|
||||||
|
* @param integer $value
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function setOption($name, $value)
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify if the specified key exists.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
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 integer $ttl
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
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 integer $ttl
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
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 boolean
|
||||||
|
*/
|
||||||
|
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 integer 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 integer
|
||||||
|
*/
|
||||||
|
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 boolean
|
||||||
|
*/
|
||||||
|
public function flushAll()
|
||||||
|
{
|
||||||
|
if (!$this->connected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->storage = array();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close Redis server connection
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,14 +12,7 @@
|
||||||
namespace Symfony\Component\HttpKernel\Tests\Profiler;
|
namespace Symfony\Component\HttpKernel\Tests\Profiler;
|
||||||
|
|
||||||
use Symfony\Component\HttpKernel\Profiler\RedisProfilerStorage;
|
use Symfony\Component\HttpKernel\Profiler\RedisProfilerStorage;
|
||||||
|
use Symfony\Component\HttpKernel\Tests\Profiler\Mock\RedisMock;
|
||||||
class DummyRedisProfilerStorage extends RedisProfilerStorage
|
|
||||||
{
|
|
||||||
public function getRedis()
|
|
||||||
{
|
|
||||||
return parent::getRedis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class RedisProfilerStorageTest extends AbstractProfilerStorageTest
|
class RedisProfilerStorageTest extends AbstractProfilerStorageTest
|
||||||
{
|
{
|
||||||
|
@ -27,19 +20,14 @@ class RedisProfilerStorageTest extends AbstractProfilerStorageTest
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
if (!extension_loaded('redis')) {
|
$redisMock = new RedisMock();
|
||||||
$this->markTestSkipped('RedisProfilerStorageTest requires redis extension to be loaded');
|
$redisMock->connect('127.0.0.1', 6379);
|
||||||
}
|
|
||||||
|
|
||||||
self::$storage = new DummyRedisProfilerStorage('redis://127.0.0.1:6379', '', '', 86400);
|
self::$storage = new RedisProfilerStorage('redis://127.0.0.1:6379', '', '', 86400);
|
||||||
try {
|
self::$storage->setRedis($redisMock);
|
||||||
self::$storage->getRedis();
|
|
||||||
|
|
||||||
|
if (self::$storage) {
|
||||||
self::$storage->purge();
|
self::$storage->purge();
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
self::$storage = false;
|
|
||||||
$this->markTestSkipped('RedisProfilerStorageTest requires that there is a Redis server present on localhost');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +35,6 @@ class RedisProfilerStorageTest extends AbstractProfilerStorageTest
|
||||||
{
|
{
|
||||||
if (self::$storage) {
|
if (self::$storage) {
|
||||||
self::$storage->purge();
|
self::$storage->purge();
|
||||||
self::$storage->getRedis()->close();
|
|
||||||
self::$storage = false;
|
self::$storage = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue