Skip to content
Permalink
Browse files

Throw exception if flash key value is not an array.

Return null if flash key doesn't exist in session.
  • Loading branch information...
ADmad committed Jul 14, 2014
1 parent c50e730 commit 293a6279740e6dbcb57b983e9f0b9bd40a294765
Showing with 24 additions and 3 deletions.
  1. +12 −3 src/View/Helper/FlashHelper.php
  2. +12 −0 tests/TestCase/View/Helper/FlashHelperTest.php
@@ -59,14 +59,23 @@ class FlashHelper extends Helper {
* @param string $key The [Flash.]key you are rendering in the view.
* @param array $options Additional options to use for the creation of this flash message.
* Supports the 'params', and 'element' keys that are used in the helper.
* @return string
* @return string|null Rendered flash message or null if flash key does not exist
* in session.
* @throws \UnexpectedValueException If value for flash settings key is not an array.
*/
public function render($key = 'flash', array $options = []) {
if (!$this->request->session()->check("Flash.$key")) {
return '';
return;
}
$flash = $options + $this->request->session()->read("Flash.$key");
$flash = $this->request->session()->read("Flash.$key");
if (!is_array($flash)) {
throw new \UnexpectedValueException(sprintf(
'Value for flash setting key "%s" must be an array.',
$key
));
}
$flash = $options + $flash;
$this->request->session()->delete("Flash.$key");
return $this->_View->element($flash['element'], $flash);
@@ -107,6 +107,18 @@ public function testFlash() {
$expected['child'] = ['tag' => 'p', 'content' => 'This is a test of the emergency broadcasting system'];
$this->assertTag($expected, $result);
$this->assertNull($this->Flash->render('non-existent'));
}
/**
* testFlashThrowsException
*
* @expectedException \UnexpectedValueException
*/
public function testFlashThrowsException() {
$this->View->request->session()->write('Flash.foo', 'bar');
$this->Flash->render('foo');
}
/**

0 comments on commit 293a627

Please sign in to comment.
You can’t perform that action at this time.