[2.3][HttpFoundation] PDO Session handling enhancements

This commit is contained in:
Brooks Boyd 2013-04-11 15:53:45 -05:00 committed by Fabien Potencier
parent 3cb87a3f95
commit ce8a441c10
4 changed files with 46 additions and 2 deletions

View File

@ -2,8 +2,12 @@ CHANGELOG
========= =========
2.3.0 2.3.0
-----
* `UploadedFile::isValid` now returns false if the file was not uploaded via HTTP (in a non-test mode) * `UploadedFile::isValid` now returns false if the file was not uploaded via HTTP (in a non-test mode)
* Improved error-handling of `\Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler`
to ensure the supplied PDO handler throws Exceptions on error (as the class expects). Added related test cases
to verify that Exceptions are properly thrown when the PDO queries fail.
2.2.0 2.2.0
----- -----

View File

@ -48,7 +48,9 @@ class PdoSessionHandler implements \SessionHandlerInterface
if (!array_key_exists('db_table', $dbOptions)) { if (!array_key_exists('db_table', $dbOptions)) {
throw new \InvalidArgumentException('You must provide the "db_table" option for a PdoSessionStorage.'); throw new \InvalidArgumentException('You must provide the "db_table" option for a PdoSessionStorage.');
} }
if (\PDO::ERRMODE_EXCEPTION !== $pdo->getAttribute(\PDO::ATTR_ERRMODE)) {
throw new \InvalidArgumentException(sprintf('"%s" requires PDO error mode attribute be set to throw Exceptions (i.e. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION))', __CLASS__));
}
$this->pdo = $pdo; $this->pdo = $pdo;
$this->dbOptions = array_merge(array( $this->dbOptions = array_merge(array(
'db_id_col' => 'sess_id', 'db_id_col' => 'sess_id',

View File

@ -97,7 +97,7 @@ class NativeSessionStorage implements SessionStorageInterface
*/ */
public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null) public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
{ {
ini_set('session.cache_limiter', ''); // disable by default because it's managed by HeaderBag (if used) session_cache_limiter(''); // disable by default because it's managed by HeaderBag (if used)
ini_set('session.use_cookies', 1); ini_set('session.use_cookies', 1);
if (version_compare(phpversion(), '5.4.0', '>=')) { if (version_compare(phpversion(), '5.4.0', '>=')) {

View File

@ -24,10 +24,48 @@ class PdoSessionHandlerTest extends \PHPUnit_Framework_TestCase
} }
$this->pdo = new \PDO("sqlite::memory:"); $this->pdo = new \PDO("sqlite::memory:");
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$sql = "CREATE TABLE sessions (sess_id VARCHAR(255) PRIMARY KEY, sess_data TEXT, sess_time INTEGER)"; $sql = "CREATE TABLE sessions (sess_id VARCHAR(255) PRIMARY KEY, sess_data TEXT, sess_time INTEGER)";
$this->pdo->exec($sql); $this->pdo->exec($sql);
} }
public function testIncompleteOptions()
{
$this->setExpectedException('InvalidArgumentException');
$storage = new PdoSessionHandler($this->pdo, array(), array());
}
public function testWrongPdoErrMode()
{
$pdo = new \PDO("sqlite::memory:");
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
$pdo->exec("CREATE TABLE sessions (sess_id VARCHAR(255) PRIMARY KEY, sess_data TEXT, sess_time INTEGER)");
$this->setExpectedException('InvalidArgumentException');
$storage = new PdoSessionHandler($pdo, array('db_table' => 'sessions'), array());
}
public function testWrongTableOptionsWrite()
{
$storage = new PdoSessionHandler($this->pdo, array('db_table' => 'bad_name'), array());
$this->setExpectedException('RuntimeException');
$storage->write('foo', 'bar');
}
public function testWrongTableOptionsRead()
{
$storage = new PdoSessionHandler($this->pdo, array('db_table' => 'bad_name'), array());
$this->setExpectedException('RuntimeException');
$storage->read('foo', 'bar');
}
public function testWriteRead()
{
$storage = new PdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array());
$storage->write('foo', 'bar');
$this->assertEquals('bar', $storage->read('foo'), 'written value can be read back correctly');
}
public function testMultipleInstances() public function testMultipleInstances()
{ {
$storage1 = new PdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array()); $storage1 = new PdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array());