bug #22368 [Bridge] [Twig] get and flush only flash sessions you need in getFlashes method (jordscream)

This PR was squashed before being merged into the 3.3-dev branch (closes #22368).

Discussion
----------

[Bridge] [Twig] get and flush only flash sessions you need in getFlashes method

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no ?!
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #22359
| License       | MIT
| Doc PR        | symfony/symfony-docs

@xabbuh comments about empty array to return

Commits
-------

33b044a [Bridge] [Twig] get and flush only flash sessions you need in getFlashes method
This commit is contained in:
Nicolas Grekas 2017-04-11 11:36:34 +02:00
commit 35afac00a0
2 changed files with 11 additions and 5 deletions

View File

@ -176,9 +176,7 @@ class AppVariable
$result = array(); $result = array();
foreach ($types as $type) { foreach ($types as $type) {
if ($value = $session->getFlashBag()->get($type)) { $result[$type] = $session->getFlashBag()->get($type);
$result[$type] = $value;
}
} }
return $result; return $result;

View File

@ -190,7 +190,10 @@ class AppVariableTest extends TestCase
$this->assertEquals(array(), $this->appVariable->getFlashes('this-does-not-exist')); $this->assertEquals(array(), $this->appVariable->getFlashes('this-does-not-exist'));
$flashMessages = $this->setFlashMessages(); $flashMessages = $this->setFlashMessages();
$this->assertEquals(array(), $this->appVariable->getFlashes(array('this-does-not-exist'))); $this->assertEquals(
array('this-does-not-exist' => array()),
$this->appVariable->getFlashes(array('this-does-not-exist'))
);
$flashMessages = $this->setFlashMessages(); $flashMessages = $this->setFlashMessages();
$this->assertEquals($flashMessages['notice'], $this->appVariable->getFlashes('notice')); $this->assertEquals($flashMessages['notice'], $this->appVariable->getFlashes('notice'));
@ -203,7 +206,7 @@ class AppVariableTest extends TestCase
$flashMessages = $this->setFlashMessages(); $flashMessages = $this->setFlashMessages();
$this->assertEquals( $this->assertEquals(
array('notice' => $flashMessages['notice']), array('notice' => $flashMessages['notice'], 'this-does-not-exist' => array()),
$this->appVariable->getFlashes(array('notice', 'this-does-not-exist')) $this->appVariable->getFlashes(array('notice', 'this-does-not-exist'))
); );
@ -218,6 +221,11 @@ class AppVariableTest extends TestCase
$this->appVariable->getFlashes(array('warning')), $this->appVariable->getFlashes(array('warning')),
'After getting some flash types (e.g. "notice" and "error"), the rest of flash messages must remain (e.g. "warning").' 'After getting some flash types (e.g. "notice" and "error"), the rest of flash messages must remain (e.g. "warning").'
); );
$this->assertEquals(
array('this-does-not-exist' => array()),
$this->appVariable->getFlashes(array('this-does-not-exist'))
);
} }
protected function setRequestStack($request) protected function setRequestStack($request)