From ce8a441c10267ebd813c4ef484f61ca8d9a79845 Mon Sep 17 00:00:00 2001 From: Brooks Boyd Date: Thu, 11 Apr 2013 15:53:45 -0500 Subject: [PATCH] [2.3][HttpFoundation] PDO Session handling enhancements --- .../Component/HttpFoundation/CHANGELOG.md | 4 ++ .../Storage/Handler/PdoSessionHandler.php | 4 +- .../Session/Storage/NativeSessionStorage.php | 2 +- .../Storage/Handler/PdoSessionHandlerTest.php | 38 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 597b65b3f6..601cd3f708 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -2,8 +2,12 @@ CHANGELOG ========= 2.3.0 +----- * `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 ----- diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php index 487dbc41e5..347cbee706 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php @@ -48,7 +48,9 @@ class PdoSessionHandler implements \SessionHandlerInterface if (!array_key_exists('db_table', $dbOptions)) { 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->dbOptions = array_merge(array( 'db_id_col' => 'sess_id', diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index 4b9be5e9b2..b12d9cbf98 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -97,7 +97,7 @@ class NativeSessionStorage implements SessionStorageInterface */ 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); if (version_compare(phpversion(), '5.4.0', '>=')) { diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php index ff1565a183..1abf3844c6 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php @@ -24,10 +24,48 @@ class PdoSessionHandlerTest extends \PHPUnit_Framework_TestCase } $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)"; $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() { $storage1 = new PdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array());