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-05-01 05:39:36 +00:00
|
|
|
if (!class_exists('dispatcher')) {
|
|
|
|
require CAKE . 'dispatcher.php';
|
|
|
|
}
|
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-05-10 01:39:56 +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
|
|
|
|
2008-05-18 20:17:28 +00:00
|
|
|
/**
|
|
|
|
* Overrides SimpleTestCase::assert to enable calling of skipIf() from within tests
|
|
|
|
*/
|
|
|
|
function assert(&$expectation, $compare, $message = '%s') {
|
|
|
|
if ($this->_should_skip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return parent::assert($expectation, $compare, $message);
|
|
|
|
}
|
|
|
|
|
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)) {
|
2008-03-12 21:19:31 +00:00
|
|
|
$this->_queries['create'] = array_merge($this->_queries['create'], array($createFixture));
|
2007-04-17 21:20:38 +00:00
|
|
|
}
|
|
|
|
if (!empty($insertsFixture)) {
|
2008-03-12 21:19:31 +00:00
|
|
|
$this->_queries['insert'] = array_merge($this->_queries['insert'], $insertsFixture);
|
2007-04-17 21:20:38 +00:00
|
|
|
}
|
|
|
|
if (!empty($dropFixture)) {
|
2008-03-12 21:19:31 +00:00
|
|
|
$this->_queries['drop'] = array_merge($this->_queries['drop'], array($dropFixture));
|
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['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
|
|
|
/**
|
2008-04-16 20:16:52 +00:00
|
|
|
* Executes a Cake URL, and can get (depending on the $params['return'] value):
|
2007-04-17 04:30:23 +00:00
|
|
|
*
|
2008-04-16 20:16:52 +00:00
|
|
|
* 1. 'result': Whatever the action returns (and also specifies $this->params['requested'] for controller)
|
|
|
|
* 2. 'view': The rendered view, without the layout
|
|
|
|
* 3. 'contents': The rendered view, within the layout.
|
|
|
|
* 4. 'vars': the view vars
|
2007-04-30 09:52:37 +00:00
|
|
|
*
|
2008-04-16 20:16:52 +00:00
|
|
|
* @param string $url Cake URL to execute (e.g: /articles/view/455)
|
|
|
|
* @param array $params Parameters, or simply a string of what to return
|
|
|
|
* @return mixed Whatever is returned depending of requested result
|
|
|
|
* @access public
|
2007-04-17 04:30:23 +00:00
|
|
|
*/
|
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'
|
|
|
|
);
|
2008-04-26 13:44:02 +00:00
|
|
|
|
2008-04-16 20:16:52 +00:00
|
|
|
if (is_string($params)) {
|
2008-04-16 20:25:36 +00:00
|
|
|
$params = array('return' => $params);
|
2008-04-16 20:16:52 +00:00
|
|
|
}
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2008-03-12 21:19:31 +00:00
|
|
|
$params = array_merge($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
|
|
|
|
2008-04-16 20:16:52 +00:00
|
|
|
if (strtolower($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'];
|
2008-04-16 20:16:52 +00:00
|
|
|
$params = array_diff_key($params, array('data' => null, 'method' => null, 'return' => null));
|
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
|
|
|
|
2008-04-16 20:16:52 +00:00
|
|
|
if ($return != 'result') {
|
|
|
|
if ($return != 'contents') {
|
|
|
|
$params['layout'] = false;
|
|
|
|
}
|
2008-04-26 13:44:02 +00:00
|
|
|
|
2007-04-17 08:41:52 +00:00
|
|
|
ob_start();
|
2008-04-16 20:22:07 +00:00
|
|
|
@$dispatcher->dispatch($url, $params);
|
2007-04-17 08:41:52 +00:00
|
|
|
$result = ob_get_clean();
|
2007-04-30 09:52:37 +00:00
|
|
|
|
2008-04-16 20:16:52 +00:00
|
|
|
if ($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)) {
|
2008-03-12 21:19:31 +00:00
|
|
|
$result = array_merge($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);
|
|
|
|
}
|
2008-04-26 13:44:02 +00:00
|
|
|
|
2008-04-16 20:16:52 +00:00
|
|
|
ClassRegistry::flush();
|
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
|
2008-04-16 20:16:52 +00:00
|
|
|
if (isset($this->fixtures) && strtolower($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-04-16 20:16:52 +00:00
|
|
|
if (isset($this->_fixtures) && isset($this->db) && !in_array(strtolower($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
|
|
|
|
2008-04-16 20:16:52 +00:00
|
|
|
if (!in_array(strtolower($method), $this->methods)) {
|
2007-04-17 04:27:43 +00:00
|
|
|
$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) {
|
2008-04-16 20:16:52 +00:00
|
|
|
if (isset($this->_fixtures) && isset($this->db) && !in_array(strtolower($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
|
|
|
|
2008-04-16 20:16:52 +00:00
|
|
|
if (!in_array(strtolower($method), $this->methods)) {
|
2007-04-17 04:27:43 +00:00
|
|
|
$this->endTest($method);
|
|
|
|
}
|
2008-05-18 20:17:28 +00:00
|
|
|
$this->_should_skip = false;
|
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'));
|
2008-03-12 21:19:31 +00:00
|
|
|
$methods = array_merge(array_merge(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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-22 19:53:00 +00:00
|
|
|
/**
|
|
|
|
* Takes an array $expected and generates a regex from it to match the provided $string. Samples for $expected:
|
2008-04-26 13:44:02 +00:00
|
|
|
*
|
2008-04-22 19:53:00 +00:00
|
|
|
* Checks for an input tag with a name attribute (contains any value) and an id attribute that contains 'my-input':
|
|
|
|
* array('input' => array('name', 'id' => 'my-input'))
|
2008-04-26 13:44:02 +00:00
|
|
|
*
|
2008-04-22 19:53:00 +00:00
|
|
|
* Checks for two p elements with some text in them:
|
|
|
|
* array(
|
|
|
|
* array('p' => true),
|
|
|
|
* 'textA',
|
2008-05-08 13:14:47 +00:00
|
|
|
* '/p',
|
2008-04-22 19:53:00 +00:00
|
|
|
* array('p' => true),
|
|
|
|
* 'textB',
|
2008-05-08 13:14:47 +00:00
|
|
|
* '/p'
|
2008-04-22 19:53:00 +00:00
|
|
|
* )
|
2008-04-26 13:44:02 +00:00
|
|
|
*
|
2008-04-26 14:28:00 +00:00
|
|
|
* You can also specify a pattern expression as part of the attribute values, or the tag being defined,
|
|
|
|
* if you prepend the value with preg: and enclose it with slashes, like so:
|
|
|
|
* array(
|
|
|
|
* array('input' => array('name', 'id' => 'preg:/FieldName\d+/')),
|
|
|
|
* 'preg:/My\s+field/'
|
|
|
|
* )
|
2008-04-26 13:44:02 +00:00
|
|
|
*
|
2008-04-22 19:53:00 +00:00
|
|
|
* Important: This function is very forgiving about whitespace and also accepts any permutation of attribute order.
|
2008-04-26 14:51:44 +00:00
|
|
|
* It will also allow whitespaces between specified tags.
|
2008-04-26 13:44:02 +00:00
|
|
|
*
|
2008-04-22 19:53:00 +00:00
|
|
|
* @param string $string An HTML/XHTML/XML string
|
2008-05-08 11:32:31 +00:00
|
|
|
* @param array $expected An array, see above
|
2008-04-22 19:53:00 +00:00
|
|
|
* @param string $message SimpleTest failure output string
|
|
|
|
* @access public
|
|
|
|
*/
|
2008-05-08 11:32:31 +00:00
|
|
|
function assertTags($string, $expected, $fullDebug = false) {
|
2008-04-22 19:53:00 +00:00
|
|
|
$regex = array();
|
|
|
|
$normalized = array();
|
2008-05-18 19:51:30 +00:00
|
|
|
foreach ((array) $expected as $key => $val) {
|
2008-04-22 19:53:00 +00:00
|
|
|
if (!is_numeric($key)) {
|
|
|
|
$normalized[] = array($key => $val);
|
|
|
|
} else {
|
|
|
|
$normalized[] = $val;
|
|
|
|
}
|
|
|
|
}
|
2008-05-08 11:32:31 +00:00
|
|
|
$i = 0;
|
2008-04-22 19:53:00 +00:00
|
|
|
foreach ($normalized as $tags) {
|
2008-05-08 11:32:31 +00:00
|
|
|
$i++;
|
2008-05-08 13:14:47 +00:00
|
|
|
if (is_string($tags) && $tags{0} == '<') {
|
|
|
|
$tags = array(substr($tags, 1) => array());
|
|
|
|
} elseif (is_string($tags)) {
|
|
|
|
if (preg_match('/^\*?\//', $tags, $match)) {
|
2008-05-08 11:32:31 +00:00
|
|
|
$prefix = array(null, null);
|
2008-05-08 13:14:47 +00:00
|
|
|
if ($match[0] == '*/') {
|
2008-05-08 11:32:31 +00:00
|
|
|
$prefix = array('Anything, ', '.*?');
|
|
|
|
}
|
|
|
|
$regex[] = array(
|
|
|
|
sprintf('%sClose %s tag', $prefix[0], substr($tags, strlen($match[0]))),
|
|
|
|
sprintf('%s<[\s]*\/[\s]*%s[\s]*>[\n\r]*', $prefix[1], substr($tags, strlen($match[0]))),
|
|
|
|
$i,
|
|
|
|
);
|
2008-04-22 19:53:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-04-26 14:28:00 +00:00
|
|
|
if (!empty($tags) && preg_match('/^preg\:\/(.+)\/$/i', $tags, $matches)) {
|
|
|
|
$tags = $matches[1];
|
2008-05-08 11:32:31 +00:00
|
|
|
$type = 'Regex matches';
|
2008-04-26 14:28:00 +00:00
|
|
|
} else {
|
|
|
|
$tags = preg_quote($tags, '/');
|
2008-05-08 11:32:31 +00:00
|
|
|
$type = 'Text equals';
|
2008-04-26 14:28:00 +00:00
|
|
|
}
|
2008-05-08 11:32:31 +00:00
|
|
|
$regex[] = array(
|
|
|
|
sprintf('%s "%s"', $type, $tags),
|
|
|
|
$tags,
|
|
|
|
$i,
|
|
|
|
);
|
2008-04-22 19:53:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($tags as $tag => $attributes) {
|
2008-05-08 11:32:31 +00:00
|
|
|
$regex[] = array(
|
|
|
|
sprintf('Open %s tag', $tag),
|
2008-05-08 13:19:38 +00:00
|
|
|
sprintf('[\s]*<%s', preg_quote($tag, '/')),
|
2008-05-08 11:32:31 +00:00
|
|
|
$i,
|
|
|
|
);
|
2008-04-22 19:53:00 +00:00
|
|
|
if ($attributes === true) {
|
|
|
|
$attributes = array();
|
|
|
|
}
|
|
|
|
$attrs = array();
|
2008-05-08 11:32:31 +00:00
|
|
|
$explanations = array();
|
2008-04-22 19:53:00 +00:00
|
|
|
foreach ($attributes as $attr => $val) {
|
2008-05-01 19:35:12 +00:00
|
|
|
if (is_numeric($attr) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
|
|
|
|
$attrs[] = $matches[1];
|
2008-05-08 11:32:31 +00:00
|
|
|
$explanations[] = sprintf('Regex "%s" matches', $matches[1]);
|
|
|
|
continue;
|
2008-04-22 19:53:00 +00:00
|
|
|
} else {
|
2008-05-10 04:28:35 +00:00
|
|
|
$quotes = '"';
|
2008-05-01 19:35:12 +00:00
|
|
|
if (is_numeric($attr)) {
|
|
|
|
$attr = $val;
|
2008-05-08 11:32:31 +00:00
|
|
|
$val = '.+?';
|
|
|
|
$explanations[] = sprintf('Attribute "%s" present', $attr);
|
2008-05-01 19:35:12 +00:00
|
|
|
} else if (!empty($val) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
|
2008-05-10 04:28:35 +00:00
|
|
|
$quotes = '"?';
|
2008-05-01 19:35:12 +00:00
|
|
|
$val = $matches[1];
|
2008-05-08 11:32:31 +00:00
|
|
|
$explanations[] = sprintf('Attribute "%s" matches "%s"', $attr, $val);
|
2008-05-01 19:35:12 +00:00
|
|
|
} else {
|
2008-05-08 11:32:31 +00:00
|
|
|
$explanations[] = sprintf('Attribute "%s" == "%s"', $attr, $val);
|
2008-05-01 19:35:12 +00:00
|
|
|
$val = preg_quote($val, '/');
|
|
|
|
}
|
2008-05-10 04:28:35 +00:00
|
|
|
$attrs[] = '[\s]+'.preg_quote($attr, '/').'='.$quotes.$val.$quotes;
|
2008-04-22 19:53:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($attrs) {
|
|
|
|
$permutations = $this->__array_permute($attrs);
|
2008-05-08 11:32:31 +00:00
|
|
|
$permutationTokens = array();
|
2008-04-22 19:53:00 +00:00
|
|
|
foreach ($permutations as $permutation) {
|
2008-05-08 11:32:31 +00:00
|
|
|
$permutationTokens[] = join('', $permutation);
|
2008-04-22 19:53:00 +00:00
|
|
|
}
|
2008-05-08 11:32:31 +00:00
|
|
|
$regex[] = array(
|
|
|
|
sprintf('%s', join(', ', $explanations)),
|
|
|
|
$permutationTokens,
|
|
|
|
$i,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$regex[] = array(
|
|
|
|
sprintf('End %s tag', $tag),
|
|
|
|
'[\s]*\/?[\s]*>[\n\r]*',
|
|
|
|
$i,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($regex as $i => $assertation) {
|
|
|
|
list($description, $expressions, $itemNum) = $assertation;
|
|
|
|
$matches = false;
|
|
|
|
foreach ((array)$expressions as $expression) {
|
|
|
|
if (preg_match(sprintf('/^%s/s', $expression), $string, $match)) {
|
|
|
|
$matches = true;
|
|
|
|
$string = substr($string, strlen($match[0]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$matches) {
|
|
|
|
$this->assert(new TrueExpectation(), false, sprintf('Item #%d / regex #%d failed: %s', $itemNum, $i, $description));
|
|
|
|
if ($fullDebug) {
|
|
|
|
debug($string, true);
|
|
|
|
debug($regex, true);
|
2008-04-22 19:53:00 +00:00
|
|
|
}
|
2008-05-08 11:32:31 +00:00
|
|
|
return false;
|
2008-04-22 19:53:00 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-08 11:32:31 +00:00
|
|
|
return $this->assert(new TrueExpectation(), true, '%s');
|
2008-04-22 19:53:00 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Generates all permutation of an array $items and returns them in a new array.
|
|
|
|
*
|
2008-05-08 11:32:31 +00:00
|
|
|
* @param array $items An array of items
|
2008-04-22 19:53:00 +00:00
|
|
|
* @return array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function __array_permute($items, $perms = array()) {
|
|
|
|
static $permuted;
|
|
|
|
if (empty($perms)) {
|
|
|
|
$permuted = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($items)) {
|
|
|
|
$permuted[] = $perms;
|
|
|
|
} else {
|
|
|
|
$numItems = count($items) - 1;
|
|
|
|
for ($i = $numItems; $i >= 0; --$i) {
|
|
|
|
$newItems = $items;
|
|
|
|
$newPerms = $perms;
|
|
|
|
list($tmp) = array_splice($newItems, $i, 1);
|
|
|
|
array_unshift($newPerms, $tmp);
|
|
|
|
$this->__array_permute($newItems, $newPerms);
|
|
|
|
}
|
|
|
|
return $permuted;
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|