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) {
|
2009-02-13 14:22:11 +00:00
|
|
|
$cases = array();
|
|
|
|
$this->hr();
|
|
|
|
$this->out("Select a class:");
|
|
|
|
$this->hr();
|
|
|
|
|
|
|
|
$keys = array();
|
|
|
|
foreach ($options as $key => $option) {
|
|
|
|
$this->out(++$key . '. ' . $option);
|
|
|
|
$keys[] = $key;
|
|
|
|
}
|
|
|
|
$keys[] = 'q';
|
2008-09-24 13:49:37 +00:00
|
|
|
|
2009-02-13 14:22:11 +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) {
|
2009-02-13 14:22:11 +00:00
|
|
|
$this->filePath = $this->path . 'cases' . DS . Inflector::tableize($class) . DS;
|
2008-09-24 13:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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";
|
2008-12-03 20:24:24 +00:00
|
|
|
$out .= "\n\tfunction startTest() {";
|
|
|
|
$out .= "\n\t\t\$this->{$name} = new Test{$name}();";
|
|
|
|
$out .= "\n\t}\n";
|
2008-05-30 11:40:08 +00:00
|
|
|
$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);
|
2009-02-13 14:22:11 +00:00
|
|
|
$ok = $this->in(__('Is this correct?', true), array('y', 'n'), 'y');
|
2008-05-30 11:40:08 +00:00
|
|
|
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}?>";
|
2009-02-13 14:22:11 +00:00
|
|
|
return $this->createFile($this->filePath . Inflector::underscore($name) . '.test.php', $content);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
/**
|
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':
|
2008-12-03 20:24:24 +00:00
|
|
|
$extras = "\n\tvar \$cacheSources = false;";
|
|
|
|
$extras .= "\n\tvar \$useDbConfig = 'test_suite';\n";
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $extras;
|
|
|
|
}
|
2009-05-15 02:23:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a test for a Model object.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function bakeModelTest($className) {
|
|
|
|
$fixtureInc = 'app';
|
|
|
|
if ($this->plugin) {
|
|
|
|
$fixtureInc = 'plugin.'.Inflector::underscore($this->plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fixture[] = "'{$fixtureInc}." . Inflector::underscore($className) ."'";
|
|
|
|
|
|
|
|
if (!empty($associations)) {
|
|
|
|
$assoc[] = Set::extract($associations, 'belongsTo.{n}.className');
|
|
|
|
$assoc[] = Set::extract($associations, 'hasOne.{n}.className');
|
|
|
|
$assoc[] = Set::extract($associations, 'hasMany.{n}.className');
|
|
|
|
foreach ($assoc as $key => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
foreach ($value as $class) {
|
|
|
|
$fixture[] = "'{$fixtureInc}." . Inflector::underscore($class) ."'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$fixture = join(", ", $fixture);
|
|
|
|
|
|
|
|
$import = $className;
|
|
|
|
if (isset($this->plugin)) {
|
|
|
|
$import = $this->plugin . '.' . $className;
|
|
|
|
}
|
|
|
|
|
|
|
|
$out = "App::import('Model', '$import');\n\n";
|
|
|
|
$out .= "class {$className}TestCase extends CakeTestCase {\n";
|
|
|
|
$out .= "\tvar \${$className} = null;\n";
|
|
|
|
$out .= "\tvar \$fixtures = array($fixture);\n\n";
|
|
|
|
$out .= "\tfunction startTest() {\n";
|
|
|
|
$out .= "\t\t\$this->{$className} =& ClassRegistry::init('{$className}');\n";
|
|
|
|
$out .= "\t}\n\n";
|
|
|
|
$out .= "\tfunction endTest() {\n";
|
|
|
|
$out .= "\t\tunset(\$this->{$className});\n";
|
|
|
|
$out .= "\t}\n\n";
|
|
|
|
$out .= "\tfunction test{$className}Instance() {\n";
|
|
|
|
$out .= "\t\t\$this->assertTrue(is_a(\$this->{$className}, '{$className}'));\n";
|
|
|
|
$out .= "\t}\n\n";
|
|
|
|
$out .= "}\n";
|
|
|
|
|
|
|
|
$path = MODEL_TESTS;
|
|
|
|
if (isset($this->plugin)) {
|
|
|
|
$pluginPath = 'plugins' . DS . Inflector::underscore($this->plugin) . DS;
|
|
|
|
$path = APP . $pluginPath . 'tests' . DS . 'cases' . DS . 'models' . DS;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = Inflector::underscore($className).'.test.php';
|
|
|
|
$this->out("\nBaking unit test for $className...");
|
|
|
|
|
|
|
|
$header = '$Id';
|
|
|
|
$content = "<?php \n/* SVN FILE: $header$ */\n/* ". $className ." Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
|
|
|
|
return $this->createFile($path . $filename, $content);
|
|
|
|
}
|
2009-05-20 04:46:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a test case for a controller.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function bakeControllerTest() {
|
|
|
|
$import = $className;
|
|
|
|
if ($this->plugin) {
|
|
|
|
$import = $this->plugin . '.' . $className;
|
|
|
|
}
|
|
|
|
$out = "App::import('Controller', '$import');\n\n";
|
|
|
|
$out .= "class Test{$className} extends {$className}Controller {\n";
|
|
|
|
$out .= "\tvar \$autoRender = false;\n}\n\n";
|
|
|
|
$out .= "class {$className}ControllerTest extends CakeTestCase {\n";
|
|
|
|
$out .= "\tvar \${$className} = null;\n\n";
|
|
|
|
$out .= "\tfunction startTest() {\n\t\t\$this->{$className} = new Test{$className}();";
|
|
|
|
$out .= "\n\t\t\$this->{$className}->constructClasses();\n\t}\n\n";
|
|
|
|
$out .= "\tfunction test{$className}ControllerInstance() {\n";
|
|
|
|
$out .= "\t\t\$this->assertTrue(is_a(\$this->{$className}, '{$className}Controller'));\n\t}\n\n";
|
|
|
|
$out .= "\tfunction endTest() {\n\t\tunset(\$this->{$className});\n\t}\n}\n";
|
|
|
|
|
|
|
|
$path = CONTROLLER_TESTS;
|
|
|
|
if (isset($this->plugin)) {
|
|
|
|
$pluginPath = 'plugins' . DS . Inflector::underscore($this->plugin) . DS;
|
|
|
|
$path = APP . $pluginPath . 'tests' . DS . 'cases' . DS . 'controllers' . DS;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = Inflector::underscore($className).'_controller.test.php';
|
|
|
|
$this->out("\nBaking unit test for $className...");
|
|
|
|
|
|
|
|
$header = '$Id';
|
|
|
|
$content = "<?php \n/* SVN FILE: $header$ */\n/* ". $className ."Controller Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
|
|
|
|
return $this->createFile($path . $filename, $content);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
?>
|