Skip to content

Commit

Permalink
[Twig Bridge] A simpler way to retrieve flash messages
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz authored and fabpot committed Mar 23, 2017
1 parent 69374da commit 5a56b23
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Symfony/Bridge/Twig/AppVariable.php
Expand Up @@ -145,4 +145,35 @@ public function getDebug()

return $this->debug;
}

/**
* Returns some or all the existing flash messages:
* * getFlashes() returns all the flash messages
* * getFlashes('notice') returns a simple array with flash messages of that type
* * getFlashes(array('notice', 'error')) returns a nested array of type => messages.
*
* @return array
*/
public function getFlashes($types = null)
{
// needed to avoid starting the session automatically when looking for flash messages
try {
$session = $this->getSession();
if (null === $session || !$session->isStarted()) {
return array();
}
} catch (\RuntimeException $e) {
return array();
}

if (null === $types || '' === $types || array() === $types) {
return $session->getFlashBag()->all();
}

if (is_string($types)) {
return $session->getFlashBag()->get($types);
}

return array_intersect_key($session->getFlashBag()->all(), array_flip($types));

This comment has been minimized.

Copy link
@alterphp

alterphp Apr 10, 2017

@javiereguiluz @fabpot Calling FlashBag::all() method will empty FlashBag::$flashes. Is it on purpose ?

This comment has been minimized.

Copy link
@javiereguiluz

javiereguiluz Apr 10, 2017

Author Member

Good catch! I think that's a bug. Could you please open an issue in this repo so we don't forget about it? Thanks!

This comment has been minimized.

Copy link
@alterphp

alterphp Apr 10, 2017

sure !

}
}
78 changes: 78 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/AppVariableTest.php
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Session;

class AppVariableTest extends TestCase
Expand Down Expand Up @@ -157,6 +158,62 @@ public function testGetSessionWithRequestStackNotSet()
$this->appVariable->getSession();
}

public function testGetFlashesWithNoRequest()
{
$this->setRequestStack(null);

$this->assertEquals(array(), $this->appVariable->getFlashes());
}

public function testGetFlashesWithNoSessionStarted()
{
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
$request->method('getSession')->willReturn(new Session());

$this->setRequestStack($request);

$this->assertEquals(array(), $this->appVariable->getFlashes());
}

public function testGetFlashes()
{
$flashMessages = $this->setFlashMessages();
$this->assertEquals($flashMessages, $this->appVariable->getFlashes(null));

$flashMessages = $this->setFlashMessages();
$this->assertEquals($flashMessages, $this->appVariable->getFlashes(''));

$flashMessages = $this->setFlashMessages();
$this->assertEquals($flashMessages, $this->appVariable->getFlashes(array()));

$flashMessages = $this->setFlashMessages();
$this->assertEquals(array(), $this->appVariable->getFlashes('this-does-not-exist'));

$flashMessages = $this->setFlashMessages();
$this->assertEquals(array(), $this->appVariable->getFlashes(array('this-does-not-exist')));

$flashMessages = $this->setFlashMessages();
$this->assertEquals($flashMessages['notice'], $this->appVariable->getFlashes('notice'));

$flashMessages = $this->setFlashMessages();
$this->assertEquals(
array('notice' => $flashMessages['notice']),
$this->appVariable->getFlashes(array('notice'))
);

$flashMessages = $this->setFlashMessages();
$this->assertEquals(
array('notice' => $flashMessages['notice']),
$this->appVariable->getFlashes(array('notice', 'this-does-not-exist'))
);

$flashMessages = $this->setFlashMessages();
$this->assertEquals(
array('notice' => $flashMessages['notice'], 'error' => $flashMessages['error']),
$this->appVariable->getFlashes(array('notice', 'error'))
);
}

protected function setRequestStack($request)
{
$requestStackMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
Expand All @@ -175,4 +232,25 @@ protected function setTokenStorage($user)

$token->method('getUser')->willReturn($user);
}

private function setFlashMessages()
{
$flashMessages = array(
'notice' => array('Notice #1 message'),
'warning' => array('Warning #1 message'),
'error' => array('Error #1 message', 'Error #2 message'),
);
$flashBag = new FlashBag();
$flashBag->initialize($flashMessages);

$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->getMock();
$session->method('isStarted')->willReturn(true);
$session->method('getFlashBag')->willReturn($flashBag);

$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
$request->method('getSession')->willReturn($session);
$this->setRequestStack($request);

return $flashMessages;
}
}

0 comments on commit 5a56b23

Please sign in to comment.