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