8000 [Twig Bridge] A simpler way to retrieve flash messages · symfony/symfony@5a56b23 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a56b23

Browse files
javiereguiluzfabpot
authored andcommitted
[Twig Bridge] A simpler way to retrieve flash messages
1 parent 69374da commit 5a56b23

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/Symfony/Bridge/Twig/AppVariable.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,35 @@ public function getDebug()
145145

146146
return $this->debug;
147147
}
148+
149+
/**
150+
* Returns some or all the existing flash messages:
151+
* * getFlashes() returns all the flash messages
152+
* * getFlashes('notice') returns a simple array with flash messages of that type
153+
* * getFlashes(array('notice', 'error')) returns a nested array of type => messages.
154+
*
155+
* @return array
156+
*/
157+
public function getFlashes($types = null)
158+
{
159+
// needed to avoid starting the session automatically when looking for flash messages
160+
try {
161+
$session = $this->getSession();
162+
if (null === $session || !$session->isStarted()) {
163+
return array();
164+
}
165+
} catch (\RuntimeException $e) {
166+
return array();
167+
}
168+
169+
if (null === $types || '' === $types || array() === $types) {
170+
return $session->getFlashBag()->all();
171+
}
172+
173+
if (is_string($types)) {
174+
return $session->getFlashBag()->get($types);
175+
}
176+
177+
return array_intersect_key($session->getFlashBag()->all(), array_flip($types));
178+
}
148179
}

src/Symfony/Bridge/Twig/Tests/AppVariableTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use Symfony\Bridge\Twig\AppVariable;
77
use Symfony\Component\HttpFoundation\Request;
8+
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
89
use Symfony\Component\HttpFoundati 8000 on\Session\Session;
910

1011
class AppVariableTest extends TestCase
@@ -157,6 +158,62 @@ public function testGetSessionWithRequestStackNotSet()
157158
$this->appVariable->getSession();
158159
}
159160

161+
public function testGetFlashesWithNoRequest()
162+
{
163+
$this->setRequestStack(null);
164+
165+
$this->assertEquals(array(), $this->appVariable->getFlashes());
166+
}
167+
168+
public function testGetFlashesWithNoSessionStarted()
169+
{
170+
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
171+
$request->method('getSession')->willReturn(new Session());
172+
173+
$this->setRequestStack($request);
174+
175+
$this->assertEquals(array(), $this->appVariable->getFlashes());
176+
}
177+
178+
public function testGetFlashes()
179+
{
180+
$flashMessages = $this->setFlashMessages();
181+
$this->assertEquals($flashMessages, $this->appVariable->getFlashes(null));
182+
183+
$flashMessages = $this->setFlashMessages();
184+
$this->assertEquals($flashMessages, $this->appVariable->getFlashes(''));
185+
186+
$flashMessages = $this->setFlashMessages();
187+
$this->assertEquals($flashMessages, $this->appVariable->getFlashes(array()));
188+
189+
$flashMessages = $this->setFlashMessages();
190+
$this->assertEquals(array(), $this->appVariable->getFlashes('this-does-not-exist'));
191+
192+
$flashMessages = $this->setFlashMessages();
193+
$this->assertEquals(array(), $this->appVariable->getFlashes(array('this-does-not-exist')));
194+
195+
$flashMessages = $this->setFlashMessages();
196+
$this->assertEquals($flashMessages['notice'], $this->appVariable->getFlashes('notice'));
197+
198+
$flashMessages = $this->setFlashMessages();
199+
$this->assertEquals(
200+
array('notice' => $flashMessages['notice']),
201+
$this->appVariable->getFlashes(array('notice'))
202+
);
203+
204+
$flashMessages = $this->setFlashMessages();
205+
$this->assertEquals(
206+
array('notice' => $flashMessages['notice']),
207+
$this->appVariable->getFlashes(array('notice', 'this-does-not-exist'))
208+
);
209+
210+
$flashMessages = $this->setFlashMessages();
211+
$this->assertEquals(
212+
array('notice' => $flashMessages['notice'], 'error' => $flashMessages['error']),
213+
$this->appVariable->getFlashes(array('notice', 'error'))
214+
);
215+
}
216+
160217
protected function setRequestStack($request)
161218
{
162219
$requestStackMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
@@ -175,4 +232,25 @@ protected function setTokenStorage($user)
175232

176233
$token->method('getUser')->willReturn($user);
177234
}
235+
236+
private function setFlashMessages()
237+
{
238+
$flashMessages = array(
239+
'notice' => array('Notice #1 message'),
240+
'warning' => array('Warning #1 message'),
241+
'error' => array('Error #1 message', 'Error #2 message'),
242+
);
243+
$flashBag = new FlashBag();
244+
$flashBag->initialize($flashMessages);
245+
246+
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->getMock();
247+
$session->method('isStarted')->willReturn(true);
248+
$session->method('getFlashBag')->willReturn($flashBag);
249+
250+
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
251+
$request->method('getSession')->willReturn($session);
252+
$this->setRequestStack($request);
253+
254+
return $flashMessages;
255+
}
178256
}

0 commit comments

Comments
 (0)
0