Skip to content

Commit

Permalink
[HttpFoundation] Add tests and some CS/docblocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Drak committed Mar 14, 2012
1 parent a6a9280 commit cb873b2
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 116 deletions.
6 changes: 3 additions & 3 deletions src/Symfony/Component/HttpFoundation/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class Session implements SessionInterface
/**
* Constructor.
*
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
*/
public function __construct(SessionStorageInterface $storage, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
{
$this->storage = $storage;
$this->storage = $storage ?: new SessionStorage();
$this->registerBag($attributes ?: new AttributeBag());
$this->registerBag($flashes ?: new FlashBag());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,4 @@ protected function setOptions(array $options)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* AbstractProxy.
*
* @author Drak <[email protected]>
*/
abstract class AbstractProxy
{
Expand Down Expand Up @@ -43,9 +45,14 @@ public function getSaveHandlerName()
return $this->saveHandlerName;
}

/**
* Is this proxy handler and instance of \SessionHandlerInterface.
*
* @return boolean
*/
public function isSessionHandlerInterface()
{
return (bool)($this instanceof \SessionHandlerInterface);
return ($this instanceof \SessionHandlerInterface);
}

/**
Expand Down Expand Up @@ -75,6 +82,6 @@ public function isActive()
*/
public function setActive($flag)
{
$this->active = (bool)$flag;
$this->active = (bool) $flag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@
* NativeProxy.
*
* This proxy is built-in session handlers in PHP 5.3.x
*
* @author Drak <[email protected]>
*/
class NativeProxy extends AbstractProxy
{
/**
* Constructor.
*
* @param $handler
*/
public function __construct($handler)
public function __construct()
{
if (version_compare(phpversion(), '5.4.0', '>=') && $handler instanceof \SessionHandlerInterface) {
throw new \InvalidArgumentException('This proxy is only for PHP 5.3 and not for instances of \SessionHandler or \SessionHandlerInterface');
}

$this->handler = $handler;
// this makes an educated guess as to what the handler is since it should already be set.
$this->saveHandlerName = ini_get('session.save_handler');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* SessionHandler proxy.
*
* @author Drak <[email protected]>
*/
class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface
{
Expand All @@ -29,7 +31,7 @@ class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterf
public function __construct(\SessionHandlerInterface $handler)
{
$this->handler = $handler;
$this->wrapper = (bool)(class_exists('SessionHandler') && $handler instanceof \SessionHandler);
$this->wrapper = ($handler instanceof \SessionHandler);
$this->saveHandlerName = $this->wrapper ? ini_get('session.save_handler') : 'user';
}

Expand All @@ -38,7 +40,7 @@ public function __construct(\SessionHandlerInterface $handler)
/**
* {@inheritdoc}
*/
function open($savePath, $sessionName)
public function open($savePath, $sessionName)
{
$return = (bool)$this->handler->open($savePath, $sessionName);

Expand All @@ -52,42 +54,42 @@ function open($savePath, $sessionName)
/**
* {@inheritdoc}
*/
function close()
public function close()
{
$this->active = false;

return (bool)$this->handler->close();
return (bool) $this->handler->close();
}

/**
* {@inheritdoc}
*/
function read($id)
public function read($id)
{
return (string)$this->handler->read($id);
return (string) $this->handler->read($id);
}

/**
* {@inheritdoc}
*/
function write($id, $data)
public function write($id, $data)
{
return (bool)$this->handler->write($id, $data);
return (bool) $this->handler->write($id, $data);
}

/**
* {@inheritdoc}
*/
function destroy($id)
public function destroy($id)
{
return (bool)$this->handler->destroy($id);
return (bool) $this->handler->destroy($id);
}

/**
* {@inheritdoc}
*/
function gc($maxlifetime)
public function gc($maxlifetime)
{
return (bool)$this->handler->gc($maxlifetime);
return (bool) $this->handler->gc($maxlifetime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ class SessionStorage implements SessionStorageInterface
* upload_progress.min-freq, "1"
* url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
*
* @param array $options Session configuration options.
* @param $handler SessionHandlerInterface.
* @param array $options Session configuration options.
* @param object $handler SessionHandlerInterface.
*/
public function __construct(array $options = array(), $handler = null)
{
$this->setOptions($options);

$this->setSaveHandler($handler);
}

Expand All @@ -118,7 +117,7 @@ public function start()
}

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

// start the session
Expand Down Expand Up @@ -225,7 +224,7 @@ public function getBag($name)
*
* @see http://php.net/session.configuration
*/
protected function setOptions(array $options)
public function setOptions(array $options)
{
$this->options = $options;

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

foreach ($defaults as $key => $value) {
Expand Down Expand Up @@ -272,14 +270,14 @@ protected function setOptions(array $options)
* @see http://php.net/sessionhandlerinterface
* @see http://php.net/sessionhandler
*
* @param object $saveHandler
* @param object $saveHandler Default null means NativeProxy.
*/
public function setSaveHandler($saveHandler)
public function setSaveHandler($saveHandler = null)
{
// Wrap $saveHandler in proxy
if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
$saveHandler = new SessionHandlerProxy($saveHandler);
} else {
} elseif (!$saveHandler instanceof AbstractProxy) {
$saveHandler = new NativeProxy($saveHandler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,44 @@

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

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

}

class ConcreteSessionHandlerInterfaceProxy extends AbstractProxy implements \SessionHandlerInterface
{
public function open($savePath, $sessionName)
{
}

public function close()
{
}

public function read($id)
{
}

public function write($id, $data)
{
}

public function destroy($id)
{
}

public function gc($maxlifetime)
{
}
}

/**
* Test class for AbstractProxy.
*
* @runTestsInSeparateProcesses
* @author Drak <[email protected]>
*/
class AbstractProxyTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -23,7 +52,7 @@ class AbstractProxyTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$this->proxy = new ConcreteProxy;
$this->proxy = new ConcreteProxy();
}

protected function tearDown()
Expand All @@ -33,37 +62,31 @@ protected function tearDown()

public function testGetSaveHandlerName()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$this->assertNull($this->proxy->getSaveHandlerName());
}

public function testIsSessionHandlerInterface()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$this->assertFalse($this->proxy->isSessionHandlerInterface());
$sh = new ConcreteSessionHandlerInterfaceProxy();
$this->assertTrue($sh->isSessionHandlerInterface());
}

public function testIsWrapper()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$this->assertFalse($this->proxy->isWrapper());
}

public function testIsActive()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$this->assertFalse($this->proxy->isActive());
}

public function testSetActive()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$this->proxy->setActive(true);
$this->assertTrue($this->proxy->isActive());
$this->proxy->setActive(false);
$this->assertFalse($this->proxy->isActive());
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,20 @@
/**
* Test class for NativeProxy.
*
* @runTestsInSeparateProcesses
* @author Drak <[email protected]>
*/
class NativeProxyPHP53Test extends \PHPUnit_Framework_TestCase
class NativeProxyTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (version_compare(phpversion(), '5.4.0', '>=')) {
$this->markTestSkipped('Test skipped, only for PHP 5.3');
}
}

public function testIsWrapper()
{
$proxy = new NativeProxy(new NativeFileSessionHandler());
$proxy = new NativeProxy();
$this->assertFalse($proxy->isWrapper());
}

public function testGetSaveHandlerName()
{
$name = ini_get('session.save_handler');
$proxy = new NativeProxy(new NativeFileSessionHandler());
$proxy = new NativeProxy();
$this->assertEquals($name, $proxy->getSaveHandlerName());
}
}
Loading

0 comments on commit cb873b2

Please sign in to comment.