Skip to content

Commit

Permalink
Throw exception if flash key value is not an array.
Browse files Browse the repository at this point in the history
Return null if flash key doesn't exist in session.
  • Loading branch information
ADmad committed Jul 14, 2014
1 parent c50e730 commit 293a627
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/View/Helper/FlashHelper.php
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/View/Helper/FlashHelperTest.php
Expand Up @@ -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');
}

/**
Expand Down

0 comments on commit 293a627

Please sign in to comment.