Skip to content

Commit 293a627

Browse files
committed
Throw exception if flash key value is not an array.
Return null if flash key doesn't exist in session.
1 parent c50e730 commit 293a627

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/View/Helper/FlashHelper.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,23 @@ class FlashHelper extends Helper {
5959
* @param string $key The [Flash.]key you are rendering in the view.
6060
* @param array $options Additional options to use for the creation of this flash message.
6161
* Supports the 'params', and 'element' keys that are used in the helper.
62-
* @return string
62+
* @return string|null Rendered flash message or null if flash key does not exist
63+
* in session.
64+
* @throws \UnexpectedValueException If value for flash settings key is not an array.
6365
*/
6466
public function render($key = 'flash', array $options = []) {
6567
if (!$this->request->session()->check("Flash.$key")) {
66-
return '';
68+
return;
6769
}
6870

69-
$flash = $options + $this->request->session()->read("Flash.$key");
71+
$flash = $this->request->session()->read("Flash.$key");
72+
if (!is_array($flash)) {
73+
throw new \UnexpectedValueException(sprintf(
74+
'Value for flash setting key "%s" must be an array.',
75+
$key
76+
));
77+
}
78+
$flash = $options + $flash;
7079
$this->request->session()->delete("Flash.$key");
7180

7281
return $this->_View->element($flash['element'], $flash);

tests/TestCase/View/Helper/FlashHelperTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ public function testFlash() {
107107

108108
$expected['child'] = ['tag' => 'p', 'content' => 'This is a test of the emergency broadcasting system'];
109109
$this->assertTag($expected, $result);
110+
111+
$this->assertNull($this->Flash->render('non-existent'));
112+
}
113+
114+
/**
115+
* testFlashThrowsException
116+
*
117+
* @expectedException \UnexpectedValueException
118+
*/
119+
public function testFlashThrowsException() {
120+
$this->View->request->session()->write('Flash.foo', 'bar');
121+
$this->Flash->render('foo');
110122
}
111123

112124
/**

0 commit comments

Comments
 (0)