[HttpFoundation][Sessions] Refactored tests
This commit is contained in:
parent
13a2c82f01
commit
3c8cc0a1a0
@ -23,23 +23,34 @@ class FileSessionHandler implements \SessionHandlerInterface
|
||||
*/
|
||||
private $savePath;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $prefix;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $savePath Path of directory to save session files.
|
||||
*/
|
||||
public function __construct($savePath)
|
||||
public function __construct($savePath = null, $prefix = 'sess_')
|
||||
{
|
||||
if (null === $savePath) {
|
||||
$savePath = sys_get_temp_dir();
|
||||
}
|
||||
|
||||
$this->savePath = $savePath;
|
||||
if (false === is_dir($this->savePath)) {
|
||||
mkdir($this->savePath, 0777, true);
|
||||
}
|
||||
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc]
|
||||
*/
|
||||
function open($savePath, $sessionName)
|
||||
public function open($savePath, $sessionName)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -47,7 +58,7 @@ class FileSessionHandler implements \SessionHandlerInterface
|
||||
/**
|
||||
* {@inheritdoc]
|
||||
*/
|
||||
function close()
|
||||
public function close()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -55,31 +66,28 @@ class FileSessionHandler implements \SessionHandlerInterface
|
||||
/**
|
||||
* {@inheritdoc]
|
||||
*/
|
||||
function read($id)
|
||||
public function read($id)
|
||||
{
|
||||
$file = $this->savePath.'/sess_'.$id;
|
||||
if (file_exists($file)) {
|
||||
return file_get_contents($file);
|
||||
}
|
||||
$file = $this->getPath().$id;
|
||||
|
||||
return '';
|
||||
return is_readable($file) ? file_get_contents($file) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc]
|
||||
*/
|
||||
function write($id, $data)
|
||||
public function write($id, $data)
|
||||
{
|
||||
return false === file_put_contents($this->savePath.'/sess_'.$id, $data) ? false : true;
|
||||
return false === file_put_contents($this->getPath().$id, $data) ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc]
|
||||
*/
|
||||
function destroy($id)
|
||||
public function destroy($id)
|
||||
{
|
||||
$file = $this->savePath.'/sess_'.$id;
|
||||
if (file_exists($file)) {
|
||||
$file = $this->getPath().$id;
|
||||
if (is_file($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
@ -89,9 +97,9 @@ class FileSessionHandler implements \SessionHandlerInterface
|
||||
/**
|
||||
* {@inheritdoc]
|
||||
*/
|
||||
function gc($maxlifetime)
|
||||
public function gc($maxlifetime)
|
||||
{
|
||||
foreach (glob($this->savePath.'/sess_*') as $file) {
|
||||
foreach (glob($this->getPath().'*') as $file) {
|
||||
if ((filemtime($file) + $maxlifetime) < time()) {
|
||||
unlink($file);
|
||||
}
|
||||
@ -99,4 +107,9 @@ class FileSessionHandler implements \SessionHandlerInterface
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function getPath()
|
||||
{
|
||||
return $this->savePath.'/'.$this->prefix;
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
|
||||
/**
|
||||
* NullSessionHandler.
|
||||
*
|
||||
* Can be used in unit testing or in a sitation where persisted sessions are not desired.
|
||||
* Can be used in unit testing or in a situations where persisted sessions are not desired.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
|
@ -11,6 +11,9 @@
|
||||
|
||||
namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\FileSessionHandler;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
|
||||
|
||||
/**
|
||||
* MockFileSessionStorage is used to mock sessions for
|
||||
* functional testing when done in a single PHP process.
|
||||
@ -25,31 +28,32 @@ namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
class MockFileSessionStorage extends MockArraySessionStorage
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
* @var array
|
||||
*/
|
||||
private $savePath;
|
||||
|
||||
private $sessionData;
|
||||
|
||||
/**
|
||||
* @var FileSessionHandler
|
||||
*/
|
||||
private $handler;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $savePath Path of directory to save session files.
|
||||
* @param string $name Session name.
|
||||
* @param FileSessionHandler $handler Save handler
|
||||
* @param MetadataBag $metaData Metadatabag
|
||||
*/
|
||||
public function __construct($savePath = null, $name = 'MOCKSESSID')
|
||||
public function __construct($savePath = null, $name = 'MOCKSESSID', FileSessionHandler $handler = null, MetadataBag $metaData = null)
|
||||
{
|
||||
if (null === $savePath) {
|
||||
$savePath = sys_get_temp_dir();
|
||||
if (null == $handler) {
|
||||
$handler = new FileSessionHandler($savePath, 'mocksess_');
|
||||
}
|
||||
|
||||
if (!is_dir($savePath)) {
|
||||
mkdir($savePath, 0777, true);
|
||||
}
|
||||
$this->handler = $handler;
|
||||
|
||||
$this->savePath = $savePath;
|
||||
|
||||
parent::__construct($name);
|
||||
parent::__construct($name, $metaData);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,7 +97,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
file_put_contents($this->getFilePath(), serialize($this->data));
|
||||
$this->handler->write($this->id, serialize($this->data));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -102,19 +106,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
*/
|
||||
private function destroy()
|
||||
{
|
||||
if (is_file($this->getFilePath())) {
|
||||
unlink($this->getFilePath());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate path to file.
|
||||
*
|
||||
* @return string File path
|
||||
*/
|
||||
private function getFilePath()
|
||||
{
|
||||
return $this->savePath.'/'.$this->id.'.mocksess';
|
||||
$this->handler->destroy($this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -122,8 +114,8 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
*/
|
||||
private function read()
|
||||
{
|
||||
$filePath = $this->getFilePath();
|
||||
$this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array();
|
||||
$data = $this->handler->read($this->id);
|
||||
$this->data = $data ? unserialize($data) : array();
|
||||
|
||||
$this->loadSession();
|
||||
}
|
||||
|
@ -25,19 +25,22 @@ class FileSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
private $handler;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $path;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->path = sys_get_temp_dir().'/filesessionhandler/';
|
||||
$this->handler = new FileSessionHandler($this->path);
|
||||
$this->path = sys_get_temp_dir().'/filesessionhandler';
|
||||
$this->handler = new FileSessionHandler($this->path, 'mocksess_');
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
foreach (glob($this->path.'*') as $file) {
|
||||
foreach (glob($this->path.'/*') as $file) {
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
@ -79,7 +82,7 @@ class FileSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testGc()
|
||||
{
|
||||
$prefix = $this->path.'sess_';
|
||||
$prefix = $this->path.'/mocksess_';
|
||||
$this->handler->write('1', 'data');
|
||||
touch($prefix.'1', time()-86400);
|
||||
|
||||
@ -92,13 +95,12 @@ class FileSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
$this->handler->write('4', 'data');
|
||||
|
||||
$this->handler->gc(90000);
|
||||
$this->assertEquals(4, count(glob($this->path.'*')));
|
||||
$this->assertEquals(4, count(glob($this->path.'/*')));
|
||||
|
||||
$this->handler->gc(4000);
|
||||
$this->assertEquals(3, count(glob($this->path.'*')));
|
||||
$this->assertEquals(3, count(glob($this->path.'/*')));
|
||||
|
||||
$this->handler->gc(200);
|
||||
$this->assertEquals(1, count(glob($this->path.'*')));
|
||||
$this->assertEquals(1, count(glob($this->path.'/*')));
|
||||
}
|
||||
}
|
||||
|
@ -1,49 +0,0 @@
|
||||
<?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\HttpFoundation\Tests\Session\Storage\Handler;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
|
||||
|
||||
/**
|
||||
* Test class for NativeFileSessionHandler.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class NativeFileSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConstruct()
|
||||
{
|
||||
$storage = new NativeSessionStorage(array('name' => 'TESTING'), new NativeFileSessionHandler(sys_get_temp_dir()));
|
||||
|
||||
if (version_compare(phpversion(), '5.4.0', '<')) {
|
||||
$this->assertEquals('files', $storage->getSaveHandler()->getSaveHandlerName());
|
||||
$this->assertEquals('files', ini_get('session.save_handler'));
|
||||
} else {
|
||||
$this->assertEquals('files', $storage->getSaveHandler()->getSaveHandlerName());
|
||||
$this->assertEquals('user', ini_get('session.save_handler'));
|
||||
}
|
||||
|
||||
$this->assertEquals(sys_get_temp_dir(), ini_get('session.save_path'));
|
||||
$this->assertEquals('TESTING', ini_get('session.name'));
|
||||
}
|
||||
|
||||
public function testConstructDefault()
|
||||
{
|
||||
$path = ini_get('session.save_path');
|
||||
$storage = new NativeSessionStorage(array('name' => 'TESTING'), new NativeFileSessionHandler());
|
||||
|
||||
$this->assertEquals($path, ini_get('session.save_path'));
|
||||
}
|
||||
}
|
@ -28,7 +28,7 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
private $sessionDir;
|
||||
|
||||
/**
|
||||
* @var FileMockSessionStorage
|
||||
* @var MockFileSessionStorage
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
@ -40,12 +40,14 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
foreach (glob($this->sessionDir.'/mocksess_*') as $file) {
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
rmdir($this->sessionDir);
|
||||
|
||||
$this->sessionDir = null;
|
||||
$this->storage = null;
|
||||
array_map('unlink', glob($this->sessionDir.'/*.session'));
|
||||
if (is_dir($this->sessionDir)) {
|
||||
rmdir($this->sessionDir);
|
||||
}
|
||||
}
|
||||
|
||||
public function testStart()
|
||||
|
@ -12,7 +12,6 @@
|
||||
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
||||
@ -137,8 +136,8 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
$this->markTestSkipped('Test skipped, for PHP 5.3 only.');
|
||||
}
|
||||
|
||||
ini_set('session.save_handler', 'files');
|
||||
$storage = $this->getStorage();
|
||||
$storage->setSaveHandler(new NativeFileSessionHandler());
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user