cakephp2-php8/lib/Cake/Test/Case/Controller/Component/FlashComponentTest.php

182 lines
4.7 KiB
PHP
Raw Normal View History

<?php
/**
* FlashComponentTest file
*
* Series of tests for flash component.
*
2017-06-10 22:15:34 +00:00
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
2017-06-10 22:15:34 +00:00
* @link https://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 https://opensource.org/licenses/mit-license.php MIT License
*/
App::uses('FlashComponent', 'Controller/Component');
App::uses('ComponentCollection', 'Controller');
/**
2015-02-04 15:31:50 +00:00
* FlashComponentTest class
*
* @package Cake.Test.Case.Controller.Component
*/
class FlashComponentTest extends CakeTestCase {
/**
2015-02-04 15:31:50 +00:00
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Components = new ComponentCollection();
$this->Flash = new FlashComponent($this->Components);
}
/**
2015-02-04 15:31:50 +00:00
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
CakeSession::destroy();
}
/**
2015-02-04 15:31:50 +00:00
* testSet method
*
* @return void
*/
public function testSet() {
$this->assertNull(CakeSession::read('Message.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('Message.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('Message.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('Message.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('Message.foobar');
$this->assertEquals($expected, $result);
}
/**
2015-02-04 15:31:50 +00:00
* testSetWithException method
*
* @return void
*/
public function testSetWithException() {
$this->assertNull(CakeSession::read('Message.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('Message.flash');
$this->assertEquals($expected, $result);
}
/**
2015-02-04 15:31:50 +00:00
* testSetWithComponentConfiguration method
*
* @return void
*/
public function testSetWithComponentConfiguration() {
$this->assertNull(CakeSession::read('Message.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('Message.flash');
$this->assertEquals($expected, $result);
}
/**
2015-02-04 15:31:50 +00:00
* Test magic call method.
*
* @return void
*/
public function testCall() {
$this->assertNull(CakeSession::read('Message.flash'));
$this->Flash->success('It worked');
$expected = array(
'message' => 'It worked',
'key' => 'flash',
'element' => 'Flash/success',
'params' => array()
);
$result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result);
$this->Flash->alert('It worked', array('plugin' => 'MyPlugin'));
$expected = array(
'message' => 'It worked',
'key' => 'flash',
'element' => 'MyPlugin.Flash/alert',
'params' => array()
);
$result = CakeSession::read('Message.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('Message.flash');
$this->assertEquals($expected, $result, 'Element is ignored in magic call.');
}
}