Browse Source

Throw exception if flash key value is not an array.

Return null if flash key doesn't exist in session.
ADmad 11 years ago
parent
commit
293a627974
2 changed files with 24 additions and 3 deletions
  1. 12 3
      src/View/Helper/FlashHelper.php
  2. 12 0
      tests/TestCase/View/Helper/FlashHelperTest.php

+ 12 - 3
src/View/Helper/FlashHelper.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);

+ 12 - 0
tests/TestCase/View/Helper/FlashHelperTest.php

@@ -107,6 +107,18 @@ class FlashHelperTest extends TestCase {
 
 		$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');
 	}
 
 /**