Skip to content

[Twig Bridge] A simpler way to retrieve flash messages #21819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
31 changes: 31 additions & 0 deletions src/Symfony/Bridge/Twig/AppVariable.php
Original file line number Diff line number Diff line change
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));
}
}
78 changes: 78 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/AppVariableTest.php
Original file line number Diff line number Diff line change
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;
}
}