2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* The TestTask handles creating and updating test files.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-10-30 17:30:26 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
|
|
|
* @since CakePHP(tm) v 1.2
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Task class for creating and updating test files.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class TestTask extends Shell {
|
|
|
|
/**
|
|
|
|
* Name of plugin
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
2008-09-24 13:49:37 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
var $plugin = null;
|
|
|
|
/**
|
|
|
|
* path to TESTS directory
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $path = TESTS;
|
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function execute() {
|
|
|
|
if (empty($this->args)) {
|
|
|
|
$this->__interactive();
|
|
|
|
}
|
2008-09-24 13:49:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (count($this->args) == 1) {
|
|
|
|
$this->__interactive($this->args[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($this->args) > 1) {
|
|
|
|
$class = Inflector::underscore($this->args[0]);
|
|
|
|
if ($this->bake($class, $this->args[1])) {
|
|
|
|
$this->out('done');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Handles interactive baking
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __interactive($class = null) {
|
|
|
|
$this->hr();
|
|
|
|
$this->out(sprintf("Bake Tests\nPath: %s", $this->path));
|
|
|
|
$this->hr();
|
2008-09-24 13:49:37 +00:00
|
|
|
|
|
|
|
$key = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
$options = array('Behavior', 'Helper', 'Component', 'Model', 'Controller');
|
2008-09-24 13:49:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($class !== null) {
|
|
|
|
$class = Inflector::camelize($class);
|
|
|
|
if (in_array($class, $options)) {
|
|
|
|
$key = array_search($class);
|
|
|
|
}
|
|
|
|
}
|
2008-09-24 13:49:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
while ($class == null) {
|
2008-09-24 13:49:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
$this->out("Select a class:");
|
|
|
|
$this->hr();
|
2008-09-24 13:49:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$keys = array();
|
|
|
|
foreach ($options as $key => $option) {
|
|
|
|
$this->out(++$key . '. ' . $option);
|
|
|
|
$keys[] = $key;
|
|
|
|
}
|
|
|
|
$keys[] = 'q';
|
2008-09-24 13:49:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$key = $this->in(__("Enter the class to test or (q)uit", true), $keys, 'q');
|
2008-09-24 13:49:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($key != 'q') {
|
|
|
|
if (isset($options[--$key])) {
|
|
|
|
$class = $options[$key];
|
|
|
|
}
|
2008-09-24 13:49:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($class) {
|
|
|
|
$name = $this->in(__("Enter the name for the test or (q)uit", true), null, 'q');
|
|
|
|
if ($name !== 'q') {
|
|
|
|
$case = null;
|
|
|
|
while ($case !== 'q') {
|
|
|
|
$case = $this->in(__("Enter a test case or (q)uit", true), null, 'q');
|
|
|
|
if ($case !== 'q') {
|
|
|
|
$cases[] = $case;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->bake($class, $name, $cases)) {
|
|
|
|
$this->out(__("Test baked\n", true));
|
|
|
|
$type = null;
|
|
|
|
}
|
|
|
|
$class = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Writes File
|
|
|
|
*
|
|
|
|
* @access public
|
2008-09-24 13:49:37 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function bake($class, $name = null, $cases = array()) {
|
|
|
|
if (!$name) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-24 13:49:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_array($cases)) {
|
|
|
|
$cases = array($cases);
|
|
|
|
}
|
2008-09-24 13:49:37 +00:00
|
|
|
|
|
|
|
if (strpos($this->path, $class) === false) {
|
|
|
|
$this->path .= 'cases' . DS . Inflector::tableize($class) . DS;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = Inflector::classify($class);
|
|
|
|
$name = Inflector::classify($name);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$import = $name;
|
|
|
|
if (isset($this->plugin)) {
|
|
|
|
$import = $this->plugin . '.' . $name;
|
|
|
|
}
|
|
|
|
$extras = $this->__extras($class);
|
|
|
|
$out = "App::import('$class', '$import');\n";
|
|
|
|
if ($class == 'Model') {
|
|
|
|
$class = null;
|
|
|
|
}
|
|
|
|
$out .= "class Test{$name} extends {$name}{$class} {\n";
|
|
|
|
$out .= "{$extras}";
|
|
|
|
$out .= "}\n\n";
|
|
|
|
$out .= "class {$name}{$class}Test extends CakeTestCase {\n";
|
|
|
|
$out .= "\n\tfunction start() {\n\t\tparent::start();\n\t\t\$this->{$name} = new Test{$name}();\n\t}\n";
|
|
|
|
$out .= "\n\tfunction test{$name}Instance() {\n";
|
|
|
|
$out .= "\t\t\$this->assertTrue(is_a(\$this->{$name}, '{$name}{$class}'));\n\t}\n";
|
|
|
|
foreach ($cases as $case) {
|
|
|
|
$case = Inflector::classify($case);
|
|
|
|
$out .= "\n\tfunction test{$case}() {\n\n\t}\n";
|
|
|
|
}
|
|
|
|
$out .= "}\n";
|
2008-09-24 13:49:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out("Baking unit test for $name...");
|
|
|
|
$this->out($out);
|
|
|
|
$ok = $this->in(__('Is this correct?'), array('y', 'n'), 'y');
|
|
|
|
if ($ok == 'n') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$header = '$Id';
|
|
|
|
$content = "<?php \n/* SVN FILE: $header$ */\n/* ". $name ." Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
|
|
|
|
return $this->createFile($this->path . Inflector::underscore($name) . '.test.php', $content);
|
|
|
|
}
|
|
|
|
/**
|
2008-09-24 13:49:37 +00:00
|
|
|
* Handles the extra stuff needed
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @access private
|
2008-09-24 13:49:37 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function __extras($class) {
|
|
|
|
$extras = null;
|
|
|
|
switch ($class) {
|
|
|
|
case 'Model':
|
|
|
|
$extras = "\n\tvar \$cacheSources = false;\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $extras;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|