8000 [HttpFoundation] Add tests and some CS/docblocks. · symfony/symfony@cb873b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit cb873b2

Browse files
author
Drak
committed
[HttpFoundation] Add tests and some CS/docblocks.
1 parent a6a9280 commit cb873b2

File tree

10 files changed

+147
-116
lines changed

10 files changed

+147
-116
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ class Session implements SessionInterface
3838
/**
3939
* Constructor.
4040
*
41-
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
41+
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
4242
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
4343
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
4444
*/
45-
public function __construct(SessionStorageInterface $storage, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
45+
public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
4646
{
47-
$this->storage = $storage;
47+
$this->storage = $storage ?: new SessionStorage();
4848
$this->registerBag($attributes ?: new AttributeBag());
4949
$this->registerBag($flashes ?: new FlashBag());
5050
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,4 @@ protected function setOptions(array $options)
6565
}
6666
}
6767
}
68-
6968
}

src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/AbstractProxy.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
/**
1515
* AbstractProxy.
16+
*
17+
* @author Drak <drak@zikula.org>
1618
*/
1719
abstract class AbstractProxy
1820
{
@@ -43,9 +45,14 @@ public function getSaveHandlerName()
4345
return $this->saveHandlerName;
4446
}
4547

48+
/**
49+
* Is this proxy handler and instance of \SessionHandlerInterface.
50+
*
51+
* @return boolean
52+
*/
4653
public function isSessionHandlerInterface()
4754
{
48-
return (bool)($this instanceof \SessionHandlerInterface);
55+
return ($this instanceof \SessionHandlerInterface);
4956
}
5057

5158
/**
@@ -75,6 +82,6 @@ public function isActive()
7582
*/
7683
public function setActive($flag)
7784
{
78-
$this->active = (bool)$flag;
85+
$this->active = (bool) $flag;
7986
}
8087
}

src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/NativeProxy.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,17 @@
1515
* NativeProxy.
1616
*
1717
* This proxy is built-in session handlers in PHP 5.3.x
18+
*
19+
* @author Drak <drak@zikula.org>
1820
*/
1921
class NativeProxy extends AbstractProxy
2022
{
2123
/**
2224
* Constructor.
23-
*
24-
* @param $handler
2525
*/
26-
public function __construct($handler)
26+
public function __construct()
2727
{
28-
if (version_compare(phpversion(), '5.4.0', '>=') && $handler instanceof \SessionHandlerInterface) {
29-
throw new \InvalidArgumentException('This proxy is only for PHP 5.3 and not for instances of \SessionHandler or \SessionHandlerInterface');
30-
}
31-
32-
$this->handler = $handler;
28+
// this makes an educated guess as to what the handler is since it should already be set.
3329
$this->saveHandlerName = ini_get('session.save_handler');
3430
}
3531

src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
/**
1515
* SessionHandler proxy.
16+
*
17+
* @author Drak <drak@zikula.org>
1618
*/
1719
class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface
1820
{
@@ -29,7 +31,7 @@ class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterf
2931
public function __construct(\SessionHandlerInterface $handler)
3032
{
3133
$this->handler = $handler;
32-
$this->wrapper = (bool)(class_exists('SessionHandler') && $handler instanceof \SessionHandler);
34+
$this->wrapper = ($handler instanceof \SessionHandler);
3335
$this->saveHandlerName = $this->wrapper ? ini_get('session.save_handler') : 'user';
3436
}
3537

@@ -38,7 +40,7 @@ public function __construct(\SessionHandlerInterface $handler)
3840
/**
3941
* {@inheritdoc}
4042
*/
41-
function open($savePath, $sessionName)
43+
public function open($savePath, $sessionName)
4244
{
4345
$return = (bool)$this->handler->open($savePath, $sessionName);
4446

@@ -52,42 +54,42 @@ function open($savePath, $sessionName)
5254
/**
5355
* {@inheritdoc}
5456
*/
55-
function close()
57+
public function close()
5658
{
5759
$this->active = false;
5860

59-
return (bool)$this->handler->close();
61+
return (bool) $this->handler->close();
6062
}
6163

6264
/**
6365
* {@inheritdoc}
6466
*/
65-
function read($id)
67+
public function read($id)
6668
{
67-
return (string)$this->handler->read($id);
69+
return (string) $this->handler->read($id);
6870
}
6971

7072
/**
7173
* {@inheritdoc}
7274
*/
73-
function write($id, $data)
75+
public function write($id, $data)
7476
{
75-
return (bool)$this->handler->write($id, $data);
77+
return (bool) $this->handler->write($id, $data);
7678
}
7779

7880
/**
7981
* {@inheritdoc}
8082
*/
81-
function destroy($id)
83+
public function destroy($id)
8284
{
83-
return (bool)$this->handler->destroy($id);
85+
return (bool) $this->handler->destroy($id);
8486
}
8587

8688
/**
8789
* {@inheritdoc}
8890
*/
89-
function gc($maxlifetime)
91+
public function gc($maxlifetime)
9092
{
91-
return (bool)$this->handler->gc($maxlifetime);
93+
return (bool) $this->handler->gc($maxlifetime);
9294
}
9395
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,12 @@ class SessionStorage implements SessionStorageInterface
8888
* upload_progress.min-freq, "1"
8989
* url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
9090
*
91-
* @param array $options Session configuration options.
92-
* @param $handler SessionHandlerInterface.
91+
* @param array $options Session configuration options.
92+
* @param object $handler SessionHandlerInterface.
9393
*/
9494
public function __construct(array $options = array(), $handler = null)
9595
{
9696
$this->setOptions($options);
97-
9897
$this->setSaveHandler($handler);
9998
}
10099

@@ -118,7 +117,7 @@ public function start()
118117
}
119118

120119
if ($this->options['use_cookies'] && headers_sent()) {
121-
throw new \RuntimeException('Failed to start the session because header have already been sent.');
120+
throw new \RuntimeException('Failed to start the session because headers have already been sent.');
122121
}
123122

124123
// start the session
@@ -225,7 +224,7 @@ public function getBag($name)
225224
*
226225
* @see http://php.net/session.configuration
227226
*/
228-
protected function setOptions(array $options)
227+
public function setOptions(array $options)
229228
{
230229
$this->options = $options;
231230

@@ -234,7 +233,6 @@ protected function setOptions(array $options)
234233
'cache_limiter' => '', // disable by default because it's managed by HeaderBag (if used)
235234
'auto_start' => false,
236235
'use_cookies' => true,
237-
'cookie_httponly' => true,
238236
);
239237

240238
foreach ($defaults as $key => $value) {
@@ -272,14 +270,14 @@ protected function setOptions(array $options)
272270
* @see http://php.net/sessionhandlerinterface
273271
* @see http://php.net/sessionhandler
274272
*
275-
* @param object $saveHandler
273+
* @param object $saveHandler Default null means NativeProxy.
276274
*/
277-
public function setSaveHandler($saveHandler)
275+
public function setSaveHandler($saveHandler = null)
278276
{
279277
// Wrap $saveHandler in proxy
280278
if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
281279
$saveHandler = new SessionHandlerProxy($saveHandler);
282-
} else {
280+
} elseif (!$saveHandler instanceof AbstractProxy) {
283281
$saveHandler = new NativeProxy($saveHandler);
284282
}
285283

tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/Proxy/AbstractProxyTest.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,44 @@
44

55
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
66

7+
// Note until PHPUnit_Mock_Objects 1.2 is released you cannot mock abstracts due to
8+
// https://github.com/sebastianbergmann/phpunit-mock-objects/issues/73
79
class ConcreteProxy extends AbstractProxy
810
{
911

1012
}
1113

14+
class ConcreteSessionHandlerInterfaceProxy extends AbstractProxy implements \SessionHandlerInterface
15+
{
16+
public function open($savePath, $sessionName)
17+
{
18+
}
19+
20+
public function close()
21+
{
22+
}
23+
24+
public function read($id)
25+
{
26+
}
27+
28+
public function write($id, $data)
29+
{
30+
}
31+
32+
public function destroy($id)
33+
{
34+
}
35+
36+
public function gc($maxlifetime)
37+
{
38+
}
39+
}
40+
1241
/**
1342
* Test class for AbstractProxy.
1443
*
15-
* @runTestsInSeparateProcesses
44+
* @author Drak <drak@zikula.org>
1645
*/
1746
class AbstractProxyTest extends \PHPUnit_Framework_TestCase
1847
{
@@ -23,7 +52,7 @@ class AbstractProxyTest extends \PHPUnit_Framework_TestCase
2352

2453
protected function setUp()
2554
{
26-
$this->proxy = new ConcreteProxy;
55+
$this->proxy = new ConcreteProxy();
2756
}
2857

2958
protected function tearDown()
@@ -33,37 +62,31 @@ protected function tearDown()
3362

3463
public function testGetSaveHandlerName()
3564
{
36-
$this->markTestIncomplete(
37-
'This test has not been implemented yet.'
38-
);
65+
$this->assertNull($this->proxy->getSaveHandlerName());
3966
}
4067

4168
public function testIsSessionHandlerInterface()
4269
{
43-
$this->markTestIncomplete(
44-
'This test has not been implemented yet.'
45-
);
70+
$this->assertFalse($this->proxy->isSessionHandlerInterface());
71+
$sh = new ConcreteSessionHandlerInterfaceProxy();
72+
$this->assertTrue($sh->isSessionHandlerInterface());
4673
}
4774

4875
public function testIsWrapper()
4976
{
50-
$this->markTestIncomplete(
51-
'This test has not been implemented yet.'
52-
);
77+
$this->assertFalse($this->proxy->isWrapper());
5378
}
5479

5580
public function testIsActive()
5681
{
57-
$this->markTestIncomplete(
58-
'This test has not been implemented yet.'
59-
);
82+
$this->assertFalse($this->proxy->isActive());
6083
}
6184

6285
public function testSetActive()
6386
{
64-
$this->markTestIncomplete(
65-
'This test has not been implemented yet.'
66-
);
87+
$this->proxy->setActive(true);
88+
$this->assertTrue($this->proxy->isActive());
89+
$this->proxy->setActive(false);
90+
$this->assertFalse($this->proxy->isActive());
6791
}
68-
6992
}

tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/Proxy/NativeProxyPHP54Test.php

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

tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/Proxy/NativeProxyPHP53Test.php renamed to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/Proxy/NativeProxyTest.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,20 @@
88
/**
99
* Test class for NativeProxy.
1010
*
11-
* @runTestsInSeparateProcesses
11+
* @author Drak <drak@zikula.org>
1212
*/
13-
class NativeProxyPHP53Test extends \PHPUnit_Framework_TestCase
13+
class NativeProxyTest extends \PHPUnit_Framework_TestCase
1414
{
15-
protected function setUp()
16-
{
17-
if (version_compare(phpversion(), '5.4.0', '>=')) {
18-
$this->markTestSkipped('Test skipped, only for PHP 5.3');
19-
}
20-
}
21-
2215
public function testIsWrapper()
2316
{
24-
$proxy = new NativeProxy(new NativeFileSessionHandler());
17+
$proxy = new NativeProxy();
2518
$this->assertFalse($proxy->isWrapper());
2619
}
2720

2821
public function testGetSaveHandlerName()
2922
{
3023
$name = ini_get('session.save_handler');
31-
$proxy = new NativeProxy(new NativeFileSessionHandler());
24+
$proxy = new NativeProxy();
3225
$this->assertEquals($name, $proxy->getSaveHandlerName());
3326
}
3427
}

0 commit comments

Comments
 (0)
0