diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5ace6ac11a..37376e9b0d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -22,7 +22,6 @@ benchmark - memcached diff --git a/src/Symfony/Component/HttpKernel/Profiler/MemcacheProfilerStorage.php b/src/Symfony/Component/HttpKernel/Profiler/MemcacheProfilerStorage.php index ca79405cd7..ebf3b5c2ac 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/MemcacheProfilerStorage.php +++ b/src/Symfony/Component/HttpKernel/Profiler/MemcacheProfilerStorage.php @@ -50,6 +50,16 @@ class MemcacheProfilerStorage extends BaseMemcacheProfilerStorage return $this->memcache; } + /** + * Set instance of the Memcache + * + * @param Memcache $memcache + */ + public function setMemcache($memcache) + { + $this->memcache = $memcache; + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/HttpKernel/Profiler/MemcachedProfilerStorage.php b/src/Symfony/Component/HttpKernel/Profiler/MemcachedProfilerStorage.php index 6d76127299..34ca1cbcad 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/MemcachedProfilerStorage.php +++ b/src/Symfony/Component/HttpKernel/Profiler/MemcachedProfilerStorage.php @@ -54,6 +54,16 @@ class MemcachedProfilerStorage extends BaseMemcacheProfilerStorage return $this->memcached; } + /** + * Set instance of the Memcached + * + * @param Memcached $memcached + */ + public function setMemcached($memcached) + { + $this->memcached = $memcached; + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/HttpKernel/Profiler/RedisProfilerStorage.php b/src/Symfony/Component/HttpKernel/Profiler/RedisProfilerStorage.php index 67722e1c15..a8196b5dcb 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/RedisProfilerStorage.php +++ b/src/Symfony/Component/HttpKernel/Profiler/RedisProfilerStorage.php @@ -211,6 +211,16 @@ class RedisProfilerStorage implements ProfilerStorageInterface 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) { $profile = new Profile($token); diff --git a/src/Symfony/Component/HttpKernel/Tests/Profiler/MemcacheProfilerStorageTest.php b/src/Symfony/Component/HttpKernel/Tests/Profiler/MemcacheProfilerStorageTest.php index fb55026a9a..f582dff799 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Profiler/MemcacheProfilerStorageTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Profiler/MemcacheProfilerStorageTest.php @@ -12,48 +12,30 @@ namespace Symfony\Component\HttpKernel\Tests\Profiler; 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 { protected static $storage; - public static function tearDownAfterClass() + protected function setUp() { + $memcacheMock = new MemcacheMock(); + $memcacheMock->addServer('127.0.0.1', 11211); + + self::$storage = new MemcacheProfilerStorage('memcache://127.0.0.1:11211', '', '', 86400); + self::$storage->setMemcache($memcacheMock); + if (self::$storage) { self::$storage->purge(); } } - protected function setUp() + protected function tearDown() { - if (!extension_loaded('memcache')) { - $this->markTestSkipped('MemcacheProfilerStorageTest requires that the extension memcache is loaded'); - } - - self::$storage = new DummyMemcacheProfilerStorage('memcache://127.0.0.1:11211', '', '', 86400); - try { - 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) { self::$storage->purge(); + self::$storage = false; } } diff --git a/src/Symfony/Component/HttpKernel/Tests/Profiler/MemcachedProfilerStorageTest.php b/src/Symfony/Component/HttpKernel/Tests/Profiler/MemcachedProfilerStorageTest.php index 5f2f5c3522..565ac35f33 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Profiler/MemcachedProfilerStorageTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Profiler/MemcachedProfilerStorageTest.php @@ -12,44 +12,30 @@ namespace Symfony\Component\HttpKernel\Tests\Profiler; 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 { protected static $storage; - public static function tearDownAfterClass() + protected function setUp() { + $memcachedMock = new MemcachedMock(); + $memcachedMock->addServer('127.0.0.1', 11211); + + self::$storage = new MemcachedProfilerStorage('memcached://127.0.0.1:11211', '', '', 86400); + self::$storage->setMemcached($memcachedMock); + if (self::$storage) { self::$storage->purge(); } } - protected function setUp() + protected function tearDown() { - if (!extension_loaded('memcached')) { - $this->markTestSkipped('MemcachedProfilerStorageTest requires that the extension memcached is loaded'); - } - - self::$storage = new DummyMemcachedProfilerStorage('memcached://127.0.0.1:11211', '', '', 86400); - try { - self::$storage->getMemcached(); - } catch (\Exception $e) { - $this->markTestSkipped('MemcachedProfilerStorageTest requires that there is a Memcache server present on localhost'); - } - if (self::$storage) { self::$storage->purge(); + self::$storage = false; } } diff --git a/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/MemcacheMock.php b/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/MemcacheMock.php new file mode 100644 index 0000000000..2673042b99 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/MemcacheMock.php @@ -0,0 +1,242 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/MemcachedMock.php b/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/MemcachedMock.php new file mode 100644 index 0000000000..995258d5e9 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/MemcachedMock.php @@ -0,0 +1,211 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/RedisMock.php b/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/RedisMock.php new file mode 100644 index 0000000000..dca419b03d --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/RedisMock.php @@ -0,0 +1,228 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Component/HttpKernel/Tests/Profiler/RedisProfilerStorageTest.php b/src/Symfony/Component/HttpKernel/Tests/Profiler/RedisProfilerStorageTest.php index 88779bc718..91354ae935 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Profiler/RedisProfilerStorageTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Profiler/RedisProfilerStorageTest.php @@ -12,14 +12,7 @@ namespace Symfony\Component\HttpKernel\Tests\Profiler; use Symfony\Component\HttpKernel\Profiler\RedisProfilerStorage; - -class DummyRedisProfilerStorage extends RedisProfilerStorage -{ - public function getRedis() - { - return parent::getRedis(); - } -} +use Symfony\Component\HttpKernel\Tests\Profiler\Mock\RedisMock; class RedisProfilerStorageTest extends AbstractProfilerStorageTest { @@ -27,19 +20,14 @@ class RedisProfilerStorageTest extends AbstractProfilerStorageTest protected function setUp() { - if (!extension_loaded('redis')) { - $this->markTestSkipped('RedisProfilerStorageTest requires redis extension to be loaded'); - } + $redisMock = new RedisMock(); + $redisMock->connect('127.0.0.1', 6379); - self::$storage = new DummyRedisProfilerStorage('redis://127.0.0.1:6379', '', '', 86400); - try { - self::$storage->getRedis(); + self::$storage = new RedisProfilerStorage('redis://127.0.0.1:6379', '', '', 86400); + self::$storage->setRedis($redisMock); + if (self::$storage) { 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) { self::$storage->purge(); - self::$storage->getRedis()->close(); self::$storage = false; } }