Added new callbacks and ability to execute CakePHP URLs from CakeTestCase

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4863 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mariano.iglesias 2007-04-17 04:27:43 +00:00
parent f4c9be8be2
commit a053a1aa7c

View file

@ -36,6 +36,81 @@ vendor('simpletest'.DS.'unit_tester');
* @subpackage cake.cake.tests.lib
*/
class CakeTestCase extends UnitTestCase {
/**
* Methods used internally.
*
* @var array
* @access private
*/
var $methods = array('start', 'end', 'startcase', 'endcase', 'starttest', 'endtest');
/**
* Called when a test case (group of methods) is about to start (to be overriden when needed.)
*
* @param string $method Test method about to get executed.
*
* @access protected
*/
function startCase() {
}
/**
* Called when a test case (group of methods) has been executed (to be overriden when needed.)
*
* @param string $method Test method about that was executed.
*
* @access protected
*/
function endCase() {
}
/**
* Called when a test case method is about to start (to be overriden when needed.)
*
* @param string $method Test method about to get executed.
*
* @access protected
*/
function startTest($method) {
}
/**
* Called when a test case method has been executed (to be overriden when needed.)
*
* @param string $method Test method about that was executed.
*
* @access protected
*/
function endTest($method) {
}
/**
* Executes a Cake URL, optionally getting the view rendering or what is returned
* when params['requested'] is set.
*
* @param string $url Cake URL to execute (e.g: /articles/view/455)
* @param string $requested Set to true if params['requested'] should be set, false otherwise
* @param array $data Data that will be sent to controller. E.g: array('Article' => array('id'=>4))
* @param string $method Method to simulate posting of data to controller ('get' or 'post')
*
* @return mixed What is returned from action (if $requested is true), or view rendered html
*
* @access protected
*/
function requestAction($url, $requested = true, $data = null, $method = 'post') {
$params = array();
if (!$requested) {
$params['return'] = true;
}
if (is_array($data) && !empty($data)) {
$data = array('data' => $data);
if (low($method) == 'get') {
$_GET = $data;
} else {
$_POST = $data;
}
}
return @Object::requestAction($url, $params);
}
/**
* Announces the start of a test.
*
@ -90,6 +165,10 @@ class CakeTestCase extends UnitTestCase {
}
}
}
if (!in_array(low($method), $this->methods)) {
$this->startTest($method);
}
}
/**
* Runs as first test to create tables.
@ -123,7 +202,6 @@ class CakeTestCase extends UnitTestCase {
}
}
}
/**
* Announces the end of a test.
*
@ -142,6 +220,10 @@ class CakeTestCase extends UnitTestCase {
}
}
if (!in_array(low($method), $this->methods)) {
$this->endTest($method);
}
parent::after($method);
}
/**
@ -153,11 +235,7 @@ class CakeTestCase extends UnitTestCase {
* @access public
*/
function getTests() {
$methods = parent::getTests();
if (isset($this->fixtures)) {
$methods = am(am(array('start'), $methods), array('end'));
}
$methods = am(am(array('start', 'startCase'), parent::getTests()), array('endCase', 'end'));
return $methods;
}