8000 [HttpFoundation][Sessions] Refactored tests · symfony/symfony@3c8cc0a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c8cc0a

Browse files
author
Drak
committed
[HttpFoundation][Sessions] Refactored tests
1 parent 13a2c82 commit 3c8cc0a

File tree

7 files changed

+70
-111
lines changed

7 files changed

+70
-111
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/FileSessionHandler.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,63 +23,71 @@ class FileSessionHandler implements \SessionHandlerInterface
2323
*/
2424
private $savePath;
2525

26+
/**
27+
* @var string
28+
*/
29+
private $prefix;
30+
2631
/**
2732
* Constructor.
2833
*
2934
* @param string $savePath Path of directory to save session files.
3035
*/
31-
public function __construct($savePath)
36+
public function __construct($savePath = null, $prefix = 'sess_')
3237
{
38+
if (null === $savePath) {
39+
$savePath = sys_get_temp_dir();
40+
}
41+
3342
$this->savePath = $savePath;
3443
if (false === is_dir($this->savePath)) {
3544
mkdir($this->savePath, 0777, true);
3645
}
46+
47+
$this->prefix = $prefix;
3748
}
3849

3950
/**
4051
* {@inheritdoc]
4152
*/
42-
function open($savePath, $sessionName)
53+
public function open($savePath, $sessionName)
4354
{
4455
return true;
4556
}
4657

4758
/**
4859
* {@inheritdoc]
4960
*/
50-
function close()
61+
public function close()
5162
{
5263
return true;
5364
}
5465

5566
/**
5667
* {@inheritdoc]
5768
*/
58-
function read($id)
69+
public function read($id)
5970
{
60-
$file = $this->savePath.'/sess_'.$id;
61-
if (file_exists($file)) {
62-
return file_get_contents($file);
63-
}
71+
$file = $this->getPath().$id;
6472

65-
return '';
73+
return is_readable($file) ? file_get_contents($file) : '';
6674
}
6775

6876
/**
6977
* {@inheritdoc]
7078
*/
71-
function write($id, $data)
79+
public function write($id, $data)
7280
{
73-
return false === file_put_contents($this->savePath.'/sess_'.$id, $data) ? false : true;
81+
return false === file_put_contents($this->getPath().$id, $data) ? false : true;
7482
}
7583

7684
/**
7785
* {@inheritdoc]
7886
*/
79-
function destroy($id)
87+
public function destroy($id)
8088
{
81-
$file = $this->savePath.'/sess_'.$id;
82-
if (file_exists($file)) {
89+
$file = $this->getPath().$id;
90+
if (is_file($file)) {
8391
unlink($file);
8492
}
8593

@@ -89,14 +97,19 @@ function destroy($id)
8997
/**
9098
* {@inheritdoc]
9199
*/
92-
function gc($maxlifetime)
100+
public function gc($maxlifetime)
93101
{
F438 94-
foreach (glob($this->savePath.'/sess_*') as $file) {
102+
foreach (glob($this->getPath().'*') as $file) {
95103
if ((filemtime($file) + $maxlifetime) < time()) {
96104
unlink($file);
97105
}
98106
}
99107

100108
return true;
101109
}
110+
111+
private function getPath()
112+
{
113+
return $this->savePath.'/'.$this->prefix;
114+
}
102115
}

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NullSessionHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* NullSessionHandler.
1616
*
17-
* Can be used in unit testing or in a sitation where persisted sessions are not desired.
17+
* Can be used in unit testing or in a situations where persisted sessions are not desired.
1818
*
1919
* @author Drak <drak@zikula.org>
2020
*

src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\HttpFoundation\Session\Storage;
1313

14+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\FileSessionHandler;
15+
use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
16+
1417
/**
1518
* MockFileSessionStorage is used to mock sessions for
1619
* functional testing when done in a single PHP process.
@@ -25,31 +28,32 @@
2528
class MockFileSessionStorage extends MockArraySessionStorage
2629
{
2730
/**
28-
* @var string
31+
* @var array
2932
*/
30-
private $savePath;
31-
3233
private $sessionData;
3334

35+
/**
36+
* @var FileSessionHandler
37+
*/
38+
private $handler;
39+
3440
/**
3541
* Constructor.
3642
*
37-
* @param string $savePath Path of directory to save session files.
38-
* @param string $name Session name.
43+
* @param string $savePath Path of directory to save session files.
44+
* @param string $name Session name.
45+
* @param FileSessionHandler $handler Save handler
46+
* @param MetadataBag $metaData Metadatabag
3947
*/
40-
public function __construct($savePath = null, $name = 'MOCKSESSID')
48+
public function __construct($savePath = null, $name = 'MOCKSESSID', FileSessionHandler $handler = null, MetadataBag $metaData = null)
4149
{
42-
if (null === $savePath) {
43-
$savePath = sys_get_temp_dir();
44-
}
45-
46-
if (!is_dir($savePath)) {
47-
mkdir($savePath, 0777, true);
50+
if (null == $handler) {
51+
$handler = new FileSessionHandler($savePath, 'mocksess_');
4852
}
4953

50-
$this->savePath = $savePath;
54+
$this->handler = $handler;
5155

52-
parent::__construct($name);
56+
parent::__construct($name, $metaData);
5357
}
5458

5559
/**
@@ -93,7 +97,7 @@ public function regenerate($destroy = false, $lifetime = null)
9397
*/
9498
public function save()
9599
{
96-
file_put_contents($this->getFilePath(), serialize($this->data));
100+
$this->handler->write($this->id, serialize($this->data));
97101
}
98102

99103
/**
@@ -102,28 +106,16 @@ public function save()
102106
*/
103107
private function destroy()
104108
{
105-
if (is_file($this->getFilePath())) {
106-
unlink($this->getFilePath());
107-
}
108-
}
109-
110-
/**
111-
* Calculate path to file.
112-
*
113-
* @return string File path
114-
*/
115-
private function getFilePath()
116-
{
117-
return $this->savePath.'/'.$this->id.'.mocksess';
109+
$this->handler->destroy($this->id);
118110
}
119111

120112
/**
121113
* Reads session from storage and loads session.
122114
*/
123115
private function read()
124116
{
125-
$filePath = $this->getFilePath();
126-
$this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array();
117+
$data = $this->handler->read($this->id);
118+
$this->data = $data ? unserialize($data) : array();
127119

128120
$this->loadSession();
129121
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FilelSessionHandlerTest.php renamed to src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FileSessionHandlerTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,22 @@ class FileSessionStorageTest extends \PHPUnit_Framework_TestCase
2525
*/
2626
private $handler;
2727

28+
/**
29+
* @var string
30+
*/
2831
private $path;
2932

3033
public function setUp()
3134
{
32-
$this->path = sys_get_temp_dir().'/filesessionhandler/';
33-
$this->handler = new FileSessionHandler($this->path);
35+
$this->path = sys_get_temp_dir().'/filesessionhandler';
36+
$this->handler = new FileSessionHandler($this->path, 'mocksess_');
3437

3538
parent::setUp();
3639
}
3740

3841
public function tearDown()
3942
{
40-
foreach (glob($this->path.'*') as $file) {
43+
foreach (glob($this->path.'/*') as $file) {
4144
unlink($file);
4245
}
4346

@@ -79,7 +82,7 @@ public function testDestroy()
7982

8083
public function testGc()
8184
{
82-
$prefix = $this->path.'sess_';
85+
$prefix = $this->path.'/mocksess_';
8386
$this->handler->write('1', 'data');
8487
touch($prefix.'1', time()-86400);
8588

@@ -92,13 +95,12 @@ public function testGc()
9295
$this->handler->write('4', 'data');
9396

9497
$this->handler->gc(90000);
95-
$this->assertEquals(4, count(glob($this->path.'*')));
98+
$this->assertEquals(4, count(glob($this->path.'/*')));
9699

97100
$this->handler->gc(4000);
98-
$this->assertEquals(3, count(glob($this->path.'*')));
101+
$this->assertEquals(3, count(glob($this->path.'/*')));
99102

100103
$this->handler->gc(200);
101-
$this->assertEquals(1, count(glob($this->path.'*')));
104+
$this->assertEquals(1, count(glob($this->path.'/*')));
102105
}
103106
}
104-

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeFileSessionHandlerTest.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockFileSessionStorageTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
2828
private $sessionDir;
2929

3030
/**
31-
* @var FileMockSessionStorage
31+
* @var MockFileSessionStorage
3232
*/
3333
protected $storage;
3434

@@ -40,12 +40,14 @@ protected function setUp()
4040

4141
protected function tearDown()
4242
{
43+
foreach (glob($this->sessionDir.'/mocksess_*') as $file) {
44+
unlink($file);
45+
}
46+
47+
rmdir($this->sessionDir);
48+
4349
$this->sessionDir = null;
4450
$this->storage = null;
45-
array_map('unlink', glob($this->sessionDir.'/*.session'));
46-
if (is_dir($this->sessionDir)) {
47-
rmdir($this->sessionDir);
48-
}
4951
}
5052

5153
public function testStart()

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
1313

1414
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
15-
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler;
1615
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
1716
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
1817
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
@@ -137,8 +136,8 @@ public function testSetSaveHandlerPHP53()
137136
$this->markTestSkipped('Test skipped, for PHP 5.3 only.');
138137
}
139138

139+
ini_set('session.save_handler', 'files');
140140
$storage = $this->getStorage();
141-
$storage->setSaveHandler(new NativeFileSessionHandler());
142141
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
143142
}
144143

0 commit comments

Comments
 (0)
0