diff --git a/src/Controller/Component/FlashComponent.php b/src/Controller/Component/FlashComponent.php index 852716b2bfe..4ad4c945250 100644 --- a/src/Controller/Component/FlashComponent.php +++ b/src/Controller/Component/FlashComponent.php @@ -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')); } diff --git a/tests/TestCase/Controller/Component/FlashComponentTest.php b/tests/TestCase/Controller/Component/FlashComponentTest.php index a1604c71723..7ef35c5c4c4 100644 --- a/tests/TestCase/Controller/Component/FlashComponentTest.php +++ b/tests/TestCase/Controller/Component/FlashComponentTest.php @@ -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); + } }