[HttpFoundation] Reworked flashes to maintain same behaviour as in Symfony 2.0

This commit is contained in:
Drak 2012-02-02 16:52:02 +05:45
parent f98f9ae8ff
commit 398acc9e9f
23 changed files with 269 additions and 698 deletions

View File

@ -235,13 +235,11 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* Added `FlashBag` (default). Flashes expire when retrieved by `popFlashes()`.
This makes the implementation ESI compatible.
* Added `AutoExpireFlashBag` to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring
after one page page load. Messages must be retrived by `popFlashes()` but will expire regardless of
being retrieved or not, which retains th old behaviour.
* [BC BREAK] Removed the following methods from the Session class: `close()`, `setFlash()`, `hasFlash()`,
and `removeFlash()` and added new methods. Use `addFlashes()` to add new flash messages.
`getFlashes()` now returns and array of flash messages.
after one page page load. Messages must be retrived by `pop()` or `popAll()`.
* [BC BREAK] Removed the following methods from the Session class: `close()`, `setFlash()`, `setFlashes()`
`getFlash()`, `hasFlash()`, andd `removeFlash()`. `getFlashes() returns a `FlashBagInterface`.
* `Session->clear()` now only clears session attributes as before it cleared flash messages and
attributes. `Session->clearAllFlashes()` clears flashes now.
attributes. `Session->getFlashes()->popAll()` clears flashes now.
* Added `AbstractSessionStorage` base class for session storage drivers.
* Added `SessionSaveHandler` interface which storage drivers should implement after inheriting from
`AbstractSessionStorage` when writing custom session save handlers.

View File

@ -32,17 +32,17 @@ UPGRADE FROM 2.0 to 2.1
Retrieving the locale from a Twig template:
Before: `{{ app.request.session.locale }}` or `{{ app.session.locale }}`
Before: `{{ app.request.session.locale }}` or `{{ app.session.locale }}`
After: `{{ app.request.locale }}`
Retrieving the locale from a PHP template:
Before: `$view['session']->getLocale()`
Before: `$view['session']->getLocale()`
After: `$view['request']->getLocale()`
Retrieving the locale from PHP code:
Before: `$session->getLocale()`
Before: `$session->getLocale()`
After: `$request->getLocale()`
* Method `equals` of `Symfony\Component\Security\Core\User\UserInterface` has
@ -250,20 +250,18 @@ UPGRADE FROM 2.0 to 2.1
After (PHP):
<?php foreach ($view['session']->popFlashes('notice') as $notice): ?>
<div class="flash-notice">
<?php echo $notice; ?>
</div>
<?php endforeach; ?>
<?php if ($view['session']->getFlashes()->has('notice')): ?>
<div class="flash-notice">
<?php echo $view['session']->getFlashes()->pop('notice') ?>
</div>
<?php endif; ?>
If You wanted to process all flash types you could also make use of the `popAllFlashes()` API:
<?php foreach ($view['session']->popAllFlashes() as $type => $flashes): ?>
<?php foreach ($flashes as $flash): ?>
<div class="flash-$type">
<?php echo $flash; ?>
</div>
<?php endforeach; ?>
<?php foreach ($view['session']->getFlashes()->all() as $type => $flash): ?>
<div class="flash-$type">
<?php echo $flash; ?>
</div>
<?php endforeach; ?>
.. note::
@ -283,20 +281,18 @@ UPGRADE FROM 2.0 to 2.1
After (Twig):
{% for flashMessage in app.session.popFlashes('notice') %}
{% if app.session.getFlashes.has('notice') %}
<div class="flash-notice">
{{ flashMessage }}
{{ app.session.getFlashes.pop('notice') }}
</div>
{% endforeach %}
{% endif %}
Again you can process all flash messages in one go with
{% for type, flashMessages in app.session.popAllFlashes() %}
{% for flashMessage in flashMessages) %}
<div class="flash-{{ type }}">
{{ flashMessage }}
</div>
{% endforeach %}
{% for type, flashMessage in app.session.getFlashes.popAll() %}
<div class="flash-{{ type }}">
{{ flashMessage }}
</div>
{% endforeach %}
.. note::
@ -306,9 +302,10 @@ UPGRADE FROM 2.0 to 2.1
* Session object
The methods, `setFlash()`, `hasFlash()`, and `removeFlash()` have been removed from the `Session`
object. You may use `addFlash()` to add flashes. `getFlashes()`, now returns an array. Use
`popFlashes()` to get flashes for display, or `popAllFlashes()` to process all flashes in on go.
The methods, `setFlash()`, `setFlashes()`, `getFlash()`, `hasFlash()`, and `removeFlash()`
have been removed from the `Session` object. `getFlashes()` now returns a `FlashBagInterface`.
Flashes should be popped off the stack using `getFlashes()->pop()` or `getFlashes()->popAll()`
to get all flashes in one go.
* Session storage drivers

View File

@ -47,19 +47,19 @@ class SessionHelper extends Helper
return $this->session->get($name, $default);
}
public function getFlashes($type)
public function getFlash($type)
{
return $this->session->getFlashes($type);
return $this->session->getFlashes()->get($type);
}
public function getAllFlashes()
public function getFlashes()
{
return $this->session->getAllFlashes();
return $this->session->getFlashes()->all();
}
public function hasFlashes($type)
public function hasFlash($type)
{
return $this->session->hasFlashes($type);
return $this->session->getFlashes()->has($type);
}
/**

View File

@ -51,7 +51,7 @@ class SessionController extends ContainerAware
{
$request = $this->container->get('request');
$session = $request->getSession();
$session->addFlash($message, 'notice');
$session->getFlashes()->set('notice', $message);
return new RedirectResponse($this->container->get('router')->generate('session_showflash'));
}
@ -61,8 +61,8 @@ class SessionController extends ContainerAware
$request = $this->container->get('request');
$session = $request->getSession();
if ($session->hasFlashes('notice')) {
list($output) = $session->popFlashes('notice');
if ($session->getFlashes()->has('notice')) {
$output = $session->getFlashes()->pop('notice');
} else {
$output = 'No flash was set.';
}

View File

@ -26,9 +26,9 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase
{
$this->request = new Request();
$session = new Session(new MockArraySessionStorage(new AttributeBag(), new FlashBag()));
$session = new Session(new MockArraySessionStorage());
$session->set('foobar', 'bar');
$session->addFlash('bar', FlashBag::NOTICE);
$session->getFlashes()->set(FlashBag::NOTICE, 'bar');
$this->request->setSession($session);
}
@ -42,11 +42,11 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase
{
$helper = new SessionHelper($this->request);
$this->assertTrue($helper->hasFlashes(FlashBag::NOTICE));
$this->assertTrue($helper->hasFlash(FlashBag::NOTICE));
$this->assertEquals(array('bar'), $helper->getFlashes(FlashBag::NOTICE));
$this->assertEquals('bar', $helper->getFlash(FlashBag::NOTICE));
$this->assertEquals(array(FlashBag::NOTICE => array('bar')), $helper->getAllFlashes());
$this->assertEquals(array(FlashBag::NOTICE => 'bar'), $helper->getFlashes());
}
public function testGet()

View File

@ -19,8 +19,6 @@ use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage;
use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\AttributeBag;
class PhpEngineTest extends TestCase
{
@ -66,7 +64,7 @@ class PhpEngineTest extends TestCase
{
$container = new Container();
$request = new Request();
$session = new Session(new MockArraySessionStorage(new AttributeBag(), new FlashBag()));
$session = new Session(new MockArraySessionStorage());
$request->setSession($session);
$container->set('request', $request);

View File

@ -57,36 +57,46 @@ class AutoExpireFlashBag implements FlashBagInterface
$this->flashes['new'] = array();
}
/**
* {@inheritdoc}
*/
public function add($message, $type = self::NOTICE)
{
$this->flashes['new'][$type][] = $message;
}
/**
* {@inheritdoc}
*/
public function get($type)
{
if (!$this->has($type)) {
return array();
throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type));
}
return $this->flashes['display'][$type];
}
/**
* {@inheritdoc}
*/
public function all()
{
return array_key_exists('display', $this->flashes) ? (array)$this->flashes['display'] : array();
}
/**
* {@inheritdoc}
*/
public function pop($type)
{
if (!$this->has($type)) {
return array();
throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type));
}
return $this->clear($type);
$return = null;
if (isset($this->flashes['new'][$type])) {
unset($this->flashes['new'][$type]);
}
if (isset($this->flashes['display'][$type])) {
$return = $this->flashes['display'][$type];
unset($this->flashes['display'][$type]);
}
return $return;
}
/**
@ -94,15 +104,26 @@ class AutoExpireFlashBag implements FlashBagInterface
*/
public function popAll()
{
return $this->clearAll();
$return = $this->flashes['display'];
$this->flashes = array('new' => array(), 'display' => array());
return $return;
}
/**
* {@inheritdoc}
*/
public function set($type, array $array)
public function setAll(array $messages)
{
$this->flashes['new'][$type] = $array;
$this->flashes['new'] = $messages;
}
/**
* {@inheritdoc}
*/
public function set($type, $message)
{
$this->flashes['new'][$type] = $message;
}
/**
@ -121,43 +142,6 @@ class AutoExpireFlashBag implements FlashBagInterface
return array_keys($this->flashes['display']);
}
/**
* {@inheritdoc}
*/
public function all()
{
return array_key_exists('display', $this->flashes) ? (array)$this->flashes['display'] : array();
}
/**
* {@inheritdoc}
*/
public function clear($type)
{
$return = array();
if (isset($this->flashes['new'][$type])) {
unset($this->flashes['new'][$type]);
}
if (isset($this->flashes['display'][$type])) {
$return = $this->flashes['display'][$type];
unset($this->flashes['display'][$type]);
}
return $return;
}
/**
* {@inheritdoc}
*/
public function clearAll()
{
$return = $this->flashes['display'];
$this->flashes = array('new' => array(), 'display' => array());
return $return;
}
/**
* {@inheritdoc}
*/

View File

@ -50,36 +50,47 @@ class FlashBag implements FlashBagInterface
$this->flashes = &$flashes;
}
/**
* {@inheritdoc}
*/
public function add($message, $type = self::NOTICE)
{
$this->flashes[$type][] = $message;
}
/**
* {@inheritdoc}
*/
public function get($type)
{
if (!$this->has($type)) {
return array();
throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type));
}
return $this->flashes[$type];
}
/**
* {@inheritdoc}
*/
public function set($type, $message)
{
$this->flashes[$type] = $message;
}
/**
* {@inheritdoc}
*/
public function all()
{
return $this->flashes;
}
/**
* {@inheritdoc}
*/
public function pop($type)
{
if (!$this->has($type)) {
return array();
throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type));
}
return $this->clear($type);
$return = $this->get($type);
unset($this->flashes[$type]);
return $return;
}
/**
@ -87,15 +98,18 @@ class FlashBag implements FlashBagInterface
*/
public function popAll()
{
return $this->clearAll();
$return = $this->all();
$this->flashes = array();
return $return;
}
/**
* {@inheritdoc}
*/
public function set($type, array $array)
public function setAll(array $messages)
{
$this->flashes[$type] = $array;
$this->flashes = $messages;
}
/**
@ -114,39 +128,6 @@ class FlashBag implements FlashBagInterface
return array_keys($this->flashes);
}
/**
* {@inheritdoc}
*/
public function all()
{
return $this->flashes;
}
/**
* {@inheritdoc}
*/
public function clear($type)
{
$return = array();
if (isset($this->flashes[$type])) {
$return = $this->flashes[$type];
unset($this->flashes[$type]);
}
return $return;
}
/**
* {@inheritdoc}
*/
public function clearAll()
{
$return = $this->flashes;
$this->flashes = array();
return $return;
}
/**
* {@inheritdoc}
*/

View File

@ -24,47 +24,49 @@ interface FlashBagInterface extends SessionBagInterface
const ERROR = 'error';
/**
* Adds a flash to the stack for a given type.
* Registers a message for a given type.
*
* @param string $message
* @param string $type
* @param string $message
*/
function add($message, $type = self::NOTICE);
function set($type, $message);
/**
* Gets flash messages for a given type.
* Gets flash message for a given type.
*
* @param string $type Message category type.
* @param string $type Message category type.
*
* @return array
* @return string
*/
function get($type);
/**
* Pops and clears flashes from the stack.
* Gets all flash messages.
*
* @return array
*/
function all();
/**
* Pops and clears flash from the stack.
*
* @param string $type
*
* @return array
* @return string
*/
function pop($type);
/**
* Pops all flashes from the stack and clears flashes.
* Pops and clears flashes from the stack.
*
* @param string $type
*
* @return array Empty array, or indexed array of arrays.
* @return array
*/
function popAll();
/**
* Sets an array of flash messages for a given type.
*
* @param string $type
* @param array $array
* Sets all flash messages.
*/
function set($type, array $array);
function setAll(array $messages);
/**
* Has flash messages for a given type?
@ -81,27 +83,4 @@ interface FlashBagInterface extends SessionBagInterface
* @return array
*/
function keys();
/**
* Gets all flash messages.
*
* @return array
*/
function all();
/**
* Clears flash messages for a given type.
*
* @param string $type
*
* @return array Returns an array of what was just cleared.
*/
function clear($type);
/**
* Clears all flash messages.
*
* @return array Empty array or indexed arrays or array if none.
*/
function clearAll();
}

View File

@ -218,113 +218,13 @@ class Session implements SessionInterface
$this->storage = $storage;
}
/**
* Adds a flash to the stack for a given type.
*
* @param string $message
* @param string $type
*/
public function addFlash($message, $type = FlashBagInterface::NOTICE)
{
$this->storage->getFlashes()->add($message, $type);
}
/**
* Gets flash messages for a given type.
*
* @param string $type Message category type.
*
* @return array
*/
public function getFlashes($type = FlashBagInterface::NOTICE)
{
return $this->storage->getFlashes()->get($type);
}
/**
* Pops flash messages off th stack for a given type.
*
* @param string $type Message category type.
*
* @return array
*/
public function popFlashes($type = FlashBagInterface::NOTICE)
{
return $this->storage->getFlashes()->pop($type);
}
/**
* Pop all flash messages from the stack.
*
* @return array Empty array or indexed array of arrays.
*/
public function popAllFlashes()
{
return $this->storage->getFlashes()->popAll();
}
/**
* Sets an array of flash messages for a given type.
*
* @param string $type
* @param array $array
*/
public function setFlashes($type, array $array)
{
$this->storage->getFlashes()->set($type, $array);
}
/**
* Has flash messages for a given type?
*
* @param string $type
*
* @return boolean
*/
public function hasFlashes($type)
{
return $this->storage->getFlashes()->has($type);
}
/**
* Returns a list of all defined types.
*
* @return array
*/
public function getFlashKeys()
{
return $this->storage->getFlashes()->keys();
}
/**
* Gets all flash messages.
*
* @return array
* @return FlashBagInterface
*/
public function getAllFlashes()
public function getFlashes()
{
return $this->storage->getFlashes()->all();
}
/**
* Clears flash messages for a given type.
*
* @param string $type
*
* @return array Returns an array of what was just cleared.
*/
public function clearFlashes($type)
{
return $this->storage->getFlashes()->clear($type);
}
/**
* Clears all flash messages.
*
* @return array Empty array or indexed arrays or array if none.
*/
public function clearAllFlashes()
{
return $this->storage->getFlashes()->clearAll();
return $this->storage->getFlashes();
}
}

View File

@ -57,82 +57,9 @@ interface SessionInterface extends AttributeInterface, \Serializable
function save();
/**
* Adds a flash to the stack for a given type.
* Gets the flashbag interface.
*
* @param string $message
* @param string $type
* @return FlashBagInterface
*/
function addFlash($message, $type = FlashBagInterface::NOTICE);
/**
* Gets flash messages for a given type.
*
* @param string $type Message category type.
*
* @return array
*/
function getFlashes($type = FlashBagInterface::NOTICE);
/**
* Pops flash messages off th stack for a given type.
*
* @param string $type Message category type.
*
* @return array
*/
function popFlashes($type = FlashBagInterface::NOTICE);
/**
* Pop all flash messages from the stack.
*
* @return array Empty array or indexed array of arrays.
*/
function popAllFlashes();
/**
* Sets an array of flash messages for a given type.
*
* @param string $type
* @param array $array
*/
function setFlashes($type, array $array);
/**
* Has flash messages for a given type?
*
* @param string $type
*
* @return boolean
*/
function hasFlashes($type);
/**
* Returns a list of all defined types.
*
* @return array
*/
function getFlashKeys();
/**
* Gets all flash messages.
*
* @return array
*/
function getAllFlashes();
/**
* Clears flash messages for a given type.
*
* @param string $type
*
* @return array Returns an array of what was just cleared.
*/
function clearFlashes($type);
/**
* Clears all flash messages.
*
* @return array Array of arrays or array if none.
*/
function clearAllFlashes();
function getFlashes();
}

View File

@ -195,7 +195,7 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
{
// clear out the bags
$this->attributeBag->clear();
$this->flashBag->clearAll();
$this->flashBag->popAll();
// clear out the session
$_SESSION = array();
@ -297,12 +297,12 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
// so long as ini_set() is called before the session is started.
if ($this instanceof SessionSaveHandlerInterface) {
session_set_save_handler(
array($this, 'sessionOpen'),
array($this, 'sessionClose'),
array($this, 'sessionRead'),
array($this, 'sessionWrite'),
array($this, 'sessionDestroy'),
array($this, 'sessionGc')
array($this, 'openSession'),
array($this, 'closeSession'),
array($this, 'readSession'),
array($this, 'writeSession'),
array($this, 'destroySession'),
array($this, 'gcSession')
);
}
}

View File

@ -89,7 +89,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
/**
* {@inheritdoc}
*/
public function sessionOpen($savePath, $sessionName)
public function openSession($savePath, $sessionName)
{
foreach ($this->memcacheOptions['serverpool'] as $server) {
$this->addServer($server);
@ -101,7 +101,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
/**
* {@inheritdoc}
*/
public function sessionClose()
public function closeSession()
{
return $this->memcache->close();
}
@ -109,17 +109,15 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
/**
* {@inheritdoc}
*/
public function sessionRead($sessionId)
public function readSession($sessionId)
{
return $this->memcache->get($this->prefix.$sessionId) ?: '';
return ($result) ? $result : '';
}
/**
* {@inheritdoc}
*/
public function sessionWrite($sessionId, $data)
public function writeSession($sessionId, $data)
{
return $this->memcache->set($this->prefix.$sessionId, $data, $this->memcacheOptions['expiretime']);
}
@ -127,7 +125,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
/**
* {@inheritdoc}
*/
public function sessionDestroy($sessionId)
public function destroySession($sessionId)
{
return $this->memcache->delete($this->prefix.$sessionId);
}
@ -135,7 +133,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
/**
* {@inheritdoc}
*/
public function sessionGc($lifetime)
public function gcSession($lifetime)
{
// not required here because memcache will auto expire the records anyhow.
return true;

View File

@ -72,7 +72,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
/**
* {@inheritdoc}
*/
public function sessionOpen($savePath, $sessionName)
public function openSession($savePath, $sessionName)
{
foreach ($this->memcachedOptions['serverpool'] as $server) {
$this->addServer($server);
@ -86,7 +86,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
*
* @return boolean
*/
public function sessionClose()
public function closeSession()
{
return $this->memcached->close();
}
@ -94,7 +94,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
/**
* {@inheritdoc}
*/
public function sessionRead($sessionId)
public function readSession($sessionId)
{
return $this->memcached->get($this->prefix.$sessionId) ?: '';
}
@ -102,7 +102,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
/**
* {@inheritdoc}
*/
public function sessionWrite($sessionId, $data)
public function writeSession($sessionId, $data)
{
return $this->memcached->set($this->prefix.$sessionId, $data, false, $this->memcachedOptions['expiretime']);
}
@ -110,7 +110,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
/**
* {@inheritdoc}
*/
public function sessionDestroy($sessionId)
public function destroySession($sessionId)
{
return $this->memcached->delete($this->prefix.$sessionId);
}
@ -118,7 +118,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
/**
* {@inheritdoc}
*/
public function sessionGc($lifetime)
public function gcSession($lifetime)
{
// not required here because memcached will auto expire the records anyhow.
return true;

View File

@ -28,7 +28,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa
/**
* {@inheritdoc}
*/
public function sessionOpen($savePath, $sessionName)
public function openSession($savePath, $sessionName)
{
return true;
}
@ -38,7 +38,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa
*
* @return boolean
*/
public function sessionClose()
public function closeSession()
{
return true;
}
@ -46,7 +46,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa
/**
* {@inheritdoc}
*/
public function sessionRead($sessionId)
public function readSession($sessionId)
{
return '';
}
@ -54,7 +54,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa
/**
* {@inheritdoc}
*/
public function sessionWrite($sessionId, $data)
public function writeSession($sessionId, $data)
{
return true;
}
@ -62,7 +62,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa
/**
* {@inheritdoc}
*/
public function sessionDestroy($sessionId)
public function destroySession($sessionId)
{
return true;
}
@ -70,7 +70,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa
/**
* {@inheritdoc}
*/
public function sessionGc($lifetime)
public function gcSession($lifetime)
{
return true;
}

View File

@ -70,7 +70,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
/**
* {@inheritdoc}
*/
public function sessionOpen($path = null, $name = null)
public function openSession($path = null, $name = null)
{
return true;
}
@ -78,7 +78,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
/**
* {@inheritdoc}
*/
public function sessionClose()
public function closeSession()
{
return true;
}
@ -88,7 +88,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
*
* @throws \RuntimeException If the session cannot be destroyed
*/
public function sessionDestroy($id)
public function destroySession($id)
{
// get table/column
$dbTable = $this->dbOptions['db_table'];
@ -113,7 +113,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
*
* @throws \RuntimeException If any old sessions cannot be cleaned
*/
public function sessionGc($lifetime)
public function gcSession($lifetime)
{
// get table/column
$dbTable = $this->dbOptions['db_table'];
@ -138,7 +138,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
*
* @throws \RuntimeException If the session cannot be read
*/
public function sessionRead($id)
public function readSession($id)
{
// get table/columns
$dbTable = $this->dbOptions['db_table'];
@ -174,7 +174,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
*
* @throws \RuntimeException If the session data cannot be written
*/
public function sessionWrite($id, $data)
public function writeSession($id, $data)
{
// get table/column
$dbTable = $this->dbOptions['db_table'];

View File

@ -66,7 +66,7 @@ interface SessionSaveHandlerInterface
*
* @return boolean
*/
function sessionOpen($savePath, $sessionName);
function openSession($savePath, $sessionName);
/**
* Close session.
@ -75,7 +75,7 @@ interface SessionSaveHandlerInterface
*
* @return boolean
*/
function sessionClose();
function closeSession();
/**
* Read session.
@ -98,7 +98,7 @@ interface SessionSaveHandlerInterface
*
* @return string String as stored in persistent storage or empty string in all other cases.
*/
function sessionRead($sessionId);
function readSession($sessionId);
/**
* Commit session to storage.
@ -120,7 +120,7 @@ interface SessionSaveHandlerInterface
*
* @return boolean
*/
function sessionWrite($sessionId, $data);
function writeSession($sessionId, $data);
/**
* Destroys this session.
@ -137,7 +137,7 @@ interface SessionSaveHandlerInterface
*
* @return boolean
*/
function sessionDestroy($sessionId);
function destroySession($sessionId);
/**
* Garbage collection for storage.
@ -153,5 +153,5 @@ interface SessionSaveHandlerInterface
*
* @return boolean
*/
function sessionGc($lifetime);
function gcSession($lifetime);
}

View File

@ -35,7 +35,7 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase
{
parent::setUp();
$this->bag = new FlashBag();
$this->array = array('new' => array(FlashBag::NOTICE => array('A previous flash message')));
$this->array = array('new' => array(FlashBag::NOTICE => 'A previous flash message'));
$this->bag->initialize($this->array);
}
@ -48,36 +48,36 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase
public function testInitialize()
{
$bag = new FlashBag();
$array = array('new' => array(FlashBag::NOTICE => array('A previous flash message')));
$array = array('new' => array(FlashBag::NOTICE => 'A previous flash message'));
$bag->initialize($array);
$this->assertEquals(array('A previous flash message'), $bag->get(FlashBag::NOTICE));
$this->assertEquals('A previous flash message', $bag->get(FlashBag::NOTICE));
$array = array('new' => array(
FlashBag::NOTICE => array('Something else'),
FlashBag::ERROR => array('a', 'b'),
FlashBag::NOTICE => 'Something else',
FlashBag::ERROR => 'a',
));
$bag->initialize($array);
$this->assertEquals(array('Something else'), $bag->get(FlashBag::NOTICE));
$this->assertEquals(array('a', 'b'), $bag->get(FlashBag::ERROR));
}
public function testAdd()
{
$this->bag->add('Something new', FlashBag::NOTICE);
$this->bag->add('Smile, it might work next time', FlashBag::ERROR);
$this->assertEquals(array('A previous flash message'), $this->bag->get(FlashBag::NOTICE));
$this->assertEquals(array(), $this->bag->get(FlashBag::ERROR));
$this->assertEquals('Something else', $bag->get(FlashBag::NOTICE));
$this->assertEquals('a', $bag->get(FlashBag::ERROR));
}
public function testGet()
{
$this->assertEquals(array('A previous flash message'), $this->bag->get(FlashBag::NOTICE));
$this->assertEquals(array(), $this->bag->get('non_existing_type'));
$this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE));
$this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testGetException()
{
$this->bag->get('non_existing_type');
}
public function testSet()
{
$this->bag->set(FlashBag::NOTICE, array('Foo', 'Bar'));
$this->assertNotEquals(array('Foo', 'Bar'), $this->bag->get(FlashBag::NOTICE));
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->assertNotEquals('Foo', $this->bag->get(FlashBag::NOTICE));
}
public function testHas()
@ -95,58 +95,45 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase
{
$array = array(
'new' => array(
FlashBag::NOTICE => array('Foo'),
FlashBag::ERROR => array('Bar'),
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
),
);
$this->bag->initialize($array);
$this->assertEquals(array(
FlashBag::NOTICE => array('Foo'),
FlashBag::ERROR => array('Bar'),
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
), $this->bag->all()
);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testPop()
{
$this->assertEquals(array('A previous flash message'), $this->bag->pop(FlashBag::NOTICE));
$this->assertEquals(array(), $this->bag->pop(FlashBag::NOTICE));
$this->assertEquals(array(), $this->bag->pop('non_existing_type'));
$this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE));
$this->bag->pop(FlashBag::NOTICE);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testPopException()
{
$this->bag->pop('non_existing_type');
}
public function testPopAll()
{
$this->bag->set(FlashBag::NOTICE, array('Foo'));
$this->bag->set(FlashBag::ERROR, array('Bar'));
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->bag->set(FlashBag::ERROR, 'Bar');
$this->assertEquals(array(
FlashBag::NOTICE => array('A previous flash message'),
FlashBag::NOTICE => 'A previous flash message',
), $this->bag->popAll()
);
$this->assertEquals(array(), $this->bag->popAll());
}
public function testClear()
{
$this->assertTrue($this->bag->has(FlashBag::NOTICE));
$this->assertEquals(array('A previous flash message'), $this->bag->clear(FlashBag::NOTICE));
$this->assertEquals(array(), $this->bag->clear(FlashBag::NOTICE));
$this->assertFalse($this->bag->has(FlashBag::NOTICE));
}
public function testClearAll()
{
$this->assertTrue($this->bag->has(FlashBag::NOTICE));
$this->bag->add('Smile, it might work next time', FlashBag::ERROR);
$this->assertFalse($this->bag->has(FlashBag::ERROR));
$this->assertEquals(array(
FlashBag::NOTICE => array('A previous flash message'),
), $this->bag->clearAll()
);
$this->assertEquals(array(), $this->bag->clearAll());
$this->assertFalse($this->bag->has(FlashBag::NOTICE));
$this->assertFalse($this->bag->has(FlashBag::ERROR));
}
}

View File

@ -35,7 +35,7 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase
{
parent::setUp();
$this->bag = new FlashBag();
$this->array = array(FlashBag::NOTICE => array('A previous flash message'));
$this->array = array(FlashBag::NOTICE => 'A previous flash message');
$this->bag->initialize($this->array);
}
@ -55,43 +55,58 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($array, $bag->all());
}
public function testAdd()
{
$this->bag->add('Something new', FlashBag::NOTICE);
$this->bag->add('Smile, it might work next time', FlashBag::ERROR);
$this->assertEquals(array('A previous flash message', 'Something new'), $this->bag->get(FlashBag::NOTICE));
$this->assertEquals(array('Smile, it might work next time'), $this->bag->get(FlashBag::ERROR));
}
public function testGet()
{
$this->assertEquals(array('A previous flash message'), $this->bag->get(FlashBag::NOTICE));
$this->assertEquals(array(), $this->bag->get('non_existing_type'));
$this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testGetException()
{
$this->bag->get('non_existing_type');
}
public function testPop()
{
$this->assertEquals(array('A previous flash message'), $this->bag->pop(FlashBag::NOTICE));
$this->assertEquals(array(), $this->bag->pop(FlashBag::NOTICE));
$this->assertEquals(array(), $this->bag->pop('non_existing_type'));
$this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testPopException()
{
$this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE));
$this->bag->pop(FlashBag::NOTICE);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testPopExceptionNotExisting()
{
$this->bag->pop('non_existing_type');
}
public function testPopAll()
{
$this->bag->set(FlashBag::NOTICE, array('Foo'));
$this->bag->set(FlashBag::ERROR, array('Bar'));
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->bag->set(FlashBag::ERROR, 'Bar');
$this->assertEquals(array(
FlashBag::NOTICE => array('Foo'),
FlashBag::ERROR => array('Bar')), $this->bag->popAll()
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar'), $this->bag->popAll()
);
$this->assertEquals(array(), $this->bag->popAll());
}
public function testSet()
public function testset()
{
$this->bag->set(FlashBag::NOTICE, array('Foo', 'Bar'));
$this->assertEquals(array('Foo', 'Bar'), $this->bag->get(FlashBag::NOTICE));
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->bag->set(FlashBag::NOTICE, 'Bar');
$this->assertEquals('Bar', $this->bag->get(FlashBag::NOTICE));
}
public function testHas()
@ -107,42 +122,19 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase
public function testAll()
{
$this->bag->set(FlashBag::NOTICE, array('Foo'));
$this->bag->set(FlashBag::ERROR, array('Bar'));
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->bag->set(FlashBag::ERROR, 'Bar');
$this->assertEquals(array(
FlashBag::NOTICE => array('Foo'),
FlashBag::ERROR => array('Bar')), $this->bag->all()
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
), $this->bag->all()
);
$this->assertTrue($this->bag->has(FlashBag::NOTICE));
$this->assertTrue($this->bag->has(FlashBag::ERROR));
$this->assertEquals(array(
FlashBag::NOTICE => array('Foo'),
FlashBag::ERROR => array('Bar'),
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
), $this->bag->all()
);
}
public function testClear()
{
$this->assertTrue($this->bag->has(FlashBag::NOTICE));
$this->assertEquals(array('A previous flash message'), $this->bag->clear(FlashBag::NOTICE));
$this->assertEquals(array(), $this->bag->clear(FlashBag::NOTICE));
$this->assertFalse($this->bag->has(FlashBag::NOTICE));
}
public function testClearAll()
{
$this->assertTrue($this->bag->has(FlashBag::NOTICE));
$this->bag->add('Smile, it might work next time', FlashBag::ERROR);
$this->assertTrue($this->bag->has(FlashBag::ERROR));
$this->assertEquals(array(
FlashBag::NOTICE => array('A previous flash message'),
FlashBag::ERROR => array('Smile, it might work next time'),
), $this->bag->clearAll()
);
$this->assertEquals(array(), $this->bag->clearAll());
$this->assertFalse($this->bag->has(FlashBag::NOTICE));
$this->assertFalse($this->bag->has(FlashBag::ERROR));
}
}

View File

@ -2,14 +2,13 @@
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\SessionStorage\AbstractSessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\SessionSaveHandlerInterface;
/**
* Turn AbstractSessionStorage into something concrete because
* certain mocking features are broken in PHPUnit 3.6.4
* certain mocking features are broken in PHPUnit-Mock-Objects < 1.1.2
* @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/73
*/
class ConcreteSessionStorage extends AbstractSessionStorage
{
@ -17,27 +16,27 @@ class ConcreteSessionStorage extends AbstractSessionStorage
class CustomHandlerSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
{
public function sessionOpen($path, $id)
public function openSession($path, $id)
{
}
public function sessionClose()
public function closeSession()
{
}
public function sessionRead($id)
public function readSession($id)
{
}
public function sessionWrite($id, $data)
public function writeSession($id, $data)
{
}
public function sessionDestroy($id)
public function destroySession($id)
{
}
public function sessionGc($lifetime)
public function gcSession($lifetime)
{
}
}
@ -58,7 +57,7 @@ class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase
*/
protected function getStorage()
{
return new CustomHandlerSessionStorage(new AttributeBag(), new FlashBag());
return new CustomHandlerSessionStorage();
}
public function testGetFlashBag()
@ -106,13 +105,13 @@ class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase
public function testCustomSaveHandlers()
{
$storage = new CustomHandlerSessionStorage(new AttributeBag(), new FlashBag());
$storage = new CustomHandlerSessionStorage();
$this->assertEquals('user', ini_get('session.save_handler'));
}
public function testNativeSaveHandlers()
{
$storage = new ConcreteSessionStorage(new AttributeBag(), new FlashBag());
$storage = new ConcreteSessionStorage();
$this->assertNotEquals('user', ini_get('session.save_handler'));
}
}

View File

@ -3,10 +3,6 @@
namespace Symfony\Test\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\MockFileSessionStorage;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
/**
* Test class for MockFileSessionStorage.
@ -74,14 +70,14 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
$this->assertNotEquals('108', $this->storage->getAttributes()->get('new'));
$this->assertFalse($this->storage->getFlashes()->has('newkey'));
$this->storage->getAttributes()->set('new', '108');
$this->storage->getFlashes()->add('test', 'newkey');
$this->storage->getFlashes()->set('newkey', 'test');
$this->storage->save();
$storage = $this->getStorage();
$storage->start();
$this->assertEquals('108', $storage->getAttributes()->get('new'));
$this->assertTrue($storage->getFlashes()->has('newkey'));
$this->assertEquals(array('test'), $storage->getFlashes()->get('newkey'));
$this->assertEquals('test', $storage->getFlashes()->get('newkey'));
}
public function testMultipleInstances()
@ -98,6 +94,6 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
private function getStorage(array $options = array())
{
return new MockFileSessionStorage($this->sessionDir, $options, new AttributeBag(), new FlashBag());
return new MockFileSessionStorage($this->sessionDir, $options);
}
}

View File

@ -2,8 +2,6 @@
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\NullSessionStorage;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\Session;
/**

View File

@ -123,28 +123,28 @@ class SessionTest extends \PHPUnit_Framework_TestCase
public function testInvalidate()
{
$this->session->set('invalidate', 123);
$this->session->addFlash('OK');
$this->session->getFlashes()->set(FlashBag::NOTICE, 'OK');
$this->session->invalidate();
$this->assertEquals(array(), $this->session->all());
$this->assertEquals(array(), $this->session->getAllFlashes());
$this->assertEquals(array(), $this->session->getFlashes()->all());
}
public function testMigrate()
{
$this->session->set('migrate', 321);
$this->session->addFlash('HI');
$this->session->getFlashes()->set(FlashBag::NOTICE, 'HI');
$this->session->migrate();
$this->assertEquals(321, $this->session->get('migrate'));
$this->assertEquals(array('HI'), $this->session->getFlashes(FlashBag::NOTICE));
$this->assertEquals('HI', $this->session->getFlashes()->get(FlashBag::NOTICE));
}
public function testMigrateDestroy()
{
$this->session->set('migrate', 333);
$this->session->addFlash('Bye');
$this->session->getFlashes()->set(FlashBag::NOTICE, 'Bye');
$this->session->migrate(true);
$this->assertEquals(333, $this->session->get('migrate'));
$this->assertEquals(array('Bye'), $this->session->getFlashes(FlashBag::NOTICE));
$this->assertEquals('Bye', $this->session->getFlashes()->get(FlashBag::NOTICE));
}
public function testSerialize()
@ -176,167 +176,4 @@ class SessionTest extends \PHPUnit_Framework_TestCase
$this->session->start();
$this->assertNotEquals('', $this->session->getId());
}
/**
* @dataProvider provideFlashes
*/
public function testAddFlash($type, $flashes)
{
foreach ($flashes as $message) {
$this->session->addFlash($message, $type);
}
$this->assertEquals($flashes, $this->session->getFlashes($type));
}
/**
* @dataProvider provideFlashes
*/
public function testGetFlashes($type, $flashes)
{
$this->session->setFlashes($type, $flashes);
$this->assertEquals($flashes, $this->session->getFlashes($type));
}
/**
* @dataProvider provideFlashes
*/
public function testPopFlashes($type, $flashes)
{
$this->session->setFlashes($type, $flashes);
$this->assertEquals($flashes, $this->session->popFlashes($type));
$this->assertEquals(array(), $this->session->popFlashes($type));
}
/**
* @dataProvider provideFlashes
*/
public function testPopAllFlashes($type, $flashes)
{
$this->session->setFlashes(FlashBag::NOTICE, array('First', 'Second'));
$this->session->setFlashes(FlashBag::ERROR, array('Third'));
$expected = array(
FlashBag::NOTICE => array('First', 'Second'),
FlashBag::ERROR => array('Third'),
);
$this->assertEquals($expected, $this->session->popAllFlashes());
$this->assertEquals(array(), $this->session->popAllFlashes());
}
public function testSetFlashes()
{
$this->session->setFlashes(FlashBag::NOTICE, array('First', 'Second'));
$this->session->setFlashes(FlashBag::ERROR, array('Third'));
$this->assertEquals(array('First', 'Second'), $this->session->getFlashes(FlashBag::NOTICE, false));
$this->assertEquals(array('Third'), $this->session->getFlashes(FlashBag::ERROR, false));
}
/**
* @dataProvider provideFlashes
*/
public function testHasFlashes($type, $flashes)
{
$this->assertFalse($this->session->hasFlashes($type));
$this->session->setFlashes($type, $flashes);
$this->assertTrue($this->session->hasFlashes($type));
}
/**
* @dataProvider provideFlashes
*/
public function testGetFlashKeys($type, $flashes)
{
$this->assertEquals(array(), $this->session->getFlashKeys());
$this->session->setFlashes($type, $flashes);
$this->assertEquals(array($type), $this->session->getFlashKeys());
}
public function testGetFlashKeysBulk()
{
$this->loadFlashes();
$this->assertEquals(array(
FlashBag::NOTICE, FlashBag::ERROR, FlashBag::WARNING, FlashBag::INFO), $this->session->getFlashKeys()
);
}
public function testGetAllFlashes()
{
$this->assertEquals(array(), $this->session->getAllFlashes());
$this->session->addFlash('a', FlashBag::NOTICE);
$this->assertEquals(array(
FlashBag::NOTICE => array('a')
), $this->session->getAllFlashes()
);
$this->session->addFlash('a', FlashBag::ERROR);
$this->assertEquals(array(
FlashBag::NOTICE => array('a'),
FlashBag::ERROR => array('a'),
), $this->session->getAllFlashes());
$this->session->addFlash('a', FlashBag::WARNING);
$this->assertEquals(array(
FlashBag::NOTICE => array('a'),
FlashBag::ERROR => array('a'),
FlashBag::WARNING => array('a'),
), $this->session->getAllFlashes()
);
$this->session->addFlash('a', FlashBag::INFO);
$this->assertEquals(array(
FlashBag::NOTICE => array('a'),
FlashBag::ERROR => array('a'),
FlashBag::WARNING => array('a'),
FlashBag::INFO => array('a'),
), $this->session->getAllFlashes()
);
$this->assertEquals(array(
FlashBag::NOTICE => array('a'),
FlashBag::ERROR => array('a'),
FlashBag::WARNING => array('a'),
FlashBag::INFO => array('a'),
), $this->session->getAllFlashes()
);
}
/**
* @dataProvider provideFlashes
*/
public function testClearFlashes($type, $flashes)
{
$this->session->setFlashes($type, $flashes);
$this->session->clearFlashes($type);
$this->assertEquals(array(), $this->session->getFlashes($type));
}
public function testClearAllFlashes()
{
$this->loadFlashes();
$this->assertNotEquals(array(), $this->session->getAllFlashes());
$this->session->clearAllFlashes();
$this->assertEquals(array(), $this->session->getAllFlashes());
}
protected function loadFlashes()
{
$flashes = $this->provideFlashes();
foreach ($flashes as $data) {
$this->session->setFlashes($data[0], $data[1]);
}
}
public function provideFlashes()
{
return array(
array(FlashBag::NOTICE, array('a', 'b', 'c')),
array(FlashBag::ERROR, array('d', 'e', 'f')),
array(FlashBag::WARNING, array('g', 'h', 'i')),
array(FlashBag::INFO, array('j', 'k', 'l')),
);
}
}