Skip to content

Commit

Permalink
Add ability to pass exceptions as flash message
Browse files Browse the repository at this point in the history
  • Loading branch information
bcrowe committed Jun 5, 2014
1 parent 2abaf3d commit 4fc8d47
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Controller/Component/FlashComponent.php
Expand Up @@ -43,6 +43,9 @@ public function __construct(ComponentRegistry $collection, array $config = []) {
}

public function set($message, $element = null, array $params = array(), $key = 'flash') {
if ($message instanceof \Exception) {
$message = $message->getMessage();
}
$this->_writeFlash($message, 'info', $params + compact('element', 'key'));
}

Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Controller/Component/FlashComponentTest.php
Expand Up @@ -115,4 +115,19 @@ public function testSet() {
$result = $this->Session->read('Message.foobar');
$this->assertEquals($expected, $result);
}

/**
* testSetWithException method
*
* @return void
* @covers \Cake\Controller\Component\FlashComponent::set
*/
public function testSetWithException() {
$this->assertNull($this->Session->read('Message.flash'));

$this->Flash->set(new \Exception('This is a test message'));
$expected = ['message' => 'This is a test message', 'params' => ['element' => null], 'type' => 'info'];
$result = $this->Session->read('Message.flash');
$this->assertEquals($expected, $result);
}
}

0 comments on commit 4fc8d47

Please sign in to comment.