From 40aa75f791911d6be842e0dbe02bc77dc4cb650f Mon Sep 17 00:00:00 2001 From: "qiang.xue" Date: Fri, 19 Jun 2009 20:41:53 +0000 Subject: [PATCH] Implemented test generation for crud command. --- framework/cli/commands/shell/CrudCommand.php | 20 ++++++++++++++++ framework/cli/commands/shell/ModelCommand.php | 23 ------------------- framework/console/CConsoleCommand.php | 23 +++++++++++++++++++ 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/framework/cli/commands/shell/CrudCommand.php b/framework/cli/commands/shell/CrudCommand.php index ea049a7a5..1af0131ac 100644 --- a/framework/cli/commands/shell/CrudCommand.php +++ b/framework/cli/commands/shell/CrudCommand.php @@ -136,6 +136,8 @@ EOD; $templatePath=$this->templatePath===null?YII_PATH.'/cli/views/shell/crud':$this->templatePath; $viewPath=$module->viewPath.DIRECTORY_SEPARATOR.str_replace('.',DIRECTORY_SEPARATOR,$controllerID); + $fixtureName=$this->pluralize($modelClass); + $fixtureName[0]=strtolower($fixtureName); $list=array( basename($controllerFile)=>array( 'source'=>$templatePath.'/controller.php', @@ -143,6 +145,12 @@ EOD; 'callback'=>array($this,'generateController'), 'params'=>array($controllerClass,$modelClass), ), + $modelClass.'Test.php'=>array( + 'source'=>$templatePath.'/test.php', + 'target'=>Yii::getPathOfAlias('application.tests.functional').DIRECTORY_SEPARATOR.$modelClass.'Test.php', + 'callback'=>array($this,'generateTest'), + 'params'=>array($controllerID,$fixtureName,$modelClass), + ), ); foreach(array('create','update','list','show','admin','_form') as $action) @@ -206,6 +214,18 @@ EOD; 'columns'=>$columns),true); } + public function generateTest($source,$params) + { + list($controllerID,$fixtureName,$modelClass)=$params; + if(!is_file($source)) // fall back to default ones + $source=YII_PATH.'/cli/views/shell/crud/'.basename($source); + return $this->renderFile($source, array( + 'controllerID'=>$controllerID, + 'fixtureName'=>$fixtureName, + 'modelClass'=>$modelClass, + ),true); + } + public function generateInputLabel($modelClass,$column) { return "CHtml::activeLabelEx(\$model,'{$column->name}')"; diff --git a/framework/cli/commands/shell/ModelCommand.php b/framework/cli/commands/shell/ModelCommand.php index 3534f568b..9a8e8813c 100644 --- a/framework/cli/commands/shell/ModelCommand.php +++ b/framework/cli/commands/shell/ModelCommand.php @@ -234,29 +234,6 @@ EOD; return $relationName; } - /** - * Converts a word to its plural form. - * @param string the word to be pluralized - * @return string the pluralized word - */ - public function pluralize($name) - { - $rules=array( - '/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es', - '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', - '/(m)an$/i' => '\1en', - '/(child)$/i' => '\1ren', - '/(r)y$/i' => '\1ies', - '/s$/' => 's', - ); - foreach($rules as $rule=>$replacement) - { - if(preg_match($rule,$name)) - return preg_replace($rule,$replacement,$name); - } - return $name.'s'; - } - /** * Execute the action. * @param array command line parameters specific for this command diff --git a/framework/console/CConsoleCommand.php b/framework/console/CConsoleCommand.php index ec103d682..f00e6d3a6 100644 --- a/framework/console/CConsoleCommand.php +++ b/framework/console/CConsoleCommand.php @@ -221,4 +221,27 @@ abstract class CConsoleCommand extends CComponent else require($_viewFile_); } + + /** + * Converts a word to its plural form. + * @param string the word to be pluralized + * @return string the pluralized word + */ + public function pluralize($name) + { + $rules=array( + '/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es', + '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', + '/(m)an$/i' => '\1en', + '/(child)$/i' => '\1ren', + '/(r)y$/i' => '\1ies', + '/s$/' => 's', + ); + foreach($rules as $rule=>$replacement) + { + if(preg_match($rule,$name)) + return preg_replace($rule,$replacement,$name); + } + return $name.'s'; + } } \ No newline at end of file