Partially revert "[HttpFoundation][Sessions] Refactored tests"

This partially reverts commit 3c8cc0a1a0.
This commit is contained in:
Drak 2012-07-13 14:58:58 +01:00
parent 39813a0a05
commit 345678786e
5 changed files with 115 additions and 26 deletions

View File

@ -0,0 +1,24 @@
<?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\Session\Storage\Handler;
/**
* Adds SessionHandler functionality if available.
*
* @see http://php.net/sessionhandler
*/
if (version_compare(phpversion(), '5.4.0', '>=')) {
class NativeSessionHandler extends \SessionHandler {}
} else {
class NativeSessionHandler {}
}

View File

@ -11,9 +11,6 @@
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.
@ -28,27 +25,35 @@ use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
class MockFileSessionStorage extends MockArraySessionStorage
{
/**
* @var FileSessionHandler
* @var string
*/
private $handler;
private $savePath;
/**
* @var array
*/
private $sessionData;
/**
* 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
* @param string $savePath Path of directory to save session files.
* @param string $name Session name.
* @param MetadataBag $metaBag MetadataBag instance.
*/
public function __construct($savePath = null, $name = 'MOCKSESSID', FileSessionHandler $handler = null, MetadataBag $metaData = null)
public function __construct($savePath = null, $name = 'MOCKSESSID', MetadataBag $metaBag = null)
{
if (null == $handler) {
$handler = new FileSessionHandler($savePath, 'mocksess_');
if (null === $savePath) {
$savePath = sys_get_temp_dir();
}
$this->handler = $handler;
if (!is_dir($savePath)) {
mkdir($savePath, 0777, true);
}
parent::__construct($name, $metaData);
$this->savePath = $savePath;
parent::__construct($name, $metaBag);
}
/**
@ -92,7 +97,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
*/
public function save()
{
$this->handler->write($this->id, serialize($this->data));
file_put_contents($this->getFilePath(), serialize($this->data));
// this is needed for Silex, where the session object is re-used across requests
// in functional tests. In Symfony, the container is rebooted, so we don't have
@ -106,7 +111,19 @@ class MockFileSessionStorage extends MockArraySessionStorage
*/
private function destroy()
{
$this->handler->destroy($this->id);
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';
}
/**
@ -114,8 +131,8 @@ class MockFileSessionStorage extends MockArraySessionStorage
*/
private function read()
{
$data = $this->handler->read($this->id);
$this->data = $data ? unserialize($data) : array();
$filePath = $this->getFilePath();
$this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array();
$this->loadSession();
}

View File

@ -0,0 +1,49 @@
<?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'));
}
}

View File

@ -28,7 +28,7 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
private $sessionDir;
/**
* @var MockFileSessionStorage
* @var FileMockSessionStorage
*/
protected $storage;
@ -40,14 +40,12 @@ 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()

View File

@ -12,6 +12,7 @@
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;
@ -136,8 +137,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());
}