1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18:
19: App::uses('AppShell', 'Console/Command');
20: App::uses('BakeTask', 'Console/Command/Task');
21: App::uses('ClassRegistry', 'Utility');
22:
23: 24: 25: 26: 27:
28: class TestTask extends BakeTask {
29:
30: 31: 32: 33: 34:
35: public $path = TESTS;
36:
37: 38: 39: 40: 41:
42: public $tasks = array('Template');
43:
44: 45: 46: 47: 48:
49: public $classTypes = array(
50: 'Model' => 'Model',
51: 'Controller' => 'Controller',
52: 'Component' => 'Controller/Component',
53: 'Behavior' => 'Model/Behavior',
54: 'Helper' => 'View/Helper'
55: );
56:
57: 58: 59: 60: 61:
62: protected $_fixtures = array();
63:
64: 65: 66: 67: 68:
69: public function execute() {
70: parent::execute();
71: if (empty($this->args)) {
72: $this->_interactive();
73: }
74:
75: if (count($this->args) == 1) {
76: $this->_interactive($this->args[0]);
77: }
78:
79: if (count($this->args) > 1) {
80: $type = Inflector::underscore($this->args[0]);
81: if ($this->bake($type, $this->args[1])) {
82: $this->out('<success>Done</success>');
83: }
84: }
85: }
86:
87: 88: 89: 90: 91: 92:
93: protected function _interactive($type = null) {
94: $this->interactive = true;
95: $this->hr();
96: $this->out(__d('cake_console', 'Bake Tests'));
97: $this->out(__d('cake_console', 'Path: %s', $this->getPath()));
98: $this->hr();
99:
100: if ($type) {
101: $type = Inflector::camelize($type);
102: if (!isset($this->classTypes[$type])) {
103: $this->error(__d('cake_console', 'Incorrect type provided. Please choose one of %s', implode(', ', array_keys($this->classTypes))));
104: }
105: } else {
106: $type = $this->getObjectType();
107: }
108: $className = $this->getClassName($type);
109: return $this->bake($type, $className);
110: }
111:
112: 113: 114: 115: 116: 117: 118:
119: public function bake($type, $className) {
120: $plugin = null;
121: if ($this->plugin) {
122: $plugin = $this->plugin . '.';
123: }
124:
125: $realType = $this->mapType($type, $plugin);
126: $fullClassName = $this->getRealClassName($type, $className);
127:
128: if ($this->typeCanDetectFixtures($type) && $this->isLoadableClass($realType, $fullClassName)) {
129: $this->out(__d('cake_console', 'Bake is detecting possible fixtures...'));
130: $testSubject = $this->buildTestSubject($type, $className);
131: $this->generateFixtureList($testSubject);
132: } elseif ($this->interactive) {
133: $this->getUserFixtures();
134: }
135: App::uses($fullClassName, $realType);
136:
137: $methods = array();
138: if (class_exists($fullClassName)) {
139: $methods = $this->getTestableMethods($fullClassName);
140: }
141: $mock = $this->hasMockClass($type, $fullClassName);
142: $construction = $this->generateConstructor($type, $fullClassName);
143:
144: $this->out("\n" . __d('cake_console', 'Baking test case for %s %s ...', $className, $type), 1, Shell::QUIET);
145:
146: $this->Template->set('fixtures', $this->_fixtures);
147: $this->Template->set('plugin', $plugin);
148: $this->Template->set(compact(
149: 'className', 'methods', 'type', 'fullClassName', 'mock',
150: 'construction', 'realType'
151: ));
152: $out = $this->Template->generate('classes', 'test');
153:
154: $filename = $this->testCaseFileName($type, $className);
155: $made = $this->createFile($filename, $out);
156: if ($made) {
157: return $out;
158: }
159: return false;
160: }
161:
162: 163: 164: 165: 166:
167: public function getObjectType() {
168: $this->hr();
169: $this->out(__d('cake_console', 'Select an object type:'));
170: $this->hr();
171:
172: $keys = array();
173: $i = 0;
174: foreach ($this->classTypes as $option => $package) {
175: $this->out(++$i . '. ' . $option);
176: $keys[] = $i;
177: }
178: $keys[] = 'q';
179: $selection = $this->in(__d('cake_console', 'Enter the type of object to bake a test for or (q)uit'), $keys, 'q');
180: if ($selection == 'q') {
181: return $this->_stop();
182: }
183: $types = array_keys($this->classTypes);
184: return $types[$selection - 1];
185: }
186:
187: 188: 189: 190: 191: 192:
193: public function getClassName($objectType) {
194: $type = ucfirst(strtolower($objectType));
195: $typeLength = strlen($type);
196: $type = $this->classTypes[$type];
197: if ($this->plugin) {
198: $plugin = $this->plugin . '.';
199: $options = App::objects($plugin . $type);
200: } else {
201: $options = App::objects($type);
202: }
203: $this->out(__d('cake_console', 'Choose a %s class', $objectType));
204: $keys = array();
205: foreach ($options as $key => $option) {
206: $this->out(++$key . '. ' . $option);
207: $keys[] = $key;
208: }
209: while (empty($selection)) {
210: $selection = $this->in(__d('cake_console', 'Choose an existing class, or enter the name of a class that does not exist'));
211: if (is_numeric($selection) && isset($options[$selection - 1])) {
212: $selection = $options[$selection - 1];
213: }
214: if ($type !== 'Model') {
215: $selection = substr($selection, 0, $typeLength * - 1);
216: }
217: }
218: return $selection;
219: }
220:
221: 222: 223: 224: 225: 226: 227:
228: public function typeCanDetectFixtures($type) {
229: $type = strtolower($type);
230: return in_array($type, array('controller', 'model'));
231: }
232:
233: 234: 235: 236: 237: 238: 239:
240: public function isLoadableClass($package, $class) {
241: App::uses($class, $package);
242: return class_exists($class);
243: }
244:
245: 246: 247: 248: 249: 250: 251: 252:
253: public function &buildTestSubject($type, $class) {
254: ClassRegistry::flush();
255: App::import($type, $class);
256: $class = $this->getRealClassName($type, $class);
257: if (strtolower($type) == 'model') {
258: $instance = ClassRegistry::init($class);
259: } else {
260: $instance = new $class();
261: }
262: return $instance;
263: }
264:
265: 266: 267: 268: 269: 270: 271: 272:
273: public function getRealClassName($type, $class) {
274: if (strtolower($type) == 'model' || empty($this->classTypes[$type])) {
275: return $class;
276: }
277: if (strlen($class) - strpos($class, $type) == strlen($type)) {
278: return $class;
279: }
280: return $class . $type;
281: }
282:
283: 284: 285: 286: 287: 288: 289:
290: public function mapType($type, $plugin) {
291: $type = ucfirst($type);
292: if (empty($this->classTypes[$type])) {
293: throw new CakeException(__d('cake_dev', 'Invalid object type.'));
294: }
295: $real = $this->classTypes[$type];
296: if ($plugin) {
297: $real = trim($plugin, '.') . '.' . $real;
298: }
299: return $real;
300: }
301:
302: 303: 304: 305: 306: 307: 308:
309: public function getTestableMethods($className) {
310: $classMethods = get_class_methods($className);
311: $parentMethods = get_class_methods(get_parent_class($className));
312: $thisMethods = array_diff($classMethods, $parentMethods);
313: $out = array();
314: foreach ($thisMethods as $method) {
315: if (substr($method, 0, 1) != '_' && $method != strtolower($className)) {
316: $out[] = $method;
317: }
318: }
319: return $out;
320: }
321:
322: 323: 324: 325: 326: 327: 328:
329: public function generateFixtureList($subject) {
330: $this->_fixtures = array();
331: if (is_a($subject, 'Model')) {
332: $this->_processModel($subject);
333: } elseif (is_a($subject, 'Controller')) {
334: $this->_processController($subject);
335: }
336: return array_values($this->_fixtures);
337: }
338:
339: 340: 341: 342: 343: 344: 345:
346: protected function _processModel($subject) {
347: $this->_addFixture($subject->name);
348: $associated = $subject->getAssociated();
349: foreach ($associated as $alias => $type) {
350: $className = $subject->{$alias}->name;
351: if (!isset($this->_fixtures[$className])) {
352: $this->_processModel($subject->{$alias});
353: }
354: if ($type == 'hasAndBelongsToMany') {
355: $joinModel = Inflector::classify($subject->hasAndBelongsToMany[$alias]['joinTable']);
356: if (!isset($this->_fixtures[$joinModel])) {
357: $this->_processModel($subject->{$joinModel});
358: }
359: }
360: }
361: }
362:
363: 364: 365: 366: 367: 368: 369:
370: protected function _processController($subject) {
371: $subject->constructClasses();
372: $models = array(Inflector::classify($subject->name));
373: if (!empty($subject->uses)) {
374: $models = $subject->uses;
375: }
376: foreach ($models as $model) {
377: $this->_processModel($subject->{$model});
378: }
379: }
380:
381: 382: 383: 384: 385: 386: 387:
388: protected function _addFixture($name) {
389: $parent = get_parent_class($name);
390: $prefix = 'app.';
391: if (strtolower($parent) != 'appmodel' && strtolower(substr($parent, -8)) == 'appmodel') {
392: $pluginName = substr($parent, 0, strlen($parent) -8);
393: $prefix = 'plugin.' . Inflector::underscore($pluginName) . '.';
394: }
395: $fixture = $prefix . Inflector::underscore($name);
396: $this->_fixtures[$name] = $fixture;
397: }
398:
399: 400: 401: 402: 403:
404: public function getUserFixtures() {
405: $proceed = $this->in(__d('cake_console', 'Bake could not detect fixtures, would you like to add some?'), array('y', 'n'), 'n');
406: $fixtures = array();
407: if (strtolower($proceed) == 'y') {
408: $fixtureList = $this->in(__d('cake_console', "Please provide a comma separated list of the fixtures names you'd like to use.\nExample: 'app.comment, app.post, plugin.forums.post'"));
409: $fixtureListTrimmed = str_replace(' ', '', $fixtureList);
410: $fixtures = explode(',', $fixtureListTrimmed);
411: }
412: $this->_fixtures = array_merge($this->_fixtures, $fixtures);
413: return $fixtures;
414: }
415:
416: 417: 418: 419: 420: 421: 422:
423: public function hasMockClass($type) {
424: $type = strtolower($type);
425: return $type == 'controller';
426: }
427:
428: 429: 430: 431: 432: 433: 434:
435: public function generateConstructor($type, $fullClassName) {
436: $type = strtolower($type);
437: if ($type == 'model') {
438: return "ClassRegistry::init('$fullClassName');\n";
439: }
440: if ($type == 'controller') {
441: $className = substr($fullClassName, 0, strlen($fullClassName) - 10);
442: return "new Test$fullClassName();\n\t\t\$this->{$className}->constructClasses();\n";
443: }
444: return "new $fullClassName();\n";
445: }
446:
447: 448: 449: 450: 451: 452: 453: 454:
455: public function testCaseFileName($type, $className) {
456: $path = $this->getPath() . 'Case' . DS;
457: $type = Inflector::camelize($type);
458: if (isset($this->classTypes[$type])) {
459: $path .= $this->classTypes[$type] . DS;
460: }
461: $className = $this->getRealClassName($type, $className);
462: return str_replace('/', DS, $path) . Inflector::camelize($className) . 'Test.php';
463: }
464:
465: 466: 467: 468: 469:
470: public function getOptionParser() {
471: $parser = parent::getOptionParser();
472: return $parser->description(__d('cake_console', 'Bake test case skeletons for classes.'))
473: ->addArgument('type', array(
474: 'help' => __d('cake_console', 'Type of class to bake, can be any of the following: controller, model, helper, component or behavior.'),
475: 'choices' => array(
476: 'Controller', 'controller',
477: 'Model', 'model',
478: 'Helper', 'helper',
479: 'Component', 'component',
480: 'Behavior', 'behavior'
481: )
482: ))->addArgument('name', array(
483: 'help' => __d('cake_console', 'An existing class to bake tests for.')
484: ))->addOption('plugin', array(
485: 'short' => 'p',
486: 'help' => __d('cake_console', 'CamelCased name of the plugin to bake tests for.')
487: ))->epilog(__d('cake_console', 'Omitting all arguments and options will enter into an interactive mode.'));
488: }
489: }
490: