merged branch willdurand/fix-session-bc (PR #4114)

Commits
-------

6756f28 [Session] Fixed Backward Compatibility issue with getFlashes()

Discussion
----------

[Session] Fixed Backward Compatibility issue with getFlashes()

---------------------------------------------------------------------------

by fabpot at 2012-04-25T22:35:42Z

ping @drak

---------------------------------------------------------------------------

by willdurand at 2012-04-25T22:37:01Z

By the way, I had this issue on a real application I upgraded from Symfony2 2.0.x to 2.1 (and written by @Seldaek)

The code looks like:

``` php
<?php
// in a controller

$this->session->setFlash('foo', array(
    'code' => 'success',
    'message' => 'lalala',
    'params' => array())
);
```

---------------------------------------------------------------------------

by Seldaek at 2012-04-26T07:25:03Z

Yup, to be fair in retrospective maybe that should have been translated in the controller directly (that's why it had message + params as an array), but this is code that predates 2.0 by at least six months, so it was obviously not clear what best practices were. Anyway it seems it can be fixed without much harm, so for the sake of safety and because I may not be the only crazy person having done this, it'd be good to fix IMO.
This commit is contained in:
Fabien Potencier 2012-04-26 10:18:31 +02:00
commit 3a6ec029c7
2 changed files with 20 additions and 1 deletions

View File

@ -256,7 +256,11 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
$return = array();
if ($all) {
foreach ($all as $name => $array) {
$return[$name] = reset($array);
if (is_numeric(key($array))) {
$return[$name] = reset($array);
} else {
$return[$name] = $array;
}
}
}

View File

@ -183,6 +183,21 @@ class SessionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('notice' => 'foo'), $this->session->getFlashes());
}
public function testGetFlashesWithArray()
{
$array = array('notice' => 'hello', 'error' => 'none');
$this->assertEquals(array(), $this->session->getFlashes());
$this->session->setFlash('foo', $array);
$this->assertEquals(array('foo' => $array), $this->session->getFlashes());
$this->assertEquals(array(), $this->session->getFlashes());
$array = array('hello', 'foo');
$this->assertEquals(array(), $this->session->getFlashes());
$this->session->setFlash('foo', $array);
$this->assertEquals(array('foo' => 'hello'), $this->session->getFlashes());
$this->assertEquals(array(), $this->session->getFlashes());
}
public function testGetSetFlash()
{
$this->assertNull($this->session->getFlash('notice'));