2010-01-04 04:22:16 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CakeTestSuiteDispatcher controls dispatching TestSuite web based requests.
|
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2010-01-04 04:22:16 +00:00
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2010-10-24 20:58:22 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-01-04 04:22:16 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2010-01-04 04:22:16 +00:00
|
|
|
*
|
2010-10-24 20:58:22 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-01-26 22:03:03 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.lib
|
2010-01-04 04:22:16 +00:00
|
|
|
* @since CakePHP(tm) v 1.3
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2010-01-04 04:22:16 +00:00
|
|
|
*/
|
|
|
|
|
2011-04-17 10:35:21 +00:00
|
|
|
define('CORE_TEST_CASES', CAKE . 'Test' . DS . 'Case');
|
2011-04-11 00:38:24 +00:00
|
|
|
define('APP_TEST_CASES', TESTS . 'Case');
|
2011-02-14 04:26:41 +00:00
|
|
|
|
|
|
|
App::uses('CakeTestSuiteCommand', 'TestSuite');
|
|
|
|
|
2010-01-04 04:22:16 +00:00
|
|
|
/**
|
|
|
|
* CakeTestSuiteDispatcher handles web requests to the test suite and runs the correct action.
|
|
|
|
*
|
|
|
|
* @package cake.tests.libs
|
|
|
|
*/
|
|
|
|
class CakeTestSuiteDispatcher {
|
|
|
|
/**
|
|
|
|
* 'Request' parameters
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $params = array(
|
2010-01-04 04:22:16 +00:00
|
|
|
'codeCoverage' => false,
|
|
|
|
'case' => null,
|
2011-02-22 13:41:36 +00:00
|
|
|
'core' => false,
|
2010-01-04 04:22:16 +00:00
|
|
|
'app' => false,
|
|
|
|
'plugin' => null,
|
|
|
|
'output' => 'html',
|
2010-01-06 03:20:32 +00:00
|
|
|
'show' => 'groups',
|
2010-05-27 02:21:34 +00:00
|
|
|
'show_passes' => false,
|
2011-02-13 20:15:01 +00:00
|
|
|
'filter' => false,
|
|
|
|
'fixture' => null
|
2010-01-04 04:22:16 +00:00
|
|
|
);
|
2010-01-06 04:12:28 +00:00
|
|
|
/**
|
|
|
|
* Baseurl for the request
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_baseUrl;
|
2010-01-06 04:12:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base dir of the request. Used for accessing assets.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_baseDir;
|
2010-01-06 04:12:28 +00:00
|
|
|
|
2010-06-26 17:48:11 +00:00
|
|
|
/**
|
|
|
|
* boolean to set auto parsing of params.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $_paramsParsed = false;
|
|
|
|
|
2010-04-15 03:24:44 +00:00
|
|
|
/**
|
|
|
|
* reporter instance used for the request
|
|
|
|
*
|
|
|
|
* @var CakeBaseReporter
|
|
|
|
*/
|
|
|
|
protected static $_Reporter = null;
|
|
|
|
|
2010-01-06 04:12:28 +00:00
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-15 02:56:51 +00:00
|
|
|
function __construct() {
|
2010-01-06 04:12:28 +00:00
|
|
|
$this->_baseUrl = $_SERVER['PHP_SELF'];
|
2010-03-19 21:20:51 +00:00
|
|
|
$dir = rtrim(dirname($this->_baseUrl), '\\');
|
2010-01-14 03:29:10 +00:00
|
|
|
$this->_baseDir = ($dir === '/') ? $dir : $dir . '/';
|
2010-01-06 04:12:28 +00:00
|
|
|
}
|
|
|
|
|
2010-01-04 04:22:16 +00:00
|
|
|
/**
|
|
|
|
* Runs the actions required by the URL parameters.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function dispatch() {
|
2010-05-04 21:00:06 +00:00
|
|
|
$this->_checkPHPUnit();
|
2010-01-04 04:22:16 +00:00
|
|
|
$this->_parseParams();
|
|
|
|
|
2010-06-26 16:58:03 +00:00
|
|
|
if ($this->params['case']) {
|
2010-06-26 17:48:11 +00:00
|
|
|
$value = $this->_runTestCase();
|
2010-01-04 04:22:16 +00:00
|
|
|
} else {
|
2010-06-26 17:48:11 +00:00
|
|
|
$value = $this->_testCaseList();
|
2010-01-04 04:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$output = ob_get_clean();
|
|
|
|
echo $output;
|
2010-06-26 17:48:11 +00:00
|
|
|
return $value;
|
2010-01-04 04:22:16 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 03:33:07 +00:00
|
|
|
/**
|
|
|
|
* Static method to initialize the test runner, keeps global space clean
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function run() {
|
|
|
|
$dispatcher = new CakeTestSuiteDispatcher();
|
|
|
|
$dispatcher->dispatch();
|
|
|
|
}
|
|
|
|
|
2010-01-04 04:22:16 +00:00
|
|
|
/**
|
2010-05-04 21:00:06 +00:00
|
|
|
* Checks that PHPUnit is installed. Will exit if it doesn't
|
2010-01-04 04:22:16 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-07-07 04:35:30 +00:00
|
|
|
protected function _checkPHPUnit() {
|
|
|
|
$found = $this->loadTestFramework();
|
|
|
|
if (!$found) {
|
|
|
|
$baseDir = $this->_baseDir;
|
2011-04-17 10:25:02 +00:00
|
|
|
include CAKE . 'TestSuite' . DS . 'templates' . DS . 'phpunit.php';
|
2010-07-07 04:35:30 +00:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for the existence of the test framework files
|
|
|
|
*
|
|
|
|
* @return boolean true if found, false otherwise
|
|
|
|
*/
|
|
|
|
public function loadTestFramework() {
|
2010-05-04 18:10:50 +00:00
|
|
|
$found = $path = null;
|
2010-05-04 21:00:06 +00:00
|
|
|
|
2010-09-28 03:07:23 +00:00
|
|
|
if (@include 'PHPUnit' . DS . 'Autoload.php') {
|
2010-05-04 21:00:06 +00:00
|
|
|
$found = true;
|
2010-05-04 18:10:50 +00:00
|
|
|
}
|
|
|
|
|
2010-05-04 21:00:06 +00:00
|
|
|
if (!$found) {
|
|
|
|
foreach (App::path('vendors') as $vendor) {
|
|
|
|
if (is_dir($vendor . 'PHPUnit')) {
|
|
|
|
$path = $vendor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($path && ini_set('include_path', $path . PATH_SEPARATOR . ini_get('include_path'))) {
|
2010-09-28 03:07:23 +00:00
|
|
|
$found = include 'PHPUnit' . DS . 'Autoload.php';
|
2010-05-04 21:00:06 +00:00
|
|
|
}
|
2010-05-04 18:10:50 +00:00
|
|
|
}
|
2010-07-07 04:35:30 +00:00
|
|
|
if ($found) {
|
2010-09-28 03:07:23 +00:00
|
|
|
PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT');
|
2010-01-04 04:22:16 +00:00
|
|
|
}
|
2010-07-07 04:35:30 +00:00
|
|
|
return $found;
|
2010-01-04 04:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for the xdebug extension required to do code coverage. Displays an error
|
|
|
|
* if xdebug isn't installed.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function _checkXdebug() {
|
|
|
|
if (!extension_loaded('xdebug')) {
|
2010-01-13 13:02:45 +00:00
|
|
|
$baseDir = $this->_baseDir;
|
2011-04-17 10:25:02 +00:00
|
|
|
include CAKE . 'TestSuite' . DS . 'templates' . DS . 'xdebug.php';
|
2010-01-04 04:22:16 +00:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-06 04:16:12 +00:00
|
|
|
/**
|
|
|
|
* Generates a page containing the a list of test cases that could be run.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function _testCaseList() {
|
2011-02-13 03:32:16 +00:00
|
|
|
$command = new CakeTestSuiteCommand('', $this->params);
|
|
|
|
$Reporter = $command->handleReporter($this->params['output']);
|
2010-01-09 16:20:47 +00:00
|
|
|
$Reporter->paintDocumentStart();
|
2010-01-07 03:52:04 +00:00
|
|
|
$Reporter->paintTestMenu();
|
2010-01-07 04:02:37 +00:00
|
|
|
$Reporter->testCaseList();
|
2010-01-07 03:52:04 +00:00
|
|
|
$Reporter->paintDocumentEnd();
|
2010-01-06 04:16:12 +00:00
|
|
|
}
|
|
|
|
|
2010-06-26 17:48:11 +00:00
|
|
|
/**
|
|
|
|
* Sets the params, calling this will bypass the auto parameter parsing.
|
|
|
|
*
|
|
|
|
* @param array $params Array of parameters for the dispatcher
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setParams($params) {
|
|
|
|
$this->params = $params;
|
|
|
|
$this->_paramsParsed = true;
|
|
|
|
}
|
|
|
|
|
2010-01-04 04:22:16 +00:00
|
|
|
/**
|
|
|
|
* Parse url params into a 'request'
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function _parseParams() {
|
2010-06-26 17:48:11 +00:00
|
|
|
if (!$this->_paramsParsed) {
|
|
|
|
if (!isset($_SERVER['SERVER_NAME'])) {
|
|
|
|
$_SERVER['SERVER_NAME'] = '';
|
|
|
|
}
|
|
|
|
foreach ($this->params as $key => $value) {
|
|
|
|
if (isset($_GET[$key])) {
|
|
|
|
$this->params[$key] = $_GET[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($_GET['code_coverage'])) {
|
|
|
|
$this->params['codeCoverage'] = true;
|
|
|
|
$this->_checkXdebug();
|
2010-01-04 04:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-12 23:39:25 +00:00
|
|
|
if (empty($this->params['plugin']) && empty($this->params['app'])) {
|
|
|
|
$this->params['core'] = true;
|
|
|
|
}
|
2010-01-06 14:50:39 +00:00
|
|
|
$this->params['baseUrl'] = $this->_baseUrl;
|
|
|
|
$this->params['baseDir'] = $this->_baseDir;
|
2010-01-04 04:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs a test case file.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function _runTestCase() {
|
2011-02-12 23:39:25 +00:00
|
|
|
$commandArgs = array(
|
|
|
|
'case' => $this->params['case'],
|
|
|
|
'core' =>$this->params['core'],
|
|
|
|
'app' => $this->params['app'],
|
|
|
|
'plugin' => $this->params['plugin'],
|
|
|
|
'codeCoverage' => $this->params['codeCoverage'],
|
|
|
|
'baseUrl' => $this->_baseUrl,
|
|
|
|
'baseDir' => $this->_baseDir,
|
|
|
|
);
|
2011-04-17 10:25:02 +00:00
|
|
|
|
2011-02-12 23:39:25 +00:00
|
|
|
$options = array(
|
|
|
|
'--filter', $this->params['filter'],
|
2011-02-13 20:15:01 +00:00
|
|
|
'--output', $this->params['output'],
|
|
|
|
'--fixture', $this->params['fixture']
|
2011-02-12 23:39:25 +00:00
|
|
|
);
|
2011-02-13 18:08:43 +00:00
|
|
|
restore_error_handler();
|
2011-02-12 23:39:25 +00:00
|
|
|
|
2010-09-20 02:41:31 +00:00
|
|
|
try {
|
2011-02-12 23:39:25 +00:00
|
|
|
$command = new CakeTestSuiteCommand('CakeTestLoader', $commandArgs);
|
|
|
|
$result = $command->run($options);
|
2010-09-20 02:41:31 +00:00
|
|
|
} catch (MissingConnectionException $exception) {
|
|
|
|
ob_end_clean();
|
|
|
|
$baseDir = $this->_baseDir;
|
2011-04-17 10:25:02 +00:00
|
|
|
include CAKE . 'TestSuite' . DS . 'templates' . DS . 'missing_connection.php';
|
2010-09-20 02:41:31 +00:00
|
|
|
exit();
|
|
|
|
}
|
2010-01-04 04:22:16 +00:00
|
|
|
}
|
|
|
|
}
|