[ProfilerStorage] Make write() returns a status (Boolean)

This commit is contained in:
Victor Berchet 2011-01-18 15:41:19 +01:00 committed by Fabien Potencier
parent 5030255021
commit 3e8f8ea6af
4 changed files with 22 additions and 8 deletions

View File

@ -105,7 +105,7 @@ class Profiler
public function export()
{
$data = base64_encode(serialize(array($this->token, $this->collectors, $this->ip, $this->url, $this->time)));
return $data;
}
@ -243,10 +243,10 @@ class Profiler
$this->time = time();
$data = base64_encode(serialize($this->collectors));
try {
$this->storage->write($this->token, $data, $this->ip, $this->url, $this->time);
$this->empty = false;
} catch (\Exception $e) {
if (true === $this->storage->write($this->token, $data, $this->ip, $this->url, $this->time)) {
$this->empty =false;
} else {
if (null !== $this->logger) {
$this->logger->err(sprintf('Unable to store the profiler information (%s).', $e->getMessage()));
}

View File

@ -41,13 +41,15 @@ interface ProfilerStorageInterface
function read($token);
/**
* Reads data associated with the given token.
* Write data associated with the given token.
*
* @param string $token A token
* @param string $data The data associated with token
* @param string $ip An IP
* @param string $url An URL
* @param integer $time The time of the data
*
* @return Boolean Write operation successful
*/
function write($token, $data, $ip, $url, $time);

View File

@ -92,9 +92,16 @@ class SQLiteProfilerStorage implements ProfilerStorageInterface
':time' => $time,
':created_at' => time(),
);
$this->exec($db, 'INSERT INTO data (token, data, ip, url, time, created_at) VALUES (:token, :data, :ip, :url, :time, :created_at)', $args);
try {
$this->exec($db, 'INSERT INTO data (token, data, ip, url, time, created_at) VALUES (:token, :data, :ip, :url, :time, :created_at)', $args);
$status = true;
} catch (\Exception $e) {
$status = false;
}
$this->cleanup();
$this->close($db);
return $status;
}
/**

View File

@ -32,7 +32,6 @@ class SQLiteProfilerStorageTest extends \PHPUnit_Framework_TestCase
@unlink(self::$dbFile);
}
protected function setUp()
{
self::$storage->purge();
@ -59,6 +58,12 @@ class SQLiteProfilerStorageTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(false !== self::$storage->read('backslash'), '->write() accpets backslash in URL');
}
public function testStoreDuplicateToken()
{
$this->assertTrue(true === self::$storage->write('token', 'data', '127.0.0.1', 'http://foo.bar', time()), '->write() returns true when the token is unique');
$this->assertTrue(false === self::$storage->write('token', 'data', '127.0.0.1', 'http://foo.bar', time()), '->write() return false when the token is already present in the DB');
}
public function testRetrieveByIp()
{
self::$storage->write('token', 'data', '127.0.0.1', 'http://foo.bar', time());