Permalink
Browse files

Tests for ported Flash component & helper

  • Loading branch information...
tanuck committed Feb 4, 2015
1 parent 2ff6bdc commit b8b6b67abd505d963fa2bdf4b488687b9e3a1ddd
@@ -0,0 +1,171 @@
<?php
/**
* FlashComponentTest file
*
* Series of tests for flash component.
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Controller.Component
* @since CakePHP(tm) v 2.7.0-dev
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('FlashComponent', 'Controller/Component');
App::uses('ComponentCollection', 'Controller');
/**
* FlashComponentTest class
*
* @package Cake.Test.Case.Controller.Component
*/
class FlashComponentTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Components = new ComponentCollection();
$this->Flash = new FlashComponent($this->Components);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
CakeSession::destroy();
}
/**
* testSet method
*
* @return void
*/
public function testSet() {
$this->assertNull(CakeSession::read('Flash.flash'));
$this->Flash->set('This is a test message');
$expected = array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/default',
'params' => array()
);
$result = CakeSession::read('Flash.flash');
$this->assertEquals($expected, $result);
$this->Flash->set('This is a test message', array(
'element' => 'test',
'params' => array('foo' => 'bar')
));
$expected = array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/test',
'params' => array('foo' => 'bar')
);
$result = CakeSession::read('Flash.flash');
$this->assertEquals($expected, $result);
$this->Flash->set('This is a test message', array('element' => 'MyPlugin.alert'));
$expected = array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'MyPlugin.Flash/alert',
'params' => array()
);
$result = CakeSession::read('Flash.flash');
$this->assertEquals($expected, $result);
$this->Flash->set('This is a test message', array('key' => 'foobar'));
$expected = array(
'message' => 'This is a test message',
'key' => 'foobar',
'element' => 'Flash/default',
'params' => array()
);
$result = CakeSession::read('Flash.foobar');
$this->assertEquals($expected, $result);
}
/**
* testSetWithException method
*
* @return void
*/
public function testSetWithException() {
$this->assertNull(CakeSession::read('Flash.flash'));
$this->Flash->set(new Exception('This is a test message', 404));
$expected = array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/default',
'params' => array('code' => 404)
);
$result = CakeSession::read('Flash.flash');
$this->assertEquals($expected, $result);
}
/**
* testSetWithComponentConfiguration method
*
* @return void
*/
public function testSetWithComponentConfiguration() {
$this->assertNull(CakeSession::read('Flash.flash'));
$FlashWithSettings = $this->Components->load('Flash', array('element' => 'test'));
$FlashWithSettings->set('This is a test message');
$expected = array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/test',
'params' => array()
);
$result = CakeSession::read('Flash.flash');
$this->assertEquals($expected, $result);
}
/**
* Test magic call method.
*
* @return void
*/
public function testCall() {
$this->assertNull(CakeSession::read('Flash.flash'));
$this->Flash->success('It worked');
$expected = array(
'message' => 'It worked',
'key' => 'flash',
'element' => 'Flash/success',
'params' => array()
);
$result = CakeSession::read('Flash.flash');
$this->assertEquals($expected, $result);
$this->Flash->error('It did not work', array('element' => 'error_thing'));
$expected = array(
'message' => 'It did not work',
'key' => 'flash',
'element' => 'Flash/error',
'params' => array()
);
$result = CakeSession::read('Flash.flash');
$this->assertEquals($expected, $result, 'Element is ignored in magic call.');
}
}
@@ -0,0 +1,153 @@
<?php
/**
* FlashHelperTest file
*
* Series of tests for flash helper.
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.View.Helper
* @since CakePHP(tm) v 2.7.0-dev
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('FlashHelper', 'View/Helper');
App::uses('View', 'View');
App::uses('CakePlugin', 'Core');
/**
* FlashHelperTest class
*
* @package Cake.Test.Case.View.Helper
*/
class FlashHelperTest extends CakeTestCase {
public static function setupBeforeClass() {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
));
}
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$controller = null;
$this->View = new View($controller);
$this->Flash = new FlashHelper($this->View);
if (!CakeSession::started()) {
CakeSession::start();
}
CakeSession::write(array(
'Flash' => array(
'flash' => array(
'key' => 'flash',
'message' => 'This is a calling',
'element' => 'Flash/default',
'params' => array()
),
'notification' => array(
'key' => 'notification',
'message' => 'Broadcast message testing',
'element' => 'flash_helper',
'params' => array(
'title' => 'Notice!',
'name' => 'Alert!'
)
),
'classy' => array(
'key' => 'classy',
'message' => 'Recorded',
'element' => 'flash_classy',
'params' => array()
)
)
));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
unset($this->View, $this->Flash);
CakeSession::destroy();
}
/**
* testFlash method
*
* @return void
*/
public function testFlash() {
$result = $this->Flash->render();
$expected = '<div class="message">This is a calling</div>';
$this->assertContains($expected, $result);
$expected = '<div id="classy-message">Recorded</div>';
$result = $this->Flash->render('classy');
$this->assertContains($expected, $result);
$result = $this->Flash->render('notification');
$expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>Broadcast message testing</p>\n</div>";
$this->assertContains($expected, $result);
$this->assertNull($this->Flash->render('non-existent'));
}
/**
* testFlashThrowsException
*
* @expectedException UnexpectedValueException
*/
public function testFlashThrowsException() {
CakeSession::write('Flash.foo', 'bar');
$this->Flash->render('foo');
}
/**
* test setting the element from the attrs.
*
* @return void
*/
public function testFlashElementInAttrs() {
$result = $this->Flash->render('notification', array(
'element' => 'flash_helper',
'params' => array('title' => 'Alert!', 'name' => 'Notice!')
));
$expected = "<div id=\"notificationLayout\">\n\t<h1>Notice!</h1>\n\t<h3>Alert!</h3>\n\t<p>Broadcast message testing</p>\n</div>";
$this->assertContains($expected, $result);
}
/**
* test using elements in plugins.
*
* @return void
*/
public function testFlashWithPluginElement() {
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
));
CakePlugin::load('TestPlugin');
$result = $this->Flash->render('flash', array('element' => 'TestPlugin.plugin_element'));
$expected = 'this is the plugin element';
$this->assertContains($expected, $result);
}
}
@@ -0,0 +1 @@
<div class="message"><?php echo $message ?></div>
@@ -0,0 +1 @@
<div id="<?php echo $key ?>-message"><?php echo $message; ?></div>
@@ -0,0 +1,5 @@
<div id="notificationLayout">
<h1><?php echo $params['name']; ?></h1>
<h3><?php echo $params['title']; ?></h3>
<p><?php echo $message; ?></p>
</div>
@@ -1,5 +1,7 @@
<?php
/**
* Flash Helper
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -24,7 +26,6 @@
* this class to output your flash messages in your views.
*
* @package Cake.View.Helper
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html
*/
class FlashHelper extends AppHelper {
@@ -38,7 +39,7 @@ class FlashHelper extends AppHelper {
* to consolidate all the parameters for a given type of flash message into the view.
*
* ```
* echo $this->Flash->render('flash', ['params' => ['name' => $user['User']['name']]]);
* echo $this->Flash->render('flash', array('params' => array('name' => $user['User']['name'])));
* ```
*
* This would pass the current user's name into the flash message, so you could create personalized
@@ -48,16 +49,16 @@ class FlashHelper extends AppHelper {
* custom elements allows you to fully customize how flash messages are generated.
*
* ```
* echo $this->Flash->render('flash', ['element' => 'my_custom_element']);
* echo $this->Flash->render('flash', array('element' => 'my_custom_element'));
* ```
*
* If you want to use an element from a plugin for rendering your flash message
* you can use the dot notation for the plugin's element name:
*
* ```
* echo $this->Flash->render('flash', [
* echo $this->Flash->render('flash', array(
* 'element' => 'MyPlugin.my_custom_element',
* ]);
* ));
* ```
*
* @param string $key The [Flash.]key you are rendering in the view.
@@ -68,12 +69,11 @@ class FlashHelper extends AppHelper {
* @throws UnexpectedValueException If value for flash settings key is not an array.
*/
public function render($key = 'flash', $options = array()) {
if (!CakeSession::check("Message.$key")) {
if (!CakeSession::check("Flash.$key")) {
return;
}
$flash = CakeSession::read("Message.$key");
$flash = CakeSession::read("Flash.$key");
if (!is_array($flash)) {
throw new UnexpectedValueException(sprintf(

0 comments on commit b8b6b67

Please sign in to comment.