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
|
|
|
|
*
|
|
|
|
* 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.console.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4433
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class TestSuiteShell extends Shell {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The test category, "app", "core" or the name of a plugin
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $category = '';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* "group", "case" or "all"
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $type = '';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Path to the test case/group file
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $file = '';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Storage for plugins that have tests
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $plugins = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Convenience variable to avoid duplicated code
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $isPluginTest = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Stores if the user wishes to get a code coverage analysis report
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $doCoverage = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Initialization method installs Simpletest and loads all plugins
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function initialize() {
|
2009-07-22 16:11:38 +00:00
|
|
|
$corePath = App::core('cake');
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($corePath[0])) {
|
|
|
|
define('TEST_CAKE_CORE_INCLUDE_PATH', rtrim($corePath[0], DS) . DS);
|
|
|
|
} else {
|
2009-08-02 20:19:16 +00:00
|
|
|
define('TEST_CAKE_CORE_INCLUDE_PATH', CAKE_CORE_INCLUDE_PATH);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->__installSimpleTest();
|
|
|
|
|
|
|
|
require_once CAKE . 'tests' . DS . 'lib' . DS . 'test_manager.php';
|
2010-01-10 00:44:50 +00:00
|
|
|
require_once CAKE . 'tests' . DS . 'lib' . DS . 'reporter' . DS . 'cake_cli_reporter.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-06-10 23:13:39 +00:00
|
|
|
$plugins = App::objects('plugin');
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($plugins as $p) {
|
|
|
|
$this->plugins[] = Inflector::underscore($p);
|
|
|
|
}
|
2010-01-10 01:20:51 +00:00
|
|
|
$this->parseArgs();
|
|
|
|
$this->getManager();
|
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-01-10 01:20:51 +00:00
|
|
|
* Parse the arguments given into the Shell object properties.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-01-10 01:20:51 +00:00
|
|
|
function parseArgs() {
|
2010-01-10 02:25:05 +00:00
|
|
|
if (empty($this->args)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-01-10 01:20:51 +00:00
|
|
|
$this->category = $this->args[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-01-10 01:20:51 +00:00
|
|
|
if (!in_array($this->category, array('app', 'core'))) {
|
|
|
|
$this->isPluginTest = true;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-01-10 01:20:51 +00:00
|
|
|
if (isset($this->args[1])) {
|
|
|
|
$this->type = $this->args[1];
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-01-10 01:20:51 +00:00
|
|
|
if (isset($this->args[2])) {
|
|
|
|
if ($this->args[2] == 'cov') {
|
|
|
|
$this->doCoverage = true;
|
|
|
|
} else {
|
|
|
|
$this->file = Inflector::underscore($this->args[2]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
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[3]) && $this->args[3] == 'cov') {
|
|
|
|
$this->doCoverage = true;
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-01-10 01:20:51 +00:00
|
|
|
/**
|
|
|
|
* Gets a manager instance, and set the app/plugin properties.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function getManager() {
|
|
|
|
$this->Manager = new TestManager();
|
|
|
|
$this->Manager->appTest = ($this->category === 'app');
|
|
|
|
if ($this->isPluginTest) {
|
|
|
|
$this->Manager->pluginTest = $this->category;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main entry point to this shell
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function main() {
|
2010-01-10 02:25:05 +00:00
|
|
|
$this->out(__('CakePHP Test Shell', true));
|
2010-01-10 01:20:51 +00:00
|
|
|
$this->hr();
|
|
|
|
|
|
|
|
if (count($this->args) == 0) {
|
2010-01-10 02:25:05 +00:00
|
|
|
$this->error(__('Sorry, you did not pass any arguments!', true));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->__canRun()) {
|
2010-01-10 02:25:05 +00:00
|
|
|
$message = sprintf(__('Running %s %s %s', true), $this->category, $this->type, $this->file);
|
|
|
|
$this->out($message);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$exitCode = 0;
|
|
|
|
if (!$this->__run()) {
|
|
|
|
$exitCode = 1;
|
|
|
|
}
|
2010-01-10 02:25:05 +00:00
|
|
|
$this->_stop($exitCode);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-01-10 02:25:05 +00:00
|
|
|
$this->error(__('Sorry, the tests could not be found.', true));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Help screen
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function help() {
|
|
|
|
$this->out('Usage: ');
|
|
|
|
$this->out("\tcake testsuite category test_type file");
|
2009-04-30 23:38:14 +00:00
|
|
|
$this->out("\t\t- category - \"app\", \"core\" or name of a plugin");
|
|
|
|
$this->out("\t\t- test_type - \"case\", \"group\" or \"all\"");
|
|
|
|
$this->out("\t\t- test_file - file name with folder prefix and without the (test|group).php suffix");
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out('Examples: ');
|
2009-04-30 23:38:14 +00:00
|
|
|
$this->out("\t\tcake testsuite app all");
|
|
|
|
$this->out("\t\tcake testsuite core all");
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2009-04-30 23:38:14 +00:00
|
|
|
$this->out("\t\tcake testsuite app case behaviors/debuggable");
|
|
|
|
$this->out("\t\tcake testsuite app case models/my_model");
|
|
|
|
$this->out("\t\tcake testsuite app case controllers/my_controller");
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2009-04-30 23:38:14 +00:00
|
|
|
$this->out("\t\tcake testsuite core case file");
|
|
|
|
$this->out("\t\tcake testsuite core case router");
|
|
|
|
$this->out("\t\tcake testsuite core case set");
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2009-04-30 23:38:14 +00:00
|
|
|
$this->out("\t\tcake testsuite app group mygroup");
|
|
|
|
$this->out("\t\tcake testsuite core group acl");
|
|
|
|
$this->out("\t\tcake testsuite core group socket");
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2009-04-30 23:38:14 +00:00
|
|
|
$this->out("\t\tcake testsuite bugs case models/bug");
|
|
|
|
$this->out("\t\t // for the plugin 'bugs' and its test case 'models/bug'");
|
|
|
|
$this->out("\t\tcake testsuite bugs group bug");
|
|
|
|
$this->out("\t\t // for the plugin bugs and its test group 'bug'");
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out('Code Coverage Analysis: ');
|
|
|
|
$this->out("\n\nAppend 'cov' to any of the above in order to enable code coverage analysis");
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Checks if the arguments supplied point to a valid test file and thus the shell can be run.
|
|
|
|
*
|
|
|
|
* @return bool true if it's a valid test file, false otherwise
|
|
|
|
* @access private
|
|
|
|
*/
|
2008-06-20 20:17:23 +00:00
|
|
|
function __canRun() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$isNeitherAppNorCore = !in_array($this->category, array('app', 'core'));
|
|
|
|
$isPlugin = in_array(Inflector::underscore($this->category), $this->plugins);
|
|
|
|
|
|
|
|
if ($isNeitherAppNorCore && !$isPlugin) {
|
2010-01-10 02:25:05 +00:00
|
|
|
$message = sprintf(
|
|
|
|
__('%s is an invalid test category (either "app", "core" or name of a plugin)', true),
|
|
|
|
$this->category
|
|
|
|
);
|
|
|
|
$this->error($message);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$folder = $this->__findFolderByCategory($this->category);
|
|
|
|
if (!file_exists($folder)) {
|
2010-01-10 02:25:05 +00:00
|
|
|
$this->err(sprintf(__('%s not found', true), $folder));
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($this->type, array('all', 'group', 'case'))) {
|
2010-01-10 02:25:05 +00:00
|
|
|
$this->err(sprintf(__('%s is invalid. Should be case, group or all', true), $this->type));
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-11 23:04:38 +00:00
|
|
|
$fileName = $this->__getFileName($folder, $this->isPluginTest);
|
|
|
|
if ($fileName === true || file_exists($folder . $fileName)) {
|
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-11 23:04:38 +00:00
|
|
|
|
2010-01-10 02:25:05 +00:00
|
|
|
$message = sprintf(
|
|
|
|
__('%s %s %s is an invalid test identifier', true),
|
|
|
|
$this->category, $this->type, $this->file
|
|
|
|
);
|
|
|
|
$this->err($message);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Executes the tests depending on our settings
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __run() {
|
2010-01-10 04:00:03 +00:00
|
|
|
$Reporter = new CakeCliReporter('utf-8', array(
|
|
|
|
'app' => $this->Manager->appTest,
|
|
|
|
'plugin' => $this->Manager->pluginTest,
|
2010-01-11 23:04:38 +00:00
|
|
|
'group' => ($this->type === 'group'),
|
|
|
|
'codeCoverage' => $this->doCoverage
|
2010-01-10 04:00:03 +00:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($this->type == 'all') {
|
2010-01-10 01:20:51 +00:00
|
|
|
return $this->Manager->runAllTests($Reporter);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->doCoverage) {
|
|
|
|
if (!extension_loaded('xdebug')) {
|
2010-01-10 02:25:05 +00:00
|
|
|
$this->out(__('You must install Xdebug to use the CakePHP(tm) Code Coverage Analyzation. Download it from http://www.xdebug.org/docs/install', true));
|
2010-01-11 23:04:38 +00:00
|
|
|
$this->_stop(0);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->type == 'group') {
|
|
|
|
$ucFirstGroup = ucfirst($this->file);
|
|
|
|
if ($this->doCoverage) {
|
|
|
|
require_once CAKE . 'tests' . DS . 'lib' . DS . 'code_coverage_manager.php';
|
2010-01-10 20:55:09 +00:00
|
|
|
CodeCoverageManager::init($ucFirstGroup, $Reporter);
|
|
|
|
CodeCoverageManager::start();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-10 01:20:51 +00:00
|
|
|
$result = $this->Manager->runGroupTest($ucFirstGroup, $Reporter);
|
2008-05-30 11:40:08 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2010-01-11 23:04:38 +00:00
|
|
|
|
|
|
|
$folder = $folder = $this->__findFolderByCategory($this->category);
|
|
|
|
$case = $this->__getFileName($folder, $this->isPluginTest);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($this->doCoverage) {
|
|
|
|
require_once CAKE . 'tests' . DS . 'lib' . DS . 'code_coverage_manager.php';
|
2010-01-10 20:55:09 +00:00
|
|
|
CodeCoverageManager::init($case, $Reporter);
|
|
|
|
CodeCoverageManager::start();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-10 01:20:51 +00:00
|
|
|
$result = $this->Manager->runTestCase($case, $Reporter);
|
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
|
|
|
/**
|
2010-01-11 23:04:38 +00:00
|
|
|
* Gets the concrete filename for the inputted test name and category/type
|
|
|
|
*
|
|
|
|
* @param string $folder Folder name to look for files in.
|
|
|
|
* @param boolean $isPlugin If the test case is a plugin.
|
|
|
|
* @return mixed Either string filename or boolean false on failure. Or true if the type is 'all'
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __getFileName($folder, $isPlugin) {
|
|
|
|
$ext = $this->Manager->getExtension($this->type);
|
|
|
|
switch ($this->type) {
|
|
|
|
case 'all':
|
|
|
|
return true;
|
|
|
|
case 'group':
|
|
|
|
return $this->file . $ext;
|
|
|
|
case 'case':
|
|
|
|
if ($this->category == 'app' || $isPlugin) {
|
|
|
|
return $this->file . $ext;
|
|
|
|
}
|
|
|
|
$coreCase = $this->file . $ext;
|
|
|
|
$coreLibCase = 'libs' . DS . $this->file . $ext;
|
|
|
|
|
|
|
|
if ($this->category == 'core' && file_exists($folder . DS . $coreCase)) {
|
|
|
|
return $coreCase;
|
|
|
|
} elseif ($this->category == 'core' && file_exists($folder . DS . $coreLibCase)) {
|
|
|
|
return $coreLibCase;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the correct folder to look for tests for based on the input category and type.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-11 23:04:38 +00:00
|
|
|
* @param string $category The category of the test. Either 'app', 'core' or a plugin name.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string the folder path
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __findFolderByCategory($category) {
|
|
|
|
$folder = '';
|
|
|
|
$paths = array(
|
|
|
|
'core' => CAKE,
|
2010-01-10 00:44:50 +00:00
|
|
|
'app' => APP
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
2010-01-11 23:04:38 +00:00
|
|
|
$typeDir = $this->type === 'group' ? 'groups' : 'cases';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (array_key_exists($category, $paths)) {
|
2010-01-11 23:04:38 +00:00
|
|
|
$folder = $paths[$category] . 'tests' . DS . $typeDir . DS;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-01-10 01:20:51 +00:00
|
|
|
$pluginPath = App::pluginPath($category);
|
|
|
|
if (is_dir($pluginPath . 'tests')) {
|
2010-01-11 23:04:38 +00:00
|
|
|
$folder = $pluginPath . 'tests' . DS . $typeDir . DS;
|
2009-04-30 23:38:14 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-04-30 23:38:14 +00:00
|
|
|
return $folder;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* tries to install simpletest and exits gracefully if it is not there
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __installSimpleTest() {
|
|
|
|
if (!App::import('Vendor', 'simpletest' . DS . 'reporter')) {
|
2010-01-10 02:25:05 +00:00
|
|
|
$this->err(__('Sorry, Simpletest could not be found. Download it from http://simpletest.org and install it to your vendors directory.', true));
|
2008-05-30 11:40:08 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|