2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Test Suite Shell
|
|
|
|
*
|
|
|
|
* This Shell allows the running of test suites via the cake command line
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, 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
|
|
|
* @since CakePHP(tm) v 1.2.0.4433
|
2010-10-03 16:38:58 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-08 06:19:36 +00:00
|
|
|
|
2011-03-08 18:48:46 +00:00
|
|
|
App::uses('Shell', 'Console');
|
2010-12-08 06:19:36 +00:00
|
|
|
App::uses('CakeTestSuiteDispatcher', 'TestSuite');
|
2011-02-14 04:26:41 +00:00
|
|
|
App::uses('CakeTestSuiteCommand', 'TestSuite');
|
|
|
|
App::uses('CakeTestLoader', 'TestSuite');
|
2010-12-08 06:19:36 +00:00
|
|
|
|
2011-07-26 01:46:52 +00:00
|
|
|
/**
|
|
|
|
* Provides a CakePHP wrapper around PHPUnit.
|
|
|
|
* Adds in CakePHP's fixtures and gives access to plugin, app and core test cases
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console.Command
|
2011-07-26 01:46:52 +00:00
|
|
|
*/
|
2011-03-08 18:48:46 +00:00
|
|
|
class TestsuiteShell extends Shell {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-06-26 17:49:21 +00:00
|
|
|
/**
|
|
|
|
* Dispatcher object for the run.
|
|
|
|
*
|
|
|
|
* @var CakeTestDispatcher
|
|
|
|
*/
|
|
|
|
protected $_dispatcher = null;
|
|
|
|
|
2010-10-12 03:06:50 +00:00
|
|
|
/**
|
|
|
|
* get the option parser for the test suite.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getOptionParser() {
|
2010-10-15 02:15:01 +00:00
|
|
|
$parser = new ConsoleOptionParser($this->name);
|
2010-10-12 03:06:50 +00:00
|
|
|
$parser->description(array(
|
2011-04-25 19:17:59 +00:00
|
|
|
__d('cake_console', 'The CakePHP Testsuite allows you to run test cases from the command line'),
|
|
|
|
__d('cake_console', 'If run with no command line arguments, a list of available core test cases will be shown')
|
2010-10-12 03:06:50 +00:00
|
|
|
))->addArgument('category', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'app, core or name of a plugin.'),
|
2010-10-12 03:06:50 +00:00
|
|
|
'required' => true
|
|
|
|
))->addArgument('file', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'file name with folder prefix and without the test.php suffix.'),
|
2010-11-03 05:57:37 +00:00
|
|
|
'required' => false,
|
2010-11-03 05:55:27 +00:00
|
|
|
))->addOption('log-junit', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<file> Log test execution in JUnit XML format to file.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('log-json', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('log-tap', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('log-dbus', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Log test execution to DBUS.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
2010-11-22 03:59:49 +00:00
|
|
|
))->addOption('coverage-html', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<dir> Generate code coverage report in HTML format.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('coverage-clover', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<file> Write code coverage data in Clover XML format.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('testdox-html', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<file> Write agile documentation in HTML format to file.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('testdox-text', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<file> Write agile documentation in Text format to file.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
2010-10-14 03:40:14 +00:00
|
|
|
))->addOption('filter', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<pattern> Filter which tests to run.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('group', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<name> Only runs tests from the specified group(s).'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('exclude-group', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<name> Exclude tests from the specified group(s).'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('list-groups', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'List available test groups.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('loader', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'TestSuiteLoader implementation to use.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('repeat', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<times> Runs the test(s) repeatedly.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('tap', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Report test execution progress in TAP format.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('testdox', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Report test execution progress in TestDox format.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false,
|
|
|
|
'boolean' => true
|
|
|
|
))->addOption('no-colors', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Do not use colors in output.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('stderr', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Write to STDERR instead of STDOUT.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
2010-11-04 00:10:48 +00:00
|
|
|
))->addOption('stop-on-error', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Stop execution upon first error or failure.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
2010-11-04 00:10:48 +00:00
|
|
|
))->addOption('stop-on-failure', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Stop execution upon first failure.'),
|
2010-11-04 00:10:48 +00:00
|
|
|
'boolean' => true
|
2010-11-03 05:55:27 +00:00
|
|
|
))->addOption('stop-on-skipped ', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Stop execution upon first skipped test.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('stop-on-incomplete', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Stop execution upon first incomplete test.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('strict', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Mark a test as incomplete if no assertions are made.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('wait', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Waits for a keystroke after each test.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('process-isolation', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Run each test in a separate PHP process.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('no-globals-backup', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Do not backup and restore $GLOBALS for each test.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('static-backup ', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Backup and restore static attributes for each test.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('syntax-check', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Try to check source files for syntax errors.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('bootstrap', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<file> A "bootstrap" PHP file that is run before the tests.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
2011-03-08 19:13:00 +00:00
|
|
|
))->addOption('configuration', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<file> Read configuration from XML file.'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('no-configuration', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Ignore default configuration file (phpunit.xml).'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'boolean' => true
|
|
|
|
))->addOption('include-path', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', '<path(s)> Prepend PHP include_path with given path(s).'),
|
2010-11-03 05:55:27 +00:00
|
|
|
'default' => false
|
|
|
|
))->addOption('directive', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'key[=value] Sets a php.ini value.'),
|
2010-10-14 03:40:14 +00:00
|
|
|
'default' => false
|
2011-02-12 22:36:00 +00:00
|
|
|
))->addOption('fixture', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Choose a custom fixture manager.'),
|
2010-10-12 03:06:50 +00:00
|
|
|
));
|
2010-11-03 05:55:27 +00:00
|
|
|
|
2010-10-12 03:06:50 +00:00
|
|
|
return $parser;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-04-25 19:17:59 +00:00
|
|
|
* Initialization method installs PHPUnit and loads all plugins
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function initialize() {
|
2010-06-26 17:49:21 +00:00
|
|
|
$this->_dispatcher = new CakeTestSuiteDispatcher();
|
2011-04-21 17:56:50 +00:00
|
|
|
$sucess = $this->_dispatcher->loadTestFramework();
|
|
|
|
if (!$sucess) {
|
2011-04-21 17:58:49 +00:00
|
|
|
throw new Exception(__d('cake_dev', 'Please install PHPUnit framework <info>(http://www.phpunit.de)</info>'));
|
2011-04-21 17:56:50 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-06-26 17:49:21 +00:00
|
|
|
* Parse the CLI options into an array CakeTestDispatcher can use.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-06-26 17:49:21 +00:00
|
|
|
* @return array Array of params for CakeTestDispatcher
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-07 04:37:22 +00:00
|
|
|
protected function parseArgs() {
|
2010-01-10 02:25:05 +00:00
|
|
|
if (empty($this->args)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-26 17:49:21 +00:00
|
|
|
$params = array(
|
2011-02-12 20:51:00 +00:00
|
|
|
'core' => false,
|
2010-06-26 17:49:21 +00:00
|
|
|
'app' => false,
|
|
|
|
'plugin' => null,
|
|
|
|
'output' => 'text',
|
|
|
|
);
|
|
|
|
|
|
|
|
$category = $this->args[0];
|
|
|
|
|
2011-02-12 20:51:00 +00:00
|
|
|
if ($category == 'core') {
|
|
|
|
$params['core'] = true;
|
|
|
|
} elseif ($category == 'app') {
|
2010-06-26 17:49:21 +00:00
|
|
|
$params['app'] = true;
|
|
|
|
} elseif ($category != 'core') {
|
|
|
|
$params['plugin'] = $category;
|
2010-01-10 01:20:51 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-01-10 01:20:51 +00:00
|
|
|
if (isset($this->args[1])) {
|
2011-04-12 02:53:03 +00:00
|
|
|
$params['case'] = $this->args[1];
|
2010-01-10 01:20:51 +00:00
|
|
|
}
|
2010-06-26 17:49:21 +00:00
|
|
|
return $params;
|
2010-01-10 01:20:51 +00:00
|
|
|
}
|
|
|
|
|
2010-07-07 04:41:06 +00:00
|
|
|
/**
|
|
|
|
* Converts the options passed to the shell as options for the PHPUnit cli runner
|
|
|
|
*
|
|
|
|
* @return array Array of params for CakeTestDispatcher
|
|
|
|
*/
|
|
|
|
protected function runnerOptions() {
|
|
|
|
$options = array();
|
2010-10-12 03:43:13 +00:00
|
|
|
$params = $this->params;
|
|
|
|
unset($params['help']);
|
2010-11-03 05:55:27 +00:00
|
|
|
|
|
|
|
if (!empty($params['no-colors'])) {
|
|
|
|
unset($params['no-colors'], $params['colors']);
|
|
|
|
} else {
|
|
|
|
$params['colors'] = true;
|
|
|
|
}
|
|
|
|
|
2010-10-12 03:43:13 +00:00
|
|
|
foreach ($params as $param => $value) {
|
2010-11-03 05:55:27 +00:00
|
|
|
if ($value === false) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-11 05:33:24 +00:00
|
|
|
$options[] = '--' . $param;
|
|
|
|
if (is_string($value)) {
|
|
|
|
$options[] = $value;
|
2010-07-07 04:41:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2010-01-10 01:20:51 +00:00
|
|
|
/**
|
|
|
|
* Main entry point to this shell
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function main() {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'CakePHP Test Shell'));
|
2010-01-10 01:20:51 +00:00
|
|
|
$this->hr();
|
|
|
|
|
2010-07-14 00:53:45 +00:00
|
|
|
$args = $this->parseArgs();
|
|
|
|
|
|
|
|
if (empty($args['case'])) {
|
2010-11-03 23:26:06 +00:00
|
|
|
return $this->available();
|
2010-07-14 00:53:45 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 01:58:21 +00:00
|
|
|
$this->run($args, $this->runnerOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the test case from $runnerArgs
|
|
|
|
*
|
|
|
|
* @param array $runnerArgs list of arguments as obtained from parseArgs()
|
|
|
|
* @param array $options list of options as constructed by runnerOptions()
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function run($runnerArgs, $options = array()) {
|
2010-11-29 02:05:12 +00:00
|
|
|
restore_error_handler();
|
|
|
|
restore_error_handler();
|
|
|
|
|
2011-02-12 21:08:48 +00:00
|
|
|
$testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
|
2010-07-14 01:58:21 +00:00
|
|
|
$testCli->run($options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-13 03:54:59 +00:00
|
|
|
/**
|
|
|
|
* Shows a list of available test cases and gives the option to run one of them
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function available() {
|
|
|
|
$params = $this->parseArgs();
|
2011-02-13 04:07:39 +00:00
|
|
|
$testCases = CakeTestLoader::generateTestList($params);
|
2010-07-13 03:54:59 +00:00
|
|
|
$app = $params['app'];
|
|
|
|
$plugin = $params['plugin'];
|
|
|
|
|
|
|
|
$title = "Core Test Cases:";
|
|
|
|
$category = 'core';
|
|
|
|
if ($app) {
|
|
|
|
$title = "App Test Cases:";
|
|
|
|
$category = 'app';
|
|
|
|
} elseif ($plugin) {
|
|
|
|
$title = Inflector::humanize($plugin) . " Test Cases:";
|
|
|
|
$category = $plugin;
|
|
|
|
}
|
2010-07-14 00:53:45 +00:00
|
|
|
|
2010-07-13 03:54:59 +00:00
|
|
|
if (empty($testCases)) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', "No test cases available \n\n"));
|
2010-11-17 04:32:23 +00:00
|
|
|
return $this->out($this->OptionParser->help());
|
2010-07-13 03:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->out($title);
|
|
|
|
$i = 1;
|
|
|
|
$cases = array();
|
|
|
|
foreach ($testCases as $testCaseFile => $testCase) {
|
2011-04-21 18:01:34 +00:00
|
|
|
$case = str_replace('Test.php', '', $testCase);
|
2010-07-13 03:54:59 +00:00
|
|
|
$this->out("[$i] $case");
|
|
|
|
$cases[$i] = $case;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
2011-03-19 17:32:35 +00:00
|
|
|
while ($choice = $this->in(__d('cake_console', 'What test case would you like to run?'), null, 'q')) {
|
2010-07-13 03:54:59 +00:00
|
|
|
if (is_numeric($choice) && isset($cases[$choice])) {
|
|
|
|
$this->args[0] = $category;
|
|
|
|
$this->args[1] = $cases[$choice];
|
2010-07-14 01:58:21 +00:00
|
|
|
$this->run($this->parseArgs(), $this->runnerOptions());
|
2010-07-13 03:54:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($choice) && in_array($choice, $cases)) {
|
|
|
|
$this->args[0] = $category;
|
|
|
|
$this->args[1] = $choice;
|
2010-07-14 01:58:21 +00:00
|
|
|
$this->run($this->parseArgs(), $this->runnerOptions());
|
2010-07-13 03:54:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($choice == 'q') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-26 01:46:52 +00:00
|
|
|
}
|