1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: 27: 28: 29: 30: 31:
32: class TestTask extends Shell {
33: 34: 35: 36: 37: 38:
39: var $plugin = null;
40: 41: 42: 43: 44: 45:
46: var $path = TESTS;
47: 48: 49: 50: 51:
52: function execute() {
53: if (empty($this->args)) {
54: $this->__interactive();
55: }
56:
57: if (count($this->args) == 1) {
58: $this->__interactive($this->args[0]);
59: }
60:
61: if (count($this->args) > 1) {
62: $class = Inflector::underscore($this->args[0]);
63: if ($this->bake($class, $this->args[1])) {
64: $this->out('done');
65: }
66: }
67: }
68: 69: 70: 71: 72:
73: function __interactive($class = null) {
74: $this->hr();
75: $this->out(sprintf("Bake Tests\nPath: %s", $this->path));
76: $this->hr();
77:
78: $key = null;
79: $options = array('Behavior', 'Helper', 'Component', 'Model', 'Controller');
80:
81: if ($class !== null) {
82: $class = Inflector::camelize($class);
83: if (in_array($class, $options)) {
84: $key = array_search($class);
85: }
86: }
87:
88: while ($class == null) {
89: $cases = array();
90: $this->hr();
91: $this->out("Select a class:");
92: $this->hr();
93:
94: $keys = array();
95: foreach ($options as $key => $option) {
96: $this->out(++$key . '. ' . $option);
97: $keys[] = $key;
98: }
99: $keys[] = 'q';
100:
101: $key = $this->in(__("Enter the class to test or (q)uit", true), $keys, 'q');
102:
103: if ($key != 'q') {
104: if (isset($options[--$key])) {
105: $class = $options[$key];
106: }
107:
108: if ($class) {
109: $name = $this->in(__("Enter the name for the test or (q)uit", true), null, 'q');
110: if ($name !== 'q') {
111: $case = null;
112: while ($case !== 'q') {
113: $case = $this->in(__("Enter a test case or (q)uit", true), null, 'q');
114: if ($case !== 'q') {
115: $cases[] = $case;
116: }
117: }
118: if ($this->bake($class, $name, $cases)) {
119: $this->out(__("Test baked\n", true));
120: $type = null;
121: }
122: $class = null;
123: }
124: }
125: } else {
126: $this->_stop();
127: }
128: }
129: }
130: 131: 132: 133: 134:
135: function bake($class, $name = null, $cases = array()) {
136: if (!$name) {
137: return false;
138: }
139:
140: if (!is_array($cases)) {
141: $cases = array($cases);
142: }
143:
144: if (strpos($this->path, $class) === false) {
145: $this->filePath = $this->path . 'cases' . DS . Inflector::tableize($class) . DS;
146: }
147:
148: $class = Inflector::classify($class);
149: $name = Inflector::classify($name);
150:
151: $import = $name;
152: if (isset($this->plugin)) {
153: $import = $this->plugin . '.' . $name;
154: }
155: $extras = $this->__extras($class);
156: $out = "App::import('$class', '$import');\n";
157: if ($class == 'Model') {
158: $class = null;
159: }
160: $out .= "class Test{$name} extends {$name}{$class} {\n";
161: $out .= "{$extras}";
162: $out .= "}\n\n";
163: $out .= "class {$name}{$class}Test extends CakeTestCase {\n";
164: $out .= "\n\tfunction startTest() {";
165: $out .= "\n\t\t\$this->{$name} = new Test{$name}();";
166: $out .= "\n\t}\n";
167: $out .= "\n\tfunction test{$name}Instance() {\n";
168: $out .= "\t\t\$this->assertTrue(is_a(\$this->{$name}, '{$name}{$class}'));\n\t}\n";
169: foreach ($cases as $case) {
170: $case = Inflector::classify($case);
171: $out .= "\n\tfunction test{$case}() {\n\n\t}\n";
172: }
173: $out .= "}\n";
174:
175: $this->out("Baking unit test for $name...");
176: $this->out($out);
177: $ok = $this->in(__('Is this correct?', true), array('y', 'n'), 'y');
178: if ($ok == 'n') {
179: return false;
180: }
181:
182: $header = '$Id';
183: $content = "<?php \n/* SVN FILE: $header$ */\n/* " . $name . " Test cases generated on: " . date('Y-m-d H:i:s') . " : ". time() . "*/\n{$out}?>";
184: return $this->createFile($this->filePath . Inflector::underscore($name) . '.test.php', $content);
185: }
186: 187: 188: 189: 190:
191: function __extras($class) {
192: $extras = null;
193: switch ($class) {
194: case 'Model':
195: $extras = "\n\tvar \$cacheSources = false;";
196: $extras .= "\n\tvar \$useDbConfig = 'test_suite';\n";
197: break;
198: }
199: return $extras;
200: }
201: }
202: ?>