2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-21 23:55:39 +00:00
|
|
|
* CakeTestCase file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.tests.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4667
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
if (!class_exists('dispatcher')) {
|
|
|
|
require CAKE . 'dispatcher.php';
|
|
|
|
}
|
|
|
|
require_once CAKE_TESTS_LIB . 'cake_test_model.php';
|
|
|
|
require_once CAKE_TESTS_LIB . 'cake_test_fixture.php';
|
|
|
|
App::import('Vendor', 'simpletest' . DS . 'unit_tester');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-21 23:55:39 +00:00
|
|
|
* CakeTestDispatcher
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.tests.lib
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class CakeTestDispatcher extends Dispatcher {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-21 23:55:39 +00:00
|
|
|
/**
|
|
|
|
* controller property
|
|
|
|
*
|
|
|
|
* @var Controller
|
|
|
|
* @access public
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
var $controller;
|
|
|
|
var $testCase;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-21 23:55:39 +00:00
|
|
|
/**
|
|
|
|
* testCase method
|
|
|
|
*
|
|
|
|
* @param CakeTestCase $testCase
|
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testCase(&$testCase) {
|
|
|
|
$this->testCase =& $testCase;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-21 23:55:39 +00:00
|
|
|
/**
|
|
|
|
* invoke method
|
|
|
|
*
|
|
|
|
* @param Controller $controller
|
|
|
|
* @param array $params
|
|
|
|
* @param boolean $missingAction
|
|
|
|
* @return Controller
|
2009-10-14 13:28:06 +00:00
|
|
|
* @access protected
|
2009-03-21 23:55:39 +00:00
|
|
|
*/
|
|
|
|
function _invoke(&$controller, $params, $missingAction = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->controller =& $controller;
|
2008-08-13 16:24:21 +00:00
|
|
|
|
2010-11-10 18:35:00 +00:00
|
|
|
if (array_key_exists('layout', $params)) {
|
|
|
|
$this->controller->layout = $params['layout'];
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($this->testCase) && method_exists($this->testCase, 'startController')) {
|
|
|
|
$this->testCase->startController($this->controller, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = parent::_invoke($this->controller, $params, $missingAction);
|
|
|
|
|
|
|
|
if (isset($this->testCase) && method_exists($this->testCase, 'endController')) {
|
|
|
|
$this->testCase->endController($this->controller, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-21 23:55:39 +00:00
|
|
|
* CakeTestCase class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.tests.lib
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class CakeTestCase extends UnitTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Methods used internally.
|
|
|
|
*
|
|
|
|
* @var array
|
2009-03-21 23:55:39 +00:00
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
var $methods = array('start', 'end', 'startcase', 'endcase', 'starttest', 'endtest');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +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;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-09 21:49:49 +00:00
|
|
|
/**
|
2008-09-09 22:18:49 +00:00
|
|
|
* Set this to false to avoid tables to be dropped if they already exist
|
2008-09-09 21:49:49 +00:00
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $dropTables = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Maps fixture class names to fixture identifiers as included in CakeTestCase::$fixtures
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
var $_fixtureClassMap = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-21 23:55:39 +00:00
|
|
|
* truncated property
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $__truncated = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-21 23:55:39 +00:00
|
|
|
/**
|
|
|
|
* savedGetData property
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $__savedGetData = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-21 23:55:39 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @return void
|
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function startCase() {
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Called when a test case (group of methods) has been executed (to be overriden when needed.)
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @param string $method Test method about that was executed.
|
|
|
|
* @return void
|
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function endCase() {
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Called when a test case method is about to start (to be overriden when needed.)
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @param string $method Test method about to get executed.
|
|
|
|
* @return void
|
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function startTest($method) {
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Called when a test case method has been executed (to be overriden when needed.)
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @param string $method Test method about that was executed.
|
|
|
|
* @return void
|
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function endTest($method) {
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Overrides SimpleTestCase::assert to enable calling of skipIf() from within tests
|
2009-03-21 23:55:39 +00:00
|
|
|
*
|
|
|
|
* @param Expectation $expectation
|
|
|
|
* @param mixed $compare
|
|
|
|
* @param string $message
|
|
|
|
* @return boolean|null
|
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function assert(&$expectation, $compare, $message = '%s') {
|
|
|
|
if ($this->_should_skip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return parent::assert($expectation, $compare, $message);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Overrides SimpleTestCase::skipIf to provide a boolean return value
|
2009-03-21 23:55:39 +00:00
|
|
|
*
|
|
|
|
* @param boolean $shouldSkip
|
|
|
|
* @param string $message
|
|
|
|
* @return boolean
|
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function skipIf($shouldSkip, $message = '%s') {
|
|
|
|
parent::skipIf($shouldSkip, $message);
|
|
|
|
return $shouldSkip;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Callback issued when a controller's action is about to be invoked through testAction().
|
|
|
|
*
|
|
|
|
* @param Controller $controller Controller that's about to be invoked.
|
|
|
|
* @param array $params Additional parameters as sent by testAction().
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return void
|
|
|
|
* @access public
|
2008-05-30 11:40:08 +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();
|
2008-08-13 16:24:21 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 01:22:33 +00:00
|
|
|
if ($controller->uses === false) {
|
|
|
|
$list = array($controller->modelClass);
|
|
|
|
} else {
|
|
|
|
$list = is_array($controller->uses) ? $controller->uses : array($controller->uses);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-13 16:24:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$models = array();
|
2008-08-13 01:22:33 +00:00
|
|
|
ClassRegistry::config(array('ds' => $params['connection']));
|
2008-08-13 16:24:21 +00:00
|
|
|
|
2008-08-13 01:22:33 +00:00
|
|
|
foreach ($list as $name) {
|
|
|
|
if ((is_array($params['fixturize']) && in_array($name, $params['fixturize'])) || $params['fixturize'] === true) {
|
2008-08-13 11:29:33 +00:00
|
|
|
if (class_exists($name) || App::import('Model', $name)) {
|
|
|
|
$object =& ClassRegistry::init($name);
|
2008-08-15 01:26:24 +00:00
|
|
|
//switch back to specified datasource.
|
|
|
|
$object->setDataSource($params['connection']);
|
2008-10-17 16:39:39 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($object->useDbConfig);
|
2008-08-13 01:22:33 +00:00
|
|
|
$db->cacheSources = false;
|
|
|
|
|
|
|
|
$models[$object->alias] = array(
|
|
|
|
'table' => $object->table,
|
|
|
|
'model' => $object->alias,
|
|
|
|
'key' => strtolower($name),
|
|
|
|
);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-13 01:22:33 +00:00
|
|
|
ClassRegistry::config(array('ds' => 'test_suite'));
|
2008-08-13 16:24:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($models) && isset($this->db)) {
|
2008-08-15 01:26:24 +00:00
|
|
|
$this->_actionFixtures = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
foreach ($models as $model) {
|
|
|
|
$fixture =& new CakeTestFixture($this->db);
|
|
|
|
|
|
|
|
$fixture->name = $model['model'] . 'Test';
|
|
|
|
$fixture->table = $model['table'];
|
|
|
|
$fixture->import = array('model' => $model['model'], 'records' => true);
|
|
|
|
$fixture->init();
|
|
|
|
|
2008-08-13 01:22:33 +00:00
|
|
|
$fixture->create($this->db);
|
|
|
|
$fixture->insert($this->db);
|
2008-08-15 01:26:24 +00:00
|
|
|
$this->_actionFixtures[] =& $fixture;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($models as $model) {
|
2008-08-13 16:24:21 +00:00
|
|
|
$object =& ClassRegistry::getObject($model['key']);
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($object !== false) {
|
|
|
|
$object->setDataSource('test_suite');
|
|
|
|
$object->cacheSources = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Callback issued when a controller's action has been invoked through testAction().
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @param Controller $controller Controller that has been invoked.
|
|
|
|
* @param array $params Additional parameters as sent by testAction().
|
|
|
|
* @return void
|
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function endController(&$controller, $params = array()) {
|
2009-02-21 15:05:17 +00:00
|
|
|
if (isset($this->db) && isset($this->_actionFixtures) && !empty($this->_actionFixtures) && $this->dropTables) {
|
2008-08-15 01:26:24 +00:00
|
|
|
foreach ($this->_actionFixtures as $fixture) {
|
2008-08-13 01:22:33 +00:00
|
|
|
$fixture->drop($this->db);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Executes a Cake URL, and can get (depending on the $params['return'] value):
|
|
|
|
*
|
2009-03-04 15:23:08 +00:00
|
|
|
* Params:
|
|
|
|
* - 'return' has several possible values:
|
|
|
|
* 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
|
2009-03-21 23:55:39 +00:00
|
|
|
*
|
2009-03-04 15:23:08 +00:00
|
|
|
* - 'fixturize' - Set to true if you want to copy model data from 'connection' to the test_suite connection
|
|
|
|
* - 'data' - The data you want to insert into $this->data in the controller.
|
|
|
|
* - 'connection' - Which connection to use in conjunction with fixturize (defaults to 'default')
|
|
|
|
* - 'method' - What type of HTTP method to simulate (defaults to post)
|
2009-03-21 23:55:39 +00:00
|
|
|
*
|
2009-03-04 15:23:08 +00:00
|
|
|
* @param string $url Cake URL to execute (e.g: /articles/view/455)
|
|
|
|
* @param mixed $params Parameters (see above), or simply a string of what to return
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed Whatever is returned depending of requested result
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function testAction($url, $params = array()) {
|
|
|
|
$default = array(
|
|
|
|
'return' => 'result',
|
|
|
|
'fixturize' => false,
|
|
|
|
'data' => array(),
|
2008-08-13 01:22:33 +00:00
|
|
|
'method' => 'post',
|
|
|
|
'connection' => 'default'
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (is_string($params)) {
|
|
|
|
$params = array('return' => $params);
|
|
|
|
}
|
|
|
|
$params = array_merge($default, $params);
|
|
|
|
|
2008-06-15 10:24:52 +00:00
|
|
|
$toSave = array(
|
|
|
|
'case' => null,
|
|
|
|
'group' => null,
|
|
|
|
'app' => null,
|
|
|
|
'output' => null,
|
|
|
|
'show' => null,
|
|
|
|
'plugin' => null
|
|
|
|
);
|
|
|
|
$this->__savedGetData = (empty($this->__savedGetData))
|
|
|
|
? array_intersect_key($_GET, $toSave)
|
|
|
|
: $this->__savedGetData;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-11-07 16:17:18 +00:00
|
|
|
$data = (!empty($params['data'])) ? $params['data'] : array();
|
2008-06-15 10:24:52 +00:00
|
|
|
|
|
|
|
if (strtolower($params['method']) == 'get') {
|
|
|
|
$_GET = array_merge($this->__savedGetData, $data);
|
|
|
|
$_POST = array();
|
|
|
|
} else {
|
2008-09-09 16:48:52 +00:00
|
|
|
$_POST = array('data' => $data);
|
2008-06-15 10:24:52 +00:00
|
|
|
$_GET = $this->__savedGetData;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$return = $params['return'];
|
|
|
|
$params = array_diff_key($params, array('data' => null, 'method' => null, 'return' => null));
|
|
|
|
|
|
|
|
$dispatcher =& new CakeTestDispatcher();
|
|
|
|
$dispatcher->testCase($this);
|
|
|
|
|
|
|
|
if ($return != 'result') {
|
|
|
|
if ($return != 'contents') {
|
|
|
|
$params['layout'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
@$dispatcher->dispatch($url, $params);
|
|
|
|
$result = ob_get_clean();
|
2008-08-13 16:24:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($return == 'vars') {
|
|
|
|
$view =& ClassRegistry::getObject('view');
|
|
|
|
$viewVars = $view->getVars();
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($viewVars as $var) {
|
|
|
|
$result[$var] = $view->getVar($var);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($view->pageTitle)) {
|
|
|
|
$result = array_merge($result, array('title' => $view->pageTitle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$params['return'] = 1;
|
|
|
|
$params['bare'] = 1;
|
|
|
|
$params['requested'] = 1;
|
|
|
|
|
|
|
|
$result = @$dispatcher->dispatch($url, $params);
|
|
|
|
}
|
|
|
|
|
2008-08-15 01:26:24 +00:00
|
|
|
if (isset($this->_actionFixtures)) {
|
|
|
|
unset($this->_actionFixtures);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-15 17:08:00 +00:00
|
|
|
ClassRegistry::flush();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Announces the start of a test.
|
|
|
|
*
|
|
|
|
* @param string $method Test method just started.
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function before($method) {
|
|
|
|
parent::before($method);
|
|
|
|
|
|
|
|
if (isset($this->fixtures) && (!is_array($this->fixtures) || empty($this->fixtures))) {
|
|
|
|
unset($this->fixtures);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up DB connection
|
|
|
|
if (isset($this->fixtures) && strtolower($method) == 'start') {
|
|
|
|
$this->_initDb();
|
|
|
|
$this->_loadFixtures();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create records
|
|
|
|
if (isset($this->_fixtures) && isset($this->db) && !in_array(strtolower($method), array('start', 'end')) && $this->__truncated && $this->autoFixtures == true) {
|
|
|
|
foreach ($this->_fixtures as $fixture) {
|
|
|
|
$inserts = $fixture->insert($this->db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array(strtolower($method), $this->methods)) {
|
|
|
|
$this->startTest($method);
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Runs as first test to create tables.
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function start() {
|
|
|
|
if (isset($this->_fixtures) && isset($this->db)) {
|
2008-08-07 15:36:26 +00:00
|
|
|
Configure::write('Cache.disable', true);
|
2008-05-30 11:40:08 +00:00
|
|
|
$cacheSources = $this->db->cacheSources;
|
|
|
|
$this->db->cacheSources = false;
|
|
|
|
$sources = $this->db->listSources();
|
|
|
|
$this->db->cacheSources = $cacheSources;
|
2008-06-22 14:28:45 +00:00
|
|
|
|
2008-09-09 21:49:49 +00:00
|
|
|
if (!$this->dropTables) {
|
|
|
|
return;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($this->_fixtures as $fixture) {
|
2009-07-24 16:29:18 +00:00
|
|
|
$table = $this->db->config['prefix'] . $fixture->table;
|
|
|
|
if (in_array($table, $sources)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$fixture->drop($this->db);
|
2008-10-04 16:35:53 +00:00
|
|
|
$fixture->create($this->db);
|
2009-07-24 16:29:18 +00:00
|
|
|
} elseif (!in_array($table, $sources)) {
|
2008-09-09 21:49:49 +00:00
|
|
|
$fixture->create($this->db);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Runs as last test to drop tables.
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function end() {
|
|
|
|
if (isset($this->_fixtures) && isset($this->db)) {
|
2008-09-09 21:49:49 +00:00
|
|
|
if ($this->dropTables) {
|
|
|
|
foreach (array_reverse($this->_fixtures) as $fixture) {
|
|
|
|
$fixture->drop($this->db);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-07 15:36:26 +00:00
|
|
|
$this->db->sources(true);
|
|
|
|
Configure::write('Cache.disable', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (class_exists('ClassRegistry')) {
|
|
|
|
ClassRegistry::flush();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Announces the end of a test.
|
|
|
|
*
|
|
|
|
* @param string $method Test method just finished.
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function after($method) {
|
2009-02-13 06:22:34 +00:00
|
|
|
$isTestMethod = !in_array(strtolower($method), array('start', 'end'));
|
|
|
|
|
|
|
|
if (isset($this->_fixtures) && isset($this->db) && $isTestMethod) {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($this->_fixtures as $fixture) {
|
|
|
|
$fixture->truncate($this->db);
|
|
|
|
}
|
|
|
|
$this->__truncated = true;
|
|
|
|
} else {
|
|
|
|
$this->__truncated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array(strtolower($method), $this->methods)) {
|
|
|
|
$this->endTest($method);
|
|
|
|
}
|
|
|
|
$this->_should_skip = false;
|
|
|
|
|
|
|
|
parent::after($method);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +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.
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return array List of test names.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getTests() {
|
2009-02-13 06:22:34 +00:00
|
|
|
return array_merge(
|
|
|
|
array('start', 'startCase'),
|
|
|
|
array_diff(parent::getTests(), array('testAction', 'testaction')),
|
|
|
|
array('endCase', 'end')
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Chooses which fixtures to load for a given test
|
|
|
|
*
|
2009-02-13 06:22:34 +00:00
|
|
|
* @param string $fixture Each parameter is a model name that corresponds to a
|
|
|
|
* fixture, i.e. 'Post', 'Author', etc.
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @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]];
|
2008-06-22 15:18:30 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$fixture->truncate($this->db);
|
|
|
|
$fixture->insert($this->db);
|
|
|
|
} else {
|
2010-01-16 20:23:31 +00:00
|
|
|
trigger_error(sprintf(__('Referenced fixture class %s not found', true), $class), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-02-13 06:22:34 +00:00
|
|
|
* Takes an array $expected and generates a regex from it to match the provided $string.
|
|
|
|
* Samples for $expected:
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-02-13 06:22:34 +00:00
|
|
|
* Checks for an input tag with a name attribute (contains any non-empty value) and an id
|
|
|
|
* attribute that contains 'my-input':
|
2008-05-30 11:40:08 +00:00
|
|
|
* array('input' => array('name', 'id' => 'my-input'))
|
|
|
|
*
|
|
|
|
* Checks for two p elements with some text in them:
|
|
|
|
* array(
|
|
|
|
* array('p' => true),
|
|
|
|
* 'textA',
|
|
|
|
* '/p',
|
|
|
|
* array('p' => true),
|
|
|
|
* 'textB',
|
|
|
|
* '/p'
|
|
|
|
* )
|
|
|
|
*
|
2009-02-13 06:22:34 +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:
|
2008-05-30 11:40:08 +00:00
|
|
|
* array(
|
|
|
|
* array('input' => array('name', 'id' => 'preg:/FieldName\d+/')),
|
|
|
|
* 'preg:/My\s+field/'
|
|
|
|
* )
|
|
|
|
*
|
2009-02-13 06:22:34 +00:00
|
|
|
* Important: This function is very forgiving about whitespace and also accepts any
|
|
|
|
* permutation of attribute order. It will also allow whitespaces between specified tags.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $string An HTML/XHTML/XML string
|
|
|
|
* @param array $expected An array, see above
|
|
|
|
* @param string $message SimpleTest failure output string
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return boolean
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function assertTags($string, $expected, $fullDebug = false) {
|
|
|
|
$regex = array();
|
|
|
|
$normalized = array();
|
|
|
|
foreach ((array) $expected as $key => $val) {
|
|
|
|
if (!is_numeric($key)) {
|
|
|
|
$normalized[] = array($key => $val);
|
|
|
|
} else {
|
|
|
|
$normalized[] = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$i = 0;
|
|
|
|
foreach ($normalized as $tags) {
|
2009-10-28 18:14:50 +00:00
|
|
|
if (!is_array($tags)) {
|
|
|
|
$tags = (string)$tags;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$i++;
|
|
|
|
if (is_string($tags) && $tags{0} == '<') {
|
|
|
|
$tags = array(substr($tags, 1) => array());
|
|
|
|
} elseif (is_string($tags)) {
|
2009-02-13 06:22:34 +00:00
|
|
|
$tagsTrimmed = preg_replace('/\s+/m', '', $tags);
|
|
|
|
|
|
|
|
if (preg_match('/^\*?\//', $tags, $match) && $tagsTrimmed !== '//') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$prefix = array(null, null);
|
2009-02-13 06:22:34 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($match[0] == '*/') {
|
|
|
|
$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,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!empty($tags) && preg_match('/^preg\:\/(.+)\/$/i', $tags, $matches)) {
|
|
|
|
$tags = $matches[1];
|
|
|
|
$type = 'Regex matches';
|
|
|
|
} else {
|
|
|
|
$tags = preg_quote($tags, '/');
|
|
|
|
$type = 'Text equals';
|
|
|
|
}
|
|
|
|
$regex[] = array(
|
|
|
|
sprintf('%s "%s"', $type, $tags),
|
|
|
|
$tags,
|
|
|
|
$i,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($tags as $tag => $attributes) {
|
|
|
|
$regex[] = array(
|
|
|
|
sprintf('Open %s tag', $tag),
|
|
|
|
sprintf('[\s]*<%s', preg_quote($tag, '/')),
|
|
|
|
$i,
|
|
|
|
);
|
|
|
|
if ($attributes === true) {
|
|
|
|
$attributes = array();
|
|
|
|
}
|
|
|
|
$attrs = array();
|
|
|
|
$explanations = array();
|
2010-04-05 01:43:29 +00:00
|
|
|
$i = 1;
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($attributes as $attr => $val) {
|
|
|
|
if (is_numeric($attr) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
|
|
|
|
$attrs[] = $matches[1];
|
|
|
|
$explanations[] = sprintf('Regex "%s" matches', $matches[1]);
|
|
|
|
continue;
|
|
|
|
} else {
|
2010-04-05 01:43:29 +00:00
|
|
|
$quotes = '["\']';
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_numeric($attr)) {
|
|
|
|
$attr = $val;
|
|
|
|
$val = '.+?';
|
|
|
|
$explanations[] = sprintf('Attribute "%s" present', $attr);
|
2009-02-13 06:22:34 +00:00
|
|
|
} elseif (!empty($val) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
|
2010-04-05 01:43:29 +00:00
|
|
|
$quotes = '["\']?';
|
2008-05-30 11:40:08 +00:00
|
|
|
$val = $matches[1];
|
|
|
|
$explanations[] = sprintf('Attribute "%s" matches "%s"', $attr, $val);
|
|
|
|
} else {
|
|
|
|
$explanations[] = sprintf('Attribute "%s" == "%s"', $attr, $val);
|
|
|
|
$val = preg_quote($val, '/');
|
|
|
|
}
|
2010-04-05 01:43:29 +00:00
|
|
|
$attrs[] = '[\s]+' . preg_quote($attr, '/') . '=' . $quotes . $val . $quotes;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-04-05 01:43:29 +00:00
|
|
|
$i++;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($attrs) {
|
|
|
|
$permutations = $this->__array_permute($attrs);
|
2010-04-05 01:43:29 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$permutationTokens = array();
|
|
|
|
foreach ($permutations as $permutation) {
|
2009-11-19 22:13:35 +00:00
|
|
|
$permutationTokens[] = implode('', $permutation);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$regex[] = array(
|
2009-11-19 22:13:35 +00:00
|
|
|
sprintf('%s', implode(', ', $explanations)),
|
2008-05-30 11:40:08 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->assert(new TrueExpectation(), true, '%s');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Initialize DB connection.
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
function _initDb() {
|
2008-07-01 16:42:44 +00:00
|
|
|
$testDbAvailable = in_array('test', array_keys(ConnectionManager::enumConnectionObjects()));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-08-13 16:24:21 +00:00
|
|
|
$_prefix = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($testDbAvailable) {
|
|
|
|
// Try for test DB
|
|
|
|
restore_error_handler();
|
|
|
|
@$db =& ConnectionManager::getDataSource('test');
|
|
|
|
set_error_handler('simpleTestErrorHandler');
|
|
|
|
$testDbAvailable = $db->isConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try for default DB
|
|
|
|
if (!$testDbAvailable) {
|
|
|
|
$db =& ConnectionManager::getDataSource('default');
|
2008-08-13 01:22:33 +00:00
|
|
|
$_prefix = $db->config['prefix'];
|
2008-05-30 11:40:08 +00:00
|
|
|
$db->config['prefix'] = 'test_suite_';
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionManager::create('test_suite', $db->config);
|
2008-08-13 01:22:33 +00:00
|
|
|
$db->config['prefix'] = $_prefix;
|
2008-08-13 16:24:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
// Get db connection
|
|
|
|
$this->db =& ConnectionManager::getDataSource('test_suite');
|
|
|
|
$this->db->cacheSources = false;
|
2008-07-30 15:37:26 +00:00
|
|
|
|
2008-07-30 19:53:12 +00:00
|
|
|
ClassRegistry::config(array('ds' => 'test_suite'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Load fixtures specified in var $fixtures.
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return void
|
|
|
|
* @access protected
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function _loadFixtures() {
|
|
|
|
if (!isset($this->fixtures) || empty($this->fixtures)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_array($this->fixtures)) {
|
|
|
|
$this->fixtures = array_map('trim', explode(',', $this->fixtures));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_fixtures = array();
|
|
|
|
|
|
|
|
foreach ($this->fixtures as $index => $fixture) {
|
|
|
|
$fixtureFile = null;
|
|
|
|
|
|
|
|
if (strpos($fixture, 'core.') === 0) {
|
|
|
|
$fixture = substr($fixture, strlen('core.'));
|
2009-07-22 16:11:38 +00:00
|
|
|
foreach (App::core('cake') as $key => $path) {
|
2008-12-19 23:30:13 +00:00
|
|
|
$fixturePaths[] = $path . 'tests' . DS . 'fixtures';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} elseif (strpos($fixture, 'app.') === 0) {
|
|
|
|
$fixture = substr($fixture, strlen('app.'));
|
|
|
|
$fixturePaths = array(
|
|
|
|
TESTS . 'fixtures',
|
|
|
|
VENDORS . 'tests' . DS . 'fixtures'
|
|
|
|
);
|
|
|
|
} elseif (strpos($fixture, 'plugin.') === 0) {
|
|
|
|
$parts = explode('.', $fixture, 3);
|
|
|
|
$pluginName = $parts[1];
|
|
|
|
$fixture = $parts[2];
|
|
|
|
$fixturePaths = array(
|
2009-11-11 22:14:24 +00:00
|
|
|
App::pluginPath($pluginName) . 'tests' . DS . 'fixtures',
|
2008-05-30 11:40:08 +00:00
|
|
|
TESTS . 'fixtures',
|
|
|
|
VENDORS . 'tests' . DS . 'fixtures'
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$fixturePaths = array(
|
|
|
|
TESTS . 'fixtures',
|
|
|
|
VENDORS . 'tests' . DS . 'fixtures',
|
|
|
|
TEST_CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'tests' . DS . 'fixtures'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($fixturePaths as $path) {
|
|
|
|
if (is_readable($path . DS . $fixture . '_fixture.php')) {
|
|
|
|
$fixtureFile = $path . DS . $fixture . '_fixture.php';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($fixtureFile)) {
|
|
|
|
require_once($fixtureFile);
|
|
|
|
$fixtureClass = Inflector::camelize($fixture) . 'Fixture';
|
|
|
|
$this->_fixtures[$this->fixtures[$index]] =& new $fixtureClass($this->db);
|
|
|
|
$this->_fixtureClassMap[Inflector::camelize($fixture)] = $this->fixtures[$index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->_fixtures)) {
|
|
|
|
unset($this->_fixtures);
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-21 23:55:39 +00:00
|
|
|
/**
|
|
|
|
* Generates all permutation of an array $items and returns them in a new array.
|
|
|
|
*
|
|
|
|
* @param array $items An array of items
|
|
|
|
* @return array
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2010-11-10 18:35:00 +00:00
|
|
|
}
|