Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663

This commit is contained in:
Greg Anderson 2015-10-08 11:40:12 -07:00
parent eb34d130a8
commit f32e58e4b1
8476 changed files with 211648 additions and 170042 deletions

View file

@ -0,0 +1,270 @@
<?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;
use Symfony\Component\HttpKernel\Profiler\Profile;
abstract class AbstractProfilerStorageTest extends \PHPUnit_Framework_TestCase
{
public function testStore()
{
for ($i = 0; $i < 10; ++$i) {
$profile = new Profile('token_'.$i);
$profile->setIp('127.0.0.1');
$profile->setUrl('http://foo.bar');
$profile->setMethod('GET');
$this->getStorage()->write($profile);
}
$this->assertCount(10, $this->getStorage()->find('127.0.0.1', 'http://foo.bar', 20, 'GET'), '->write() stores data in the storage');
}
public function testChildren()
{
$parentProfile = new Profile('token_parent');
$parentProfile->setIp('127.0.0.1');
$parentProfile->setUrl('http://foo.bar/parent');
$childProfile = new Profile('token_child');
$childProfile->setIp('127.0.0.1');
$childProfile->setUrl('http://foo.bar/child');
$parentProfile->addChild($childProfile);
$this->getStorage()->write($parentProfile);
$this->getStorage()->write($childProfile);
// Load them from storage
$parentProfile = $this->getStorage()->read('token_parent');
$childProfile = $this->getStorage()->read('token_child');
// Check child has link to parent
$this->assertNotNull($childProfile->getParent());
$this->assertEquals($parentProfile->getToken(), $childProfile->getParentToken());
// Check parent has child
$children = $parentProfile->getChildren();
$this->assertCount(1, $children);
$this->assertEquals($childProfile->getToken(), $children[0]->getToken());
}
public function testStoreSpecialCharsInUrl()
{
// The storage accepts special characters in URLs (Even though URLs are not
// supposed to contain them)
$profile = new Profile('simple_quote');
$profile->setUrl('http://foo.bar/\'');
$this->getStorage()->write($profile);
$this->assertTrue(false !== $this->getStorage()->read('simple_quote'), '->write() accepts single quotes in URL');
$profile = new Profile('double_quote');
$profile->setUrl('http://foo.bar/"');
$this->getStorage()->write($profile);
$this->assertTrue(false !== $this->getStorage()->read('double_quote'), '->write() accepts double quotes in URL');
$profile = new Profile('backslash');
$profile->setUrl('http://foo.bar/\\');
$this->getStorage()->write($profile);
$this->assertTrue(false !== $this->getStorage()->read('backslash'), '->write() accepts backslash in URL');
$profile = new Profile('comma');
$profile->setUrl('http://foo.bar/,');
$this->getStorage()->write($profile);
$this->assertTrue(false !== $this->getStorage()->read('comma'), '->write() accepts comma in URL');
}
public function testStoreDuplicateToken()
{
$profile = new Profile('token');
$profile->setUrl('http://example.com/');
$this->assertTrue($this->getStorage()->write($profile), '->write() returns true when the token is unique');
$profile->setUrl('http://example.net/');
$this->assertTrue($this->getStorage()->write($profile), '->write() returns true when the token is already present in the storage');
$this->assertEquals('http://example.net/', $this->getStorage()->read('token')->getUrl(), '->write() overwrites the current profile data');
$this->assertCount(1, $this->getStorage()->find('', '', 1000, ''), '->find() does not return the same profile twice');
}
public function testRetrieveByIp()
{
$profile = new Profile('token');
$profile->setIp('127.0.0.1');
$profile->setMethod('GET');
$this->getStorage()->write($profile);
$this->assertCount(1, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'), '->find() retrieve a record by IP');
$this->assertCount(0, $this->getStorage()->find('127.0.%.1', '', 10, 'GET'), '->find() does not interpret a "%" as a wildcard in the IP');
$this->assertCount(0, $this->getStorage()->find('127.0._.1', '', 10, 'GET'), '->find() does not interpret a "_" as a wildcard in the IP');
}
public function testRetrieveByUrl()
{
$profile = new Profile('simple_quote');
$profile->setIp('127.0.0.1');
$profile->setUrl('http://foo.bar/\'');
$profile->setMethod('GET');
$this->getStorage()->write($profile);
$profile = new Profile('double_quote');
$profile->setIp('127.0.0.1');
$profile->setUrl('http://foo.bar/"');
$profile->setMethod('GET');
$this->getStorage()->write($profile);
$profile = new Profile('backslash');
$profile->setIp('127.0.0.1');
$profile->setUrl('http://foo\\bar/');
$profile->setMethod('GET');
$this->getStorage()->write($profile);
$profile = new Profile('percent');
$profile->setIp('127.0.0.1');
$profile->setUrl('http://foo.bar/%');
$profile->setMethod('GET');
$this->getStorage()->write($profile);
$profile = new Profile('underscore');
$profile->setIp('127.0.0.1');
$profile->setUrl('http://foo.bar/_');
$profile->setMethod('GET');
$this->getStorage()->write($profile);
$profile = new Profile('semicolon');
$profile->setIp('127.0.0.1');
$profile->setUrl('http://foo.bar/;');
$profile->setMethod('GET');
$this->getStorage()->write($profile);
$this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/\'', 10, 'GET'), '->find() accepts single quotes in URLs');
$this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/"', 10, 'GET'), '->find() accepts double quotes in URLs');
$this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo\\bar/', 10, 'GET'), '->find() accepts backslash in URLs');
$this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/;', 10, 'GET'), '->find() accepts semicolon in URLs');
$this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/%', 10, 'GET'), '->find() does not interpret a "%" as a wildcard in the URL');
$this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/_', 10, 'GET'), '->find() does not interpret a "_" as a wildcard in the URL');
}
public function testStoreTime()
{
$dt = new \DateTime('now');
$start = $dt->getTimestamp();
for ($i = 0; $i < 3; ++$i) {
$dt->modify('+1 minute');
$profile = new Profile('time_'.$i);
$profile->setIp('127.0.0.1');
$profile->setUrl('http://foo.bar');
$profile->setTime($dt->getTimestamp());
$profile->setMethod('GET');
$this->getStorage()->write($profile);
}
$records = $this->getStorage()->find('', '', 3, 'GET', $start, time() + 3 * 60);
$this->assertCount(3, $records, '->find() returns all previously added records');
$this->assertEquals($records[0]['token'], 'time_2', '->find() returns records ordered by time in descendant order');
$this->assertEquals($records[1]['token'], 'time_1', '->find() returns records ordered by time in descendant order');
$this->assertEquals($records[2]['token'], 'time_0', '->find() returns records ordered by time in descendant order');
$records = $this->getStorage()->find('', '', 3, 'GET', $start, time() + 2 * 60);
$this->assertCount(2, $records, '->find() should return only first two of the previously added records');
}
public function testRetrieveByEmptyUrlAndIp()
{
for ($i = 0; $i < 5; ++$i) {
$profile = new Profile('token_'.$i);
$profile->setMethod('GET');
$this->getStorage()->write($profile);
}
$this->assertCount(5, $this->getStorage()->find('', '', 10, 'GET'), '->find() returns all previously added records');
$this->getStorage()->purge();
}
public function testRetrieveByMethodAndLimit()
{
foreach (array('POST', 'GET') as $method) {
for ($i = 0; $i < 5; ++$i) {
$profile = new Profile('token_'.$i.$method);
$profile->setMethod($method);
$this->getStorage()->write($profile);
}
}
$this->assertCount(5, $this->getStorage()->find('', '', 5, 'POST'));
$this->getStorage()->purge();
}
public function testPurge()
{
$profile = new Profile('token1');
$profile->setIp('127.0.0.1');
$profile->setUrl('http://example.com/');
$profile->setMethod('GET');
$this->getStorage()->write($profile);
$this->assertTrue(false !== $this->getStorage()->read('token1'));
$this->assertCount(1, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'));
$profile = new Profile('token2');
$profile->setIp('127.0.0.1');
$profile->setUrl('http://example.net/');
$profile->setMethod('GET');
$this->getStorage()->write($profile);
$this->assertTrue(false !== $this->getStorage()->read('token2'));
$this->assertCount(2, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'));
$this->getStorage()->purge();
$this->assertEmpty($this->getStorage()->read('token'), '->purge() removes all data stored by profiler');
$this->assertCount(0, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'), '->purge() removes all items from index');
}
public function testDuplicates()
{
for ($i = 1; $i <= 5; ++$i) {
$profile = new Profile('foo'.$i);
$profile->setIp('127.0.0.1');
$profile->setUrl('http://example.net/');
$profile->setMethod('GET');
///three duplicates
$this->getStorage()->write($profile);
$this->getStorage()->write($profile);
$this->getStorage()->write($profile);
}
$this->assertCount(3, $this->getStorage()->find('127.0.0.1', 'http://example.net/', 3, 'GET'), '->find() method returns incorrect number of entries');
}
public function testStatusCode()
{
$profile = new Profile('token1');
$profile->setStatusCode(200);
$this->getStorage()->write($profile);
$profile = new Profile('token2');
$profile->setStatusCode(404);
$this->getStorage()->write($profile);
$tokens = $this->getStorage()->find('', '', 10, '');
$this->assertCount(2, $tokens);
$this->assertContains($tokens[0]['status_code'], array(200, 404));
$this->assertContains($tokens[1]['status_code'], array(200, 404));
}
/**
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
*/
abstract protected function getStorage();
}

View file

@ -0,0 +1,100 @@
<?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;
use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
use Symfony\Component\HttpKernel\Profiler\Profile;
class FileProfilerStorageTest extends AbstractProfilerStorageTest
{
protected static $tmpDir;
protected static $storage;
protected static function cleanDir()
{
$flags = \FilesystemIterator::SKIP_DOTS;
$iterator = new \RecursiveDirectoryIterator(self::$tmpDir, $flags);
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
if (is_file($file)) {
unlink($file);
}
}
}
public static function setUpBeforeClass()
{
self::$tmpDir = sys_get_temp_dir().'/sf2_profiler_file_storage';
if (is_dir(self::$tmpDir)) {
self::cleanDir();
}
self::$storage = new FileProfilerStorage('file:'.self::$tmpDir);
}
public static function tearDownAfterClass()
{
self::cleanDir();
}
protected function setUp()
{
self::$storage->purge();
}
/**
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
*/
protected function getStorage()
{
return self::$storage;
}
public function testMultiRowIndexFile()
{
$iteration = 3;
for ($i = 0; $i < $iteration; ++$i) {
$profile = new Profile('token'.$i);
$profile->setIp('127.0.0.'.$i);
$profile->setUrl('http://foo.bar/'.$i);
$storage = $this->getStorage();
$storage->write($profile);
$storage->write($profile);
$storage->write($profile);
}
$handle = fopen(self::$tmpDir.'/index.csv', 'r');
for ($i = 0; $i < $iteration; ++$i) {
$row = fgetcsv($handle);
$this->assertEquals('token'.$i, $row[0]);
$this->assertEquals('127.0.0.'.$i, $row[1]);
$this->assertEquals('http://foo.bar/'.$i, $row[3]);
}
$this->assertFalse(fgetcsv($handle));
}
public function testReadLineFromFile()
{
$r = new \ReflectionMethod(self::$storage, 'readLineFromFile');
$r->setAccessible(true);
$h = tmpfile();
fwrite($h, "line1\n\n\nline2\n");
fseek($h, 0, SEEK_END);
$this->assertEquals('line2', $r->invoke(self::$storage, $h));
$this->assertEquals('line1', $r->invoke(self::$storage, $h));
}
}

View file

@ -0,0 +1,49 @@
<?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;
use Symfony\Component\HttpKernel\Profiler\MemcacheProfilerStorage;
use Symfony\Component\HttpKernel\Tests\Profiler\Mock\MemcacheMock;
class MemcacheProfilerStorageTest extends AbstractProfilerStorageTest
{
protected static $storage;
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 tearDown()
{
if (self::$storage) {
self::$storage->purge();
self::$storage = false;
}
}
/**
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
*/
protected function getStorage()
{
return self::$storage;
}
}

View file

@ -0,0 +1,49 @@
<?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;
use Symfony\Component\HttpKernel\Profiler\MemcachedProfilerStorage;
use Symfony\Component\HttpKernel\Tests\Profiler\Mock\MemcachedMock;
class MemcachedProfilerStorageTest extends AbstractProfilerStorageTest
{
protected static $storage;
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 tearDown()
{
if (self::$storage) {
self::$storage->purge();
self::$storage = false;
}
}
/**
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
*/
protected function getStorage()
{
return self::$storage;
}
}

View file

@ -0,0 +1,254 @@
<?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 = false;
private $storage = array();
/**
* Open memcached server connection.
*
* @param string $host
* @param int $port
* @param int $timeout
*
* @return bool
*/
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 int $port
* @param int $timeout
*
* @return bool
*/
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 int $port
* @param bool $persistent
* @param int $weight
* @param int $timeout
* @param int $retry_interval
* @param bool $status
* @param callable $failure_callback
* @param int $timeoutms
*
* @return bool
*/
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 int $flag
* @param int $expire
*
* @return bool
*/
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 int $flag
* @param int $expire
*
* @return bool
*/
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 int $flag
* @param int $expire
*
* @return bool
*/
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 int|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 bool
*/
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 bool
*/
public function flush()
{
if (!$this->connected) {
return false;
}
$this->storage = array();
return true;
}
/**
* Close memcached 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;
}
}

View file

@ -0,0 +1,219 @@
<?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 = false;
private $storage = array();
/**
* Set a Memcached option.
*
* @param int $option
* @param mixed $value
*
* @return bool
*/
public function setOption($option, $value)
{
return true;
}
/**
* Add a memcached server to connection pool.
*
* @param string $host
* @param int $port
* @param int $weight
*
* @return bool
*/
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 int $expiration
*
* @return bool
*/
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 int $expiration
*
* @return bool
*/
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 int $expiration
*
* @return bool
*/
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 bool
*/
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 bool
*/
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 bool
*/
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 bool
*/
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;
}
}

View file

@ -0,0 +1,247 @@
<?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;
}
}

View file

@ -0,0 +1,165 @@
<?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;
use Symfony\Component\HttpKernel\Profiler\MongoDbProfilerStorage;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DummyMongoDbProfilerStorage extends MongoDbProfilerStorage
{
public function getMongo()
{
return parent::getMongo();
}
}
class MongoDbProfilerStorageTestDataCollector extends DataCollector
{
public function setData($data)
{
$this->data = $data;
}
public function getData()
{
return $this->data;
}
public function collect(Request $request, Response $response, \Exception $exception = null)
{
}
public function getName()
{
return 'test_data_collector';
}
}
class MongoDbProfilerStorageTest extends AbstractProfilerStorageTest
{
protected static $storage;
public static function setUpBeforeClass()
{
if (extension_loaded('mongo')) {
self::$storage = new DummyMongoDbProfilerStorage('mongodb://localhost/symfony_tests/profiler_data', '', '', 86400);
try {
self::$storage->getMongo();
} catch (\MongoConnectionException $e) {
self::$storage = null;
}
}
}
public static function tearDownAfterClass()
{
if (self::$storage) {
self::$storage->purge();
self::$storage = null;
}
}
public function getDsns()
{
return array(
array('mongodb://localhost/symfony_tests/profiler_data', array(
'mongodb://localhost/symfony_tests',
'symfony_tests',
'profiler_data',
)),
array('mongodb://user:password@localhost/symfony_tests/profiler_data', array(
'mongodb://user:password@localhost/symfony_tests',
'symfony_tests',
'profiler_data',
)),
array('mongodb://user:password@localhost/admin/symfony_tests/profiler_data', array(
'mongodb://user:password@localhost/admin',
'symfony_tests',
'profiler_data',
)),
array('mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin/symfony_tests/profiler_data', array(
'mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin',
'symfony_tests',
'profiler_data',
)),
);
}
public function testCleanup()
{
$dt = new \DateTime('-2 day');
for ($i = 0; $i < 3; ++$i) {
$dt->modify('-1 day');
$profile = new Profile('time_'.$i);
$profile->setTime($dt->getTimestamp());
$profile->setMethod('GET');
self::$storage->write($profile);
}
$records = self::$storage->find('', '', 3, 'GET');
$this->assertCount(1, $records, '->find() returns only one record');
$this->assertEquals($records[0]['token'], 'time_2', '->find() returns the latest added record');
self::$storage->purge();
}
/**
* @dataProvider getDsns
*/
public function testDsnParser($dsn, $expected)
{
$m = new \ReflectionMethod(self::$storage, 'parseDsn');
$m->setAccessible(true);
$this->assertEquals($expected, $m->invoke(self::$storage, $dsn));
}
public function testUtf8()
{
$profile = new Profile('utf8_test_profile');
$data = 'HЁʃʃϿ, ϢorЃd!';
$nonUtf8Data = mb_convert_encoding($data, 'UCS-2');
$collector = new MongoDbProfilerStorageTestDataCollector();
$collector->setData($nonUtf8Data);
$profile->setCollectors(array($collector));
self::$storage->write($profile);
$readProfile = self::$storage->read('utf8_test_profile');
$collectors = $readProfile->getCollectors();
$this->assertCount(1, $collectors);
$this->assertArrayHasKey('test_data_collector', $collectors);
$this->assertEquals($nonUtf8Data, $collectors['test_data_collector']->getData(), 'Non-UTF8 data is properly encoded/decoded');
}
/**
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
*/
protected function getStorage()
{
return self::$storage;
}
protected function setUp()
{
if (self::$storage) {
self::$storage->purge();
} else {
$this->markTestSkipped('MongoDbProfilerStorageTest requires the mongo PHP extension and a MongoDB server on localhost');
}
}
}

View file

@ -0,0 +1,86 @@
<?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;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
use Symfony\Component\HttpKernel\Profiler\SqliteProfilerStorage;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ProfilerTest extends \PHPUnit_Framework_TestCase
{
private $tmp;
private $storage;
public function testCollect()
{
$request = new Request();
$request->query->set('foo', 'bar');
$response = new Response('', 204);
$collector = new RequestDataCollector();
$profiler = new Profiler($this->storage);
$profiler->add($collector);
$profile = $profiler->collect($request, $response);
$this->assertSame(204, $profile->getStatusCode());
$this->assertSame('GET', $profile->getMethod());
$this->assertEquals(array('foo' => 'bar'), $profiler->get('request')->getRequestQuery()->all());
}
public function testFindWorksWithDates()
{
$profiler = new Profiler($this->storage);
$this->assertCount(0, $profiler->find(null, null, null, null, '7th April 2014', '9th April 2014'));
}
public function testFindWorksWithTimestamps()
{
$profiler = new Profiler($this->storage);
$this->assertCount(0, $profiler->find(null, null, null, null, '1396828800', '1397001600'));
}
public function testFindWorksWithInvalidDates()
{
$profiler = new Profiler($this->storage);
$this->assertCount(0, $profiler->find(null, null, null, null, 'some string', ''));
}
protected function setUp()
{
if (!class_exists('SQLite3') && (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers()))) {
$this->markTestSkipped('This test requires SQLite support in your environment');
}
$this->tmp = tempnam(sys_get_temp_dir(), 'sf2_profiler');
if (file_exists($this->tmp)) {
@unlink($this->tmp);
}
$this->storage = new SqliteProfilerStorage('sqlite:'.$this->tmp);
$this->storage->purge();
}
protected function tearDown()
{
if (null !== $this->storage) {
$this->storage->purge();
$this->storage = null;
@unlink($this->tmp);
}
}
}

View file

@ -0,0 +1,49 @@
<?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;
use Symfony\Component\HttpKernel\Profiler\RedisProfilerStorage;
use Symfony\Component\HttpKernel\Tests\Profiler\Mock\RedisMock;
class RedisProfilerStorageTest extends AbstractProfilerStorageTest
{
protected static $storage;
protected function setUp()
{
$redisMock = new RedisMock();
$redisMock->connect('127.0.0.1', 6379);
self::$storage = new RedisProfilerStorage('redis://127.0.0.1:6379', '', '', 86400);
self::$storage->setRedis($redisMock);
if (self::$storage) {
self::$storage->purge();
}
}
protected function tearDown()
{
if (self::$storage) {
self::$storage->purge();
self::$storage = false;
}
}
/**
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
*/
protected function getStorage()
{
return self::$storage;
}
}

View file

@ -0,0 +1,50 @@
<?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;
use Symfony\Component\HttpKernel\Profiler\SqliteProfilerStorage;
class SqliteProfilerStorageTest extends AbstractProfilerStorageTest
{
protected static $dbFile;
protected static $storage;
public static function setUpBeforeClass()
{
self::$dbFile = tempnam(sys_get_temp_dir(), 'sf2_sqlite_storage');
if (file_exists(self::$dbFile)) {
@unlink(self::$dbFile);
}
self::$storage = new SqliteProfilerStorage('sqlite:'.self::$dbFile);
}
public static function tearDownAfterClass()
{
@unlink(self::$dbFile);
}
protected function setUp()
{
if (!class_exists('SQLite3') && (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers()))) {
$this->markTestSkipped('This test requires SQLite support in your environment');
}
self::$storage->purge();
}
/**
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
*/
protected function getStorage()
{
return self::$storage;
}
}