00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class TestTask extends Shell {
00034
00035
00036
00037
00038
00039
00040 var $plugin = null;
00041
00042
00043
00044
00045
00046
00047 var $path = TESTS;
00048
00049
00050
00051
00052
00053 function execute() {
00054 if (empty($this->args)) {
00055 $this->__interactive();
00056 }
00057
00058 if (count($this->args) == 1) {
00059 $this->__interactive($this->args[0]);
00060 }
00061
00062 if (count($this->args) > 1) {
00063 $class = Inflector::underscore($this->args[0]);
00064 if ($this->bake($class, $this->args[1])) {
00065 $this->out('done');
00066 }
00067 }
00068 }
00069
00070
00071
00072
00073
00074 function __interactive($class = null) {
00075 $this->hr();
00076 $this->out(sprintf("Bake Tests\nPath: %s", $this->path));
00077 $this->hr();
00078
00079 $key = null;
00080 $options = array('Behavior', 'Helper', 'Component', 'Model', 'Controller');
00081
00082 if ($class !== null) {
00083 $class = Inflector::camelize($class);
00084 if (in_array($class, $options)) {
00085 $key = array_search($class);
00086 }
00087 }
00088
00089 while ($class == null) {
00090 $cases = array();
00091 $this->hr();
00092 $this->out("Select a class:");
00093 $this->hr();
00094
00095 $keys = array();
00096 foreach ($options as $key => $option) {
00097 $this->out(++$key . '. ' . $option);
00098 $keys[] = $key;
00099 }
00100 $keys[] = 'q';
00101
00102 $key = $this->in(__("Enter the class to test or (q)uit", true), $keys, 'q');
00103
00104 if ($key != 'q') {
00105 if (isset($options[--$key])) {
00106 $class = $options[$key];
00107 }
00108
00109 if ($class) {
00110 $name = $this->in(__("Enter the name for the test or (q)uit", true), null, 'q');
00111 if ($name !== 'q') {
00112 $case = null;
00113 while ($case !== 'q') {
00114 $case = $this->in(__("Enter a test case or (q)uit", true), null, 'q');
00115 if ($case !== 'q') {
00116 $cases[] = $case;
00117 }
00118 }
00119 if ($this->bake($class, $name, $cases)) {
00120 $this->out(__("Test baked\n", true));
00121 $type = null;
00122 }
00123 $class = null;
00124 }
00125 }
00126 } else {
00127 $this->_stop();
00128 }
00129 }
00130 }
00131
00132
00133
00134
00135
00136 function bake($class, $name = null, $cases = array()) {
00137 if (!$name) {
00138 return false;
00139 }
00140
00141 if (!is_array($cases)) {
00142 $cases = array($cases);
00143 }
00144
00145 if (strpos($this->path, $class) === false) {
00146 $this->filePath = $this->path . 'cases' . DS . Inflector::tableize($class) . DS;
00147 }
00148
00149 $class = Inflector::classify($class);
00150 $name = Inflector::classify($name);
00151
00152 $import = $name;
00153 if (isset($this->plugin)) {
00154 $import = $this->plugin . '.' . $name;
00155 }
00156 $extras = $this->__extras($class);
00157 $out = "App::import('$class', '$import');\n";
00158 if ($class == 'Model') {
00159 $class = null;
00160 }
00161 $out .= "class Test{$name} extends {$name}{$class} {\n";
00162 $out .= "{$extras}";
00163 $out .= "}\n\n";
00164 $out .= "class {$name}{$class}Test extends CakeTestCase {\n";
00165 $out .= "\n\tfunction startTest() {";
00166 $out .= "\n\t\t\$this->{$name} = new Test{$name}();";
00167 $out .= "\n\t}\n";
00168 $out .= "\n\tfunction test{$name}Instance() {\n";
00169 $out .= "\t\t\$this->assertTrue(is_a(\$this->{$name}, '{$name}{$class}'));\n\t}\n";
00170 foreach ($cases as $case) {
00171 $case = Inflector::classify($case);
00172 $out .= "\n\tfunction test{$case}() {\n\n\t}\n";
00173 }
00174 $out .= "}\n";
00175
00176 $this->out("Baking unit test for $name...");
00177 $this->out($out);
00178 $ok = $this->in(__('Is this correct?', true), array('y', 'n'), 'y');
00179 if ($ok == 'n') {
00180 return false;
00181 }
00182
00183 $header = '$Id';
00184 $content = "<?php \n/* SVN FILE: $header$ */\n/* " . $name . " Test cases generated on: " . date('Y-m-d H:i:s') . " : ". time() . "*/\n{$out}?>";
00185 return $this->createFile($this->filePath . Inflector::underscore($name) . '.test.php', $content);
00186 }
00187
00188
00189
00190
00191
00192 function __extras($class) {
00193 $extras = null;
00194 switch ($class) {
00195 case 'Model':
00196 $extras = "\n\tvar \$cacheSources = false;";
00197 $extras .= "\n\tvar \$useDbConfig = 'test_suite';\n";
00198 break;
00199 }
00200 return $extras;
00201 }
00202 }
00203 ?>