2010-01-04 04:22:16 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CakeTestSuiteDispatcher controls dispatching TestSuite web based requests.
|
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-01-04 04:22:16 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2010-01-04 04:22:16 +00:00
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite
|
2010-01-04 04:22:16 +00:00
|
|
|
* @since CakePHP(tm) v 1.3
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
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.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite
|
2010-01-04 04:22:16 +00:00
|
|
|
*/
|
|
|
|
class CakeTestSuiteDispatcher {
|
2012-03-05 02:51:44 +00:00
|
|
|
|
2010-01-04 04:22:16 +00:00
|
|
|
/**
|
|
|
|
* '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,
|
2015-05-11 01:34:08 +00:00
|
|
|
'app' => false,
|
2010-01-04 04:22:16 +00:00
|
|
|
'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
|
|
|
);
|
2011-12-06 20:52:48 +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
|
|
|
|
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Base dir of the request. Used for accessing assets.
|
2010-01-06 04:12:28 +00:00
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2010-06-26 17:48:11 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2013-09-08 19:18:59 +00:00
|
|
|
* Constructor
|
2010-01-06 04:12:28 +00:00
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
public 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
|
|
|
/**
|
2012-12-22 22:48:15 +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
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if found, false otherwise
|
2010-07-07 04:35:30 +00:00
|
|
|
*/
|
|
|
|
public function loadTestFramework() {
|
2013-03-23 01:01:20 +00:00
|
|
|
if (class_exists('PHPUnit_Framework_TestCase')) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-13 09:57:50 +00:00
|
|
|
$phpunitPath = 'phpunit' . DS . 'phpunit';
|
|
|
|
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
|
|
|
|
$composerGlobalDir[] = env('APPDATA') . DS . 'Composer' . DS . 'vendor' . DS;
|
|
|
|
} else {
|
|
|
|
$composerGlobalDir[] = env('HOME') . DS . '.composer' . DS . 'vendor' . DS;
|
|
|
|
}
|
|
|
|
$vendors = array_merge(App::path('vendors'), $composerGlobalDir);
|
|
|
|
foreach ($vendors as $vendor) {
|
2013-03-18 10:38:50 +00:00
|
|
|
$vendor = rtrim($vendor, DS);
|
2014-05-13 09:57:50 +00:00
|
|
|
if (is_dir($vendor . DS . $phpunitPath)) {
|
|
|
|
ini_set('include_path', $vendor . DS . $phpunitPath . PATH_SEPARATOR . ini_get('include_path'));
|
|
|
|
break;
|
|
|
|
} elseif (is_dir($vendor . DS . 'PHPUnit')) {
|
2012-06-11 18:40:53 +00:00
|
|
|
ini_set('include_path', $vendor . PATH_SEPARATOR . ini_get('include_path'));
|
|
|
|
break;
|
2014-11-24 19:51:36 +00:00
|
|
|
} elseif (is_file($vendor . DS . 'phpunit.phar')) {
|
|
|
|
$backup = $GLOBALS['_SERVER']['SCRIPT_NAME'];
|
|
|
|
$GLOBALS['_SERVER']['SCRIPT_NAME'] = '-';
|
|
|
|
$included = include_once $vendor . DS . 'phpunit.phar';
|
|
|
|
$GLOBALS['_SERVER']['SCRIPT_NAME'] = $backup;
|
|
|
|
return $included;
|
2010-05-04 21:00:06 +00:00
|
|
|
}
|
2012-06-11 18:22:26 +00:00
|
|
|
}
|
2013-03-23 01:01:20 +00:00
|
|
|
include 'PHPUnit' . DS . 'Autoload.php';
|
|
|
|
return class_exists('PHPUnit_Framework_TestCase');
|
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
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
protected function _checkXdebug() {
|
2010-01-04 04:22:16 +00:00
|
|
|
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
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
protected 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
|
|
|
/**
|
2013-10-08 03:17:58 +00:00
|
|
|
* Parse URL params into a 'request'
|
2010-01-04 04:22:16 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
protected 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-12-20 04:13:46 +00:00
|
|
|
if (empty($this->params['plugin']) && empty($this->params['core'])) {
|
|
|
|
$this->params['app'] = true;
|
2011-02-12 23:39:25 +00:00
|
|
|
}
|
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
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
protected function _runTestCase() {
|
2011-02-12 23:39:25 +00:00
|
|
|
$commandArgs = array(
|
|
|
|
'case' => $this->params['case'],
|
2011-11-30 15:44:11 +00:00
|
|
|
'core' => $this->params['core'],
|
2011-02-12 23:39:25 +00:00
|
|
|
'app' => $this->params['app'],
|
|
|
|
'plugin' => $this->params['plugin'],
|
|
|
|
'codeCoverage' => $this->params['codeCoverage'],
|
2011-07-09 09:40:22 +00:00
|
|
|
'showPasses' => !empty($this->params['show_passes']),
|
2011-02-12 23:39:25 +00:00
|
|
|
'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 {
|
2015-07-21 08:22:53 +00:00
|
|
|
static::time();
|
2011-02-12 23:39:25 +00:00
|
|
|
$command = new CakeTestSuiteCommand('CakeTestLoader', $commandArgs);
|
2013-01-23 12:45:50 +00:00
|
|
|
$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
|
|
|
}
|
2012-03-05 02:51:44 +00:00
|
|
|
|
2012-03-28 15:49:41 +00:00
|
|
|
/**
|
|
|
|
* Sets a static timestamp
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $reset to set new static timestamp.
|
|
|
|
* @return int timestamp
|
2012-03-28 15:49:41 +00:00
|
|
|
*/
|
|
|
|
public static function time($reset = false) {
|
|
|
|
static $now;
|
|
|
|
if ($reset || !$now) {
|
|
|
|
$now = time();
|
|
|
|
}
|
|
|
|
return $now;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns formatted date string using static time
|
|
|
|
* This method is being used as formatter for created, modified and updated fields in Model::save()
|
|
|
|
*
|
|
|
|
* @param string $format format to be used.
|
|
|
|
* @return string formatted date
|
|
|
|
*/
|
|
|
|
public static function date($format) {
|
2015-07-21 08:22:53 +00:00
|
|
|
return date($format, static::time());
|
2012-03-28 15:49:41 +00:00
|
|
|
}
|
|
|
|
|
2010-01-04 04:22:16 +00:00
|
|
|
}
|