14 use PHPUnit\Framework\TestCase;
29 @unlink($this->dbFile);
36 $this->dbFile = tempnam(sys_get_temp_dir(),
'sf2_sqlite_sessions');
38 return 'sqlite:'.$this->dbFile;
43 $pdo = new \PDO(
'sqlite::memory:');
44 $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
57 $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
93 $this->assertSame(
'', $data,
'New session returns empty string data');
98 $this->assertSame(
'data', $data,
'Written value can be read back correctly');
112 $this->assertSame(
'', $data,
'New session returns empty string data');
117 $this->assertSame(
'data', $data,
'Written value can be read back correctly');
122 $sessionData =
'da'.
"\0".
'ta';
127 $storage->write(
'id', $sessionData);
129 $this->assertSame(
'', $readData,
'New session returns empty string data');
134 $this->assertSame($sessionData, $readData,
'Written value can be read back correctly');
139 if (defined(
'HHVM_VERSION')) {
140 $this->markTestSkipped(
'PHPUnit_MockObject cannot mock the PDOStatement class on HHVM. See https://github.com/sebastianbergmann/phpunit-mock-objects/pull/289');
144 $pdo->prepareResult = $this->getMockBuilder(
'PDOStatement')->getMock();
147 $stream = $this->createStream($content);
149 $pdo->prepareResult->expects($this->once())->method(
'fetchAll')
150 ->will($this->returnValue(array(array($stream, 42, time()))));
155 $this->assertSame($content, $result);
160 if (defined(
'HHVM_VERSION')) {
161 $this->markTestSkipped(
'PHPUnit_MockObject cannot mock the PDOStatement class on HHVM. See https://github.com/sebastianbergmann/phpunit-mock-objects/pull/289');
165 $selectStmt = $this->getMockBuilder(
'PDOStatement')->getMock();
166 $insertStmt = $this->getMockBuilder(
'PDOStatement')->getMock();
168 $pdo->prepareResult =
function ($statement) use ($selectStmt, $insertStmt) {
169 return 0 === strpos($statement,
'INSERT') ? $insertStmt : $selectStmt;
173 $stream = $this->createStream($content);
176 $selectStmt->expects($this->atLeast(2))->method(
'fetchAll')
177 ->will($this->returnCallback(
function () use (&$exception, $stream) {
178 return $exception ? array(array($stream, 42, time())) : array();
181 $insertStmt->expects($this->once())->method(
'execute')
182 ->will($this->returnCallback(
function () use (&$exception) {
183 throw $exception = new \PDOException(
'',
'23');
189 $this->assertSame($content, $result);
202 $readDataCaseSensitive =
$storage->read(
'ID');
203 $readDataNoCharFolding =
$storage->read(
'tést');
204 $readDataKeepSpace =
$storage->read(
'space ');
205 $readDataExtraSpace =
$storage->read(
'space ');
208 $this->assertSame(
'', $readDataCaseSensitive,
'Retrieval by ID should be case-sensitive (collation setting)');
209 $this->assertSame(
'', $readDataNoCharFolding,
'Retrieval by ID should not do character folding (collation setting)');
210 $this->assertSame(
'data', $readDataKeepSpace,
'Retrieval by ID requires spaces as-is');
211 $this->assertSame(
'', $readDataExtraSpace,
'Retrieval by ID requires spaces as-is');
223 $storage->write(
'new_id',
'data_of_new_session_id');
230 $this->assertSame(
'data_of_new_session_id', $data,
'Data of regenerated session id is available');
238 $storage->write(
'other_id',
'other_data');
242 $otherData =
$storage->read(
'other_id');
245 $this->assertSame(
'data', $data);
246 $this->assertSame(
'other_data', $otherData);
258 $this->assertEquals(1, $pdo->query(
'SELECT COUNT(*) FROM sessions')->fetchColumn());
264 $this->assertEquals(0, $pdo->query(
'SELECT COUNT(*) FROM sessions')->fetchColumn());
269 $this->assertSame(
'', $data,
'Destroyed session returns empty string');
274 $previousLifeTime = ini_set(
'session.gc_maxlifetime', 1000);
285 ini_set(
'session.gc_maxlifetime', -1);
288 $this->assertEquals(2, $pdo->query(
'SELECT COUNT(*) FROM sessions')->fetchColumn(),
'No session pruned because gc not called');
295 ini_set(
'session.gc_maxlifetime', $previousLifeTime);
297 $this->assertSame(
'', $data,
'Session already considered garbage, so not returning data even if it is not pruned yet');
298 $this->assertEquals(1, $pdo->query(
'SELECT COUNT(*) FROM sessions')->fetchColumn(),
'Expired session is pruned');
305 $method = new \ReflectionMethod(
$storage,
'getConnection');
306 $method->setAccessible(
true);
308 $this->assertInstanceOf(
'\PDO', $method->invoke(
$storage));
315 $method = new \ReflectionMethod(
$storage,
'getConnection');
316 $method->setAccessible(
true);
318 $this->assertInstanceOf(
'\PDO', $method->invoke(
$storage));
321 private function createStream($content)
324 fwrite($stream, $content);
337 public function __construct($driverName =
null, $errorMode =
null)
339 $this->driverName = $driverName;
340 $this->errorMode =
null !== $errorMode ?: \PDO::ERRMODE_EXCEPTION;
345 if (\PDO::ATTR_ERRMODE === $attribute) {
346 return $this->errorMode;
349 if (\PDO::ATTR_DRIVER_NAME === $attribute) {
350 return $this->driverName;
353 return parent::getAttribute($attribute);
356 public function prepare($statement, $driverOptions = array())
358 return is_callable($this->prepareResult)
359 ? call_user_func($this->prepareResult, $statement, $driverOptions)