2007-03-23 21:59:08 +00:00
|
|
|
<?php
|
2007-03-23 22:11:06 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
|
2008-01-01 22:18:17 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-03-23 22:11:06 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-01-01 22:18:17 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-03-23 22:11:06 +00:00
|
|
|
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.tests.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4667
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
|
|
|
*/
|
2008-03-09 04:07:56 +00:00
|
|
|
require_once CAKE_TESTS_LIB . 'cake_test_model.php';
|
|
|
|
require_once CAKE_TESTS_LIB . 'cake_test_fixture.php';
|
2008-03-01 05:23:32 +00:00
|
|
|
!App::import('Vendor', 'simpletest' . DS . 'unit_tester');
|
2007-04-17 09:09:44 +00:00
|
|
|
/**
|
|
|
|
* Short description for class.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.tests.lib
|
|
|
|
*/
|
|
|
|
class CakeTestDispatcher extends Dispatcher {
|
|
|
|
var $controller;
|
2007-04-17 21:20:38 +00:00
|
|
|
var $testCase;
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
function testCase(&$testCase) {
|
|
|
|
$this->testCase =& $testCase;
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 09:09:44 +00:00
|
|
|
function _invoke (&$controller, $params, $missingAction = false) {
|
|
|
|
$this->controller =& $controller;
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
if (isset($this->testCase) && method_exists($this->testCase, 'startController')) {
|
|
|
|
$this->testCase->startController($this->controller, $params);
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
$result = parent::_invoke($this->controller, $params, $missingAction);
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
if (isset($this->testCase) && method_exists($this->testCase, 'endController')) {
|
|
|
|
$this->testCase->endController($this->controller, $params);
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
return $result;
|
2007-04-17 09:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
/**
|
|
|
|
* Short description for class.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.tests.lib
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
class CakeTestCase extends UnitTestCase {
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* Methods used internally.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2007-04-17 04:27:43 +00:00
|
|
|
var $methods = array('start', 'end', 'startcase', 'endcase', 'starttest', 'endtest');
|
2007-10-28 19:42:16 +00:00
|
|
|
var $__truncated = true;
|
2008-01-11 06:45:00 +00:00
|
|
|
/**
|
|
|
|
* By default, all fixtures attached to this class will be truncated and reloaded after each test.
|
|
|
|
* Set this to false to handle manually
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $autoFixtures = true;
|
|
|
|
/**
|
|
|
|
* Maps fixture class names to fixture identifiers as included in CakeTestCase::$fixtures
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
var $_fixtureClassMap = array();
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* Called when a test case (group of methods) is about to start (to be overriden when needed.)
|
2007-04-30 09:52:37 +00:00
|
|
|
*
|
2007-04-17 04:30:23 +00:00
|
|
|
* @param string $method Test method about to get executed.
|
2007-04-30 09:52:37 +00:00
|
|
|
*
|
2007-04-17 04:30:23 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
2007-04-17 04:27:43 +00:00
|
|
|
function startCase() {
|
|
|
|
}
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* Called when a test case (group of methods) has been executed (to be overriden when needed.)
|
2007-04-30 09:52:37 +00:00
|
|
|
*
|
2007-04-17 04:30:23 +00:00
|
|
|
* @param string $method Test method about that was executed.
|
2007-04-30 09:52:37 +00:00
|
|
|
*
|
2007-04-17 04:30:23 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
2007-04-17 04:27:43 +00:00
|
|
|
function endCase() {
|
|
|
|
}
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* Called when a test case method is about to start (to be overriden when needed.)
|
2007-04-30 09:52:37 +00:00
|
|
|
*
|
2007-04-17 04:30:23 +00:00
|
|
|
* @param string $method Test method about to get executed.
|
2007-04-30 09:52:37 +00:00
|
|
|
*
|
2007-04-17 04:30:23 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
2007-04-17 04:27:43 +00:00
|
|
|
function startTest($method) {
|
|
|
|
}
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* Called when a test case method has been executed (to be overriden when needed.)
|
2007-04-30 09:52:37 +00:00
|
|
|
*
|
2007-04-17 04:30:23 +00:00
|
|
|
* @param string $method Test method about that was executed.
|
2007-04-30 09:52:37 +00:00
|
|
|
*
|
2007-04-17 04:30:23 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
2007-04-17 04:27:43 +00:00
|
|
|
function endTest($method) {
|
|
|
|
}
|
2007-04-17 21:20:38 +00:00
|
|
|
|
|
|
|
/**
|
2007-04-18 00:34:38 +00:00
|
|
|
* Callback issued when a controller's action is about to be invoked through testAction().
|
2007-04-17 21:20:38 +00:00
|
|
|
*
|
|
|
|
* @param Controller $controller Controller that's about to be invoked.
|
2007-04-18 00:34:38 +00:00
|
|
|
* @param array $params Additional parameters as sent by testAction().
|
2007-04-17 21:20:38 +00:00
|
|
|
*/
|
|
|
|
function startController(&$controller, $params = array()) {
|
|
|
|
if (isset($params['fixturize']) && ((is_array($params['fixturize']) && !empty($params['fixturize'])) || $params['fixturize'] === true)) {
|
|
|
|
if (!isset($this->db)) {
|
|
|
|
$this->_initDb();
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
$classRegistry =& ClassRegistry::getInstance();
|
|
|
|
$models = array();
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($classRegistry->__map as $key => $name) {
|
2007-04-17 21:20:38 +00:00
|
|
|
$object =& $classRegistry->getObject(Inflector::camelize($key));
|
2007-10-27 01:32:17 +00:00
|
|
|
if (is_subclass_of($object, 'Model') && ((is_array($params['fixturize']) && in_array($object->alias, $params['fixturize'])) || $params['fixturize'] === true)) {
|
|
|
|
$models[$object->alias] = array (
|
2007-04-17 21:20:38 +00:00
|
|
|
'table' => $object->table,
|
2007-10-27 01:32:17 +00:00
|
|
|
'model' => $object->alias,
|
2007-04-17 21:20:38 +00:00
|
|
|
'key' => Inflector::camelize($key)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
if (!empty($models) && isset($this->db)) {
|
|
|
|
$this->_queries = array(
|
|
|
|
'create' => array(),
|
|
|
|
'insert' => array(),
|
|
|
|
'drop' => array()
|
|
|
|
);
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($models as $model) {
|
2007-04-17 21:20:38 +00:00
|
|
|
$fixture =& new CakeTestFixture($this->db);
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
$fixture->name = $model['model'] . 'Test';
|
|
|
|
$fixture->table = $model['table'];
|
|
|
|
$fixture->import = array('model' => $model['model'], 'records' => true);
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
$fixture->init();
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2008-01-11 06:45:00 +00:00
|
|
|
$createFixture = $fixture->create($this->db);
|
2007-04-17 21:20:38 +00:00
|
|
|
$insertsFixture = $fixture->insert();
|
|
|
|
$dropFixture = $fixture->drop();
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
if (!empty($createFixture)) {
|
|
|
|
$this->_queries['create'] = am($this->_queries['create'], array($createFixture));
|
|
|
|
}
|
|
|
|
if (!empty($insertsFixture)) {
|
|
|
|
$this->_queries['insert'] = am($this->_queries['insert'], $insertsFixture);
|
|
|
|
}
|
|
|
|
if (!empty($dropFixture)) {
|
|
|
|
$this->_queries['drop'] = am($this->_queries['drop'], array($dropFixture));
|
|
|
|
}
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($this->_queries['create'] as $query) {
|
2007-04-17 21:20:38 +00:00
|
|
|
if (isset($query) && $query !== false) {
|
2007-10-27 20:33:52 +00:00
|
|
|
$this->db->execute($query);
|
2007-04-17 21:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($this->_queries['insert'] as $query) {
|
2007-04-17 21:20:38 +00:00
|
|
|
if (isset($query) && $query !== false) {
|
2007-10-27 20:33:52 +00:00
|
|
|
$this->db->execute($query);
|
2007-04-17 21:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($models as $model) {
|
2007-04-17 21:20:38 +00:00
|
|
|
$object =& $classRegistry->getObject($model['key']);
|
|
|
|
if ($object !== false) {
|
2007-08-21 21:46:59 +00:00
|
|
|
$object->setDataSource('test_suite');
|
|
|
|
$object->cacheSources = false;
|
2007-04-17 21:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
2007-04-18 00:34:38 +00:00
|
|
|
* Callback issued when a controller's action has been invoked through testAction().
|
2007-04-17 21:20:38 +00:00
|
|
|
*
|
|
|
|
* @param Controller $controller Controller that has been invoked.
|
2007-04-18 00:34:38 +00:00
|
|
|
* * @param array $params Additional parameters as sent by testAction().
|
2007-04-17 21:20:38 +00:00
|
|
|
*/
|
|
|
|
function endController(&$controller, $params = array()) {
|
|
|
|
if (isset($this->db) && isset($this->_queries) && !empty($this->_queries) && !empty($this->_queries['drop'])) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($this->_queries['drop'] as $query) {
|
2007-04-17 21:20:38 +00:00
|
|
|
if (isset($query) && $query !== false) {
|
2007-10-27 20:33:52 +00:00
|
|
|
$this->db->execute($query);
|
2007-04-17 21:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
2007-04-17 21:20:38 +00:00
|
|
|
* @param array $params Parameters
|
2007-04-30 09:52:37 +00:00
|
|
|
*
|
2007-04-17 04:30:23 +00:00
|
|
|
* @return mixed What is returned from action (if $requested is true), or view rendered html
|
2007-04-30 09:52:37 +00:00
|
|
|
*
|
2007-04-17 04:30:23 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
2007-04-18 00:34:38 +00:00
|
|
|
function testAction($url, $params = array()) {
|
2007-04-17 21:20:38 +00:00
|
|
|
$default = array(
|
|
|
|
'return' => 'result',
|
|
|
|
'fixturize' => false,
|
|
|
|
'data' => array(),
|
|
|
|
'method' => 'post'
|
|
|
|
);
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
$params = am($default, $params);
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
if (!empty($params['data'])) {
|
|
|
|
$data = array('data' => $params['data']);
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
if (low($params['method']) == 'get') {
|
2007-04-17 04:27:43 +00:00
|
|
|
$_GET = $data;
|
|
|
|
} else {
|
|
|
|
$_POST = $data;
|
|
|
|
}
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
$return = $params['return'];
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
unset($params['data']);
|
|
|
|
unset($params['method']);
|
|
|
|
unset($params['return']);
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 09:09:44 +00:00
|
|
|
$dispatcher =& new CakeTestDispatcher();
|
2007-04-17 21:20:38 +00:00
|
|
|
$dispatcher->testCase($this);
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 09:09:44 +00:00
|
|
|
if (low($return) != 'result') {
|
2007-04-17 08:41:52 +00:00
|
|
|
$params['return'] = 0;
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 08:41:52 +00:00
|
|
|
ob_start();
|
|
|
|
@$dispatcher->dispatch($url, $params);
|
|
|
|
$result = ob_get_clean();
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 09:09:44 +00:00
|
|
|
if (low($return) == 'vars') {
|
2007-04-17 21:20:38 +00:00
|
|
|
$view =& ClassRegistry::getObject('view');
|
|
|
|
$viewVars = $view->getVars();
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
$result = array();
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($viewVars as $var) {
|
2007-04-17 21:20:38 +00:00
|
|
|
$result[$var] = $view->getVar($var);
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
if (!empty($view->pageTitle)) {
|
|
|
|
$result = am($result, array('title' => $view->pageTitle));
|
2007-04-17 09:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-17 08:41:52 +00:00
|
|
|
} else {
|
|
|
|
$params['return'] = 1;
|
|
|
|
$params['bare'] = 1;
|
|
|
|
$params['requested'] = 1;
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 08:41:52 +00:00
|
|
|
$result = @$dispatcher->dispatch($url, $params);
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
$classRegistry =& ClassRegistry::getInstance();
|
|
|
|
$keys = array_keys($classRegistry->__objects);
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($keys as $key) {
|
2007-04-17 21:20:38 +00:00
|
|
|
$key = Inflector::camelize($key);
|
|
|
|
$classRegistry->removeObject($key);
|
|
|
|
}
|
|
|
|
$classRegistry->__map = array();
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
if (isset($this->_queries)) {
|
|
|
|
unset($this->_queries);
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 08:41:52 +00:00
|
|
|
return $result;
|
2007-04-17 04:27:43 +00:00
|
|
|
}
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* Announces the start of a test.
|
|
|
|
*
|
|
|
|
* @param string $method Test method just started.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
function before($method) {
|
|
|
|
parent::before($method);
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
if (isset($this->fixtures) && (!is_array($this->fixtures) || empty($this->fixtures))) {
|
|
|
|
unset($this->fixtures);
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
// Set up DB connection
|
|
|
|
if (isset($this->fixtures) && low($method) == 'start') {
|
2007-04-17 21:20:38 +00:00
|
|
|
$this->_initDb();
|
2007-03-23 21:59:08 +00:00
|
|
|
$this->_loadFixtures();
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
// Create records
|
2008-01-11 06:45:00 +00:00
|
|
|
if (isset($this->_fixtures) && isset($this->db) && !in_array(low($method), array('start', 'end')) && $this->__truncated && $this->autoFixtures == true) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($this->_fixtures as $fixture) {
|
2008-01-11 06:45:00 +00:00
|
|
|
$inserts = $fixture->insert($this->db);
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 04:27:43 +00:00
|
|
|
if (!in_array(low($method), $this->methods)) {
|
|
|
|
$this->startTest($method);
|
|
|
|
}
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* Runs as first test to create tables.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
function start() {
|
|
|
|
if (isset($this->_fixtures) && isset($this->db)) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($this->_fixtures as $fixture) {
|
2008-01-11 06:45:00 +00:00
|
|
|
$fixture->create($this->db);
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* Runs as last test to drop tables.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
function end() {
|
|
|
|
if (isset($this->_fixtures) && isset($this->db)) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach (array_reverse($this->_fixtures) as $fixture) {
|
2008-01-11 06:45:00 +00:00
|
|
|
$fixture->drop($this->db);
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* Announces the end of a test.
|
|
|
|
*
|
|
|
|
* @param string $method Test method just finished.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
function after($method) {
|
|
|
|
if (isset($this->_fixtures) && isset($this->db) && !in_array(low($method), array('start', 'end'))) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($this->_fixtures as $fixture) {
|
2007-10-27 20:33:52 +00:00
|
|
|
$this->db->truncate($fixture->table);
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
2007-10-28 19:42:16 +00:00
|
|
|
$this->__truncated = true;
|
|
|
|
} else {
|
|
|
|
$this->__truncated = false;
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2007-04-17 04:27:43 +00:00
|
|
|
if (!in_array(low($method), $this->methods)) {
|
|
|
|
$this->endTest($method);
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
parent::after($method);
|
|
|
|
}
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* Gets a list of test names. Normally that will be all internal methods that start with the
|
|
|
|
* name "test". This method should be overridden if you want a different rule.
|
|
|
|
*
|
|
|
|
* @return array List of test names.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-04-17 21:20:38 +00:00
|
|
|
function getTests() {
|
2007-04-30 09:52:37 +00:00
|
|
|
$methods = array_diff(parent::getTests(), array('testAction', 'testaction'));
|
2007-04-18 00:34:38 +00:00
|
|
|
$methods = am(am(array('start', 'startCase'), $methods), array('endCase', 'end'));
|
2007-03-23 21:59:08 +00:00
|
|
|
return $methods;
|
|
|
|
}
|
2008-01-11 06:45:00 +00:00
|
|
|
/**
|
|
|
|
* Chooses which fixtures to load for a given test
|
|
|
|
*
|
|
|
|
* @param string $fixture Each parameter is a model name that corresponds to a fixture, i.e. 'Post', 'Author', etc.
|
|
|
|
* @access public
|
|
|
|
* @see CakeTestCase::$autoFixtures
|
|
|
|
*/
|
|
|
|
function loadFixtures() {
|
|
|
|
$args = func_get_args();
|
|
|
|
foreach ($args as $class) {
|
|
|
|
if (isset($this->_fixtureClassMap[$class])) {
|
|
|
|
$fixture = $this->_fixtures[$this->_fixtureClassMap[$class]];
|
|
|
|
$this->db->truncate($fixture->table);
|
|
|
|
$fixture->insert($this->db);
|
|
|
|
} else {
|
|
|
|
trigger_error("Non-existent fixture class {$class} referenced in test", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-17 21:20:38 +00:00
|
|
|
/**
|
|
|
|
* Initialize DB connection.
|
|
|
|
*
|
2008-03-10 06:05:31 +00:00
|
|
|
* @access protected
|
2007-04-17 21:20:38 +00:00
|
|
|
*/
|
|
|
|
function _initDb() {
|
2007-04-30 10:27:58 +00:00
|
|
|
$testDbAvailable = false;
|
|
|
|
|
|
|
|
if (class_exists('DATABASE_CONFIG')) {
|
|
|
|
$dbConfig =& new DATABASE_CONFIG();
|
|
|
|
$testDbAvailable = isset($dbConfig->test);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($testDbAvailable) {
|
|
|
|
// Try for test DB
|
|
|
|
restore_error_handler();
|
|
|
|
@$db =& ConnectionManager::getDataSource('test');
|
|
|
|
set_error_handler('simpleTestErrorHandler');
|
|
|
|
|
|
|
|
$testDbAvailable = $db->isConnected();
|
|
|
|
}
|
2007-04-17 21:20:38 +00:00
|
|
|
|
|
|
|
// Try for default DB
|
2007-04-30 10:27:58 +00:00
|
|
|
if (!$testDbAvailable) {
|
2007-04-17 21:20:38 +00:00
|
|
|
$db =& ConnectionManager::getDataSource('default');
|
2008-01-11 06:45:00 +00:00
|
|
|
$db->config['prefix'] = 'test_suite_';
|
2007-04-17 21:20:38 +00:00
|
|
|
}
|
|
|
|
|
2007-08-29 08:32:18 +00:00
|
|
|
ConnectionManager::create('test_suite', $db->config);
|
2007-04-17 21:20:38 +00:00
|
|
|
// Get db connection
|
|
|
|
$this->db =& ConnectionManager::getDataSource('test_suite');
|
2007-08-21 21:46:59 +00:00
|
|
|
$this->db->cacheSources = false;
|
2007-04-17 21:20:38 +00:00
|
|
|
$this->db->fullDebug = false;
|
|
|
|
}
|
2007-04-17 04:30:23 +00:00
|
|
|
/**
|
|
|
|
* Load fixtures specified in var $fixtures.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
function _loadFixtures() {
|
|
|
|
if (!isset($this->fixtures) || empty($this->fixtures)) {
|
|
|
|
return;
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
if (!is_array($this->fixtures)) {
|
2008-01-11 06:45:00 +00:00
|
|
|
$this->fixtures = array_map('trim', explode(',', $this->fixtures));
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
$this->_fixtures = array();
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($this->fixtures as $index => $fixture) {
|
2007-03-23 21:59:08 +00:00
|
|
|
$fixtureFile = null;
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
if (strpos($fixture, 'core.') === 0) {
|
|
|
|
$fixture = substr($fixture, strlen('core.'));
|
2007-11-22 01:55:50 +00:00
|
|
|
foreach (Configure::corePaths('cake') as $key => $path) {
|
|
|
|
$fixturePaths[] = $path . DS . 'tests' . DS . 'fixtures';
|
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
} elseif (strpos($fixture, 'app.') === 0) {
|
2007-03-23 21:59:08 +00:00
|
|
|
$fixture = substr($fixture, strlen('app.'));
|
|
|
|
$fixturePaths = array(
|
2008-03-09 04:07:56 +00:00
|
|
|
TESTS . 'fixtures',
|
|
|
|
VENDORS . 'tests' . DS . 'fixtures'
|
|
|
|
);
|
|
|
|
} elseif (strpos($fixture, 'plugin.') === 0) {
|
|
|
|
$parts = explode('.', $fixture, 3);
|
|
|
|
$pluginName = $parts[1];
|
|
|
|
$fixture = $parts[2];
|
|
|
|
$fixturePaths = array(
|
|
|
|
APP . 'plugins' . DS . $pluginName . DS . 'tests' . DS . 'fixtures',
|
|
|
|
TESTS . 'fixtures',
|
2007-08-29 08:32:18 +00:00
|
|
|
VENDORS . 'tests' . DS . 'fixtures'
|
2007-03-23 21:59:08 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$fixturePaths = array(
|
|
|
|
TESTS . 'fixtures',
|
2007-08-29 08:32:18 +00:00
|
|
|
VENDORS . 'tests' . DS . 'fixtures',
|
2007-11-23 08:16:23 +00:00
|
|
|
TEST_CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'tests' . DS . 'fixtures'
|
2007-03-23 21:59:08 +00:00
|
|
|
);
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($fixturePaths as $path) {
|
2007-03-23 21:59:08 +00:00
|
|
|
if (is_readable($path . DS . $fixture . '_fixture.php')) {
|
|
|
|
$fixtureFile = $path . DS . $fixture . '_fixture.php';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
if (isset($fixtureFile)) {
|
|
|
|
require_once($fixtureFile);
|
|
|
|
$fixtureClass = Inflector::camelize($fixture) . 'Fixture';
|
|
|
|
$this->_fixtures[$this->fixtures[$index]] =& new $fixtureClass($this->db);
|
2008-01-11 06:45:00 +00:00
|
|
|
$this->_fixtureClassMap[Inflector::camelize($fixture)] = $this->fixtures[$index];
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
if (empty($this->_fixtures)) {
|
|
|
|
unset($this->_fixtures);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|