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: list($preConstruct, $construction, $postConstruct) = $this->generateConstructor($type, $fullClassName);
143: $uses = $this->generateUses($type, $realType, $fullClassName);
144:
145: $this->out("\n" . __d('cake_console', 'Baking test case for %s %s ...', $className, $type), 1, Shell::QUIET);
146:
147: $this->Template->set('fixtures', $this->_fixtures);
148: $this->Template->set('plugin', $plugin);
149: $this->Template->set(compact(
150: 'className', 'methods', 'type', 'fullClassName', 'mock',
151: 'realType', 'preConstruct', 'postConstruct', 'construction',
152: 'uses'
153: ));
154: $out = $this->Template->generate('classes', 'test');
155:
156: $filename = $this->testCaseFileName($type, $className);
157: $made = $this->createFile($filename, $out);
158: if ($made) {
159: return $out;
160: }
161: return false;
162: }
163:
164: 165: 166: 167: 168:
169: public function getObjectType() {
170: $this->hr();
171: $this->out(__d('cake_console', 'Select an object type:'));
172: $this->hr();
173:
174: $keys = array();
175: $i = 0;
176: foreach ($this->classTypes as $option => $package) {
177: $this->out(++$i . '. ' . $option);
178: $keys[] = $i;
179: }
180: $keys[] = 'q';
181: $selection = $this->in(__d('cake_console', 'Enter the type of object to bake a test for or (q)uit'), $keys, 'q');
182: if ($selection == 'q') {
183: return $this->_stop();
184: }
185: $types = array_keys($this->classTypes);
186: return $types[$selection - 1];
187: }
188:
189: 190: 191: 192: 193: 194:
195: public function getClassName($objectType) {
196: $type = ucfirst(strtolower($objectType));
197: $typeLength = strlen($type);
198: $type = $this->classTypes[$type];
199: if ($this->plugin) {
200: $plugin = $this->plugin . '.';
201: $options = App::objects($plugin . $type);
202: } else {
203: $options = App::objects($type);
204: }
205: $this->out(__d('cake_console', 'Choose a %s class', $objectType));
206: $keys = array();
207: foreach ($options as $key => $option) {
208: $this->out(++$key . '. ' . $option);
209: $keys[] = $key;
210: }
211: while (empty($selection)) {
212: $selection = $this->in(__d('cake_console', 'Choose an existing class, or enter the name of a class that does not exist'));
213: if (is_numeric($selection) && isset($options[$selection - 1])) {
214: $selection = $options[$selection - 1];
215: }
216: if ($type !== 'Model') {
217: $selection = substr($selection, 0, $typeLength * - 1);
218: }
219: }
220: return $selection;
221: }
222:
223: 224: 225: 226: 227: 228: 229:
230: public function typeCanDetectFixtures($type) {
231: $type = strtolower($type);
232: return in_array($type, array('controller', 'model'));
233: }
234:
235: 236: 237: 238: 239: 240: 241:
242: public function isLoadableClass($package, $class) {
243: App::uses($class, $package);
244: return class_exists($class);
245: }
246:
247: 248: 249: 250: 251: 252: 253: 254:
255: public function &buildTestSubject($type, $class) {
256: ClassRegistry::flush();
257: App::import($type, $class);
258: $class = $this->getRealClassName($type, $class);
259: if (strtolower($type) == 'model') {
260: $instance = ClassRegistry::init($class);
261: } else {
262: $instance = new $class();
263: }
264: return $instance;
265: }
266:
267: 268: 269: 270: 271: 272: 273: 274:
275: public function getRealClassName($type, $class) {
276: if (strtolower($type) == 'model' || empty($this->classTypes[$type])) {
277: return $class;
278: }
279:
280: $position = strpos($class, $type);
281:
282: if ($position !== false && strlen($class) - $position == strlen($type)) {
283: return $class;
284: }
285: return $class . $type;
286: }
287:
288: 289: 290: 291: 292: 293: 294: 295:
296: public function mapType($type, $plugin) {
297: $type = ucfirst($type);
298: if (empty($this->classTypes[$type])) {
299: throw new CakeException(__d('cake_dev', 'Invalid object type.'));
300: }
301: $real = $this->classTypes[$type];
302: if ($plugin) {
303: $real = trim($plugin, '.') . '.' . $real;
304: }
305: return $real;
306: }
307:
308: 309: 310: 311: 312: 313: 314:
315: public function getTestableMethods($className) {
316: $classMethods = get_class_methods($className);
317: $parentMethods = get_class_methods(get_parent_class($className));
318: $thisMethods = array_diff($classMethods, $parentMethods);
319: $out = array();
320: foreach ($thisMethods as $method) {
321: if (substr($method, 0, 1) != '_' && $method != strtolower($className)) {
322: $out[] = $method;
323: }
324: }
325: return $out;
326: }
327:
328: 329: 330: 331: 332: 333: 334:
335: public function generateFixtureList($subject) {
336: $this->_fixtures = array();
337: if (is_a($subject, 'Model')) {
338: $this->_processModel($subject);
339: } elseif (is_a($subject, 'Controller')) {
340: $this->_processController($subject);
341: }
342: return array_values($this->_fixtures);
343: }
344:
345: 346: 347: 348: 349: 350: 351:
352: protected function _processModel($subject) {
353: $this->_addFixture($subject->name);
354: $associated = $subject->getAssociated();
355: foreach ($associated as $alias => $type) {
356: $className = $subject->{$alias}->name;
357: if (!isset($this->_fixtures[$className])) {
358: $this->_processModel($subject->{$alias});
359: }
360: if ($type == 'hasAndBelongsToMany') {
361: if (!empty($subject->hasAndBelongsToMany[$alias]['with'])) {
362: list($plugin, $joinModel) = pluginSplit($subject->hasAndBelongsToMany[$alias]['with']);
363: } else {
364: $joinModel = Inflector::classify($subject->hasAndBelongsToMany[$alias]['joinTable']);
365: }
366: if (!isset($this->_fixtures[$joinModel])) {
367: $this->_processModel($subject->{$joinModel});
368: }
369: }
370: }
371: }
372:
373: 374: 375: 376: 377: 378: 379:
380: protected function _processController($subject) {
381: $subject->constructClasses();
382: $models = array(Inflector::classify($subject->name));
383: if (!empty($subject->uses)) {
384: $models = $subject->uses;
385: }
386: foreach ($models as $model) {
387: $this->_processModel($subject->{$model});
388: }
389: }
390:
391: 392: 393: 394: 395: 396: 397:
398: protected function _addFixture($name) {
399: $parent = get_parent_class($name);
400: $prefix = 'app.';
401: if (strtolower($parent) != 'appmodel' && strtolower(substr($parent, -8)) == 'appmodel') {
402: $pluginName = substr($parent, 0, -8);
403: $prefix = 'plugin.' . Inflector::underscore($pluginName) . '.';
404: }
405: $fixture = $prefix . Inflector::underscore($name);
406: $this->_fixtures[$name] = $fixture;
407: }
408:
409: 410: 411: 412: 413:
414: public function getUserFixtures() {
415: $proceed = $this->in(__d('cake_console', 'Bake could not detect fixtures, would you like to add some?'), array('y', 'n'), 'n');
416: $fixtures = array();
417: if (strtolower($proceed) == 'y') {
418: $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'"));
419: $fixtureListTrimmed = str_replace(' ', '', $fixtureList);
420: $fixtures = explode(',', $fixtureListTrimmed);
421: }
422: $this->_fixtures = array_merge($this->_fixtures, $fixtures);
423: return $fixtures;
424: }
425:
426: 427: 428: 429: 430: 431: 432:
433: public function hasMockClass($type) {
434: $type = strtolower($type);
435: return $type == 'controller';
436: }
437:
438: 439: 440: 441: 442: 443: 444:
445: public function generateConstructor($type, $fullClassName) {
446: $type = strtolower($type);
447: $pre = $post = '';
448: if ($type == 'model') {
449: $construct = "ClassRegistry::init('$fullClassName');\n";
450: }
451: if ($type == 'behavior') {
452: $construct = "new $fullClassName();\n";
453: }
454: if ($type == 'controller') {
455: $className = substr($fullClassName, 0, -10);
456: $construct = "new Test$fullClassName();\n";
457: $post = "\$this->{$className}->constructClasses();\n";
458: }
459: if ($type == 'helper') {
460: $pre = "\$View = new View();\n";
461: $construct = "new {$fullClassName}(\$View);\n";
462: }
463: if ($type == 'component') {
464: $pre = "\$Collection = new ComponentCollection();\n";
465: $construct = "new {$fullClassName}(\$Collection);\n";
466: }
467: return array($pre, $construct, $post);
468: }
469:
470: 471: 472: 473: 474: 475: 476: 477:
478: public function generateUses($type, $realType, $className) {
479: $uses = array();
480: if ($type == 'component') {
481: $uses[] = array('ComponentCollection', 'Controller');
482: $uses[] = array('Component', 'Controller');
483: }
484: if ($type == 'helper') {
485: $uses[] = array('View', 'View');
486: $uses[] = array('Helper', 'View');
487: }
488: $uses[] = array($className, $realType);
489: return $uses;
490: }
491:
492: 493: 494: 495: 496: 497: 498: 499:
500: public function testCaseFileName($type, $className) {
501: $path = $this->getPath() . 'Case' . DS;
502: $type = Inflector::camelize($type);
503: if (isset($this->classTypes[$type])) {
504: $path .= $this->classTypes[$type] . DS;
505: }
506: $className = $this->getRealClassName($type, $className);
507: return str_replace('/', DS, $path) . Inflector::camelize($className) . 'Test.php';
508: }
509:
510: 511: 512: 513: 514:
515: public function getOptionParser() {
516: $parser = parent::getOptionParser();
517: return $parser->description(__d('cake_console', 'Bake test case skeletons for classes.'))
518: ->addArgument('type', array(
519: 'help' => __d('cake_console', 'Type of class to bake, can be any of the following: controller, model, helper, component or behavior.'),
520: 'choices' => array(
521: 'Controller', 'controller',
522: 'Model', 'model',
523: 'Helper', 'helper',
524: 'Component', 'component',
525: 'Behavior', 'behavior'
526: )
527: ))->addArgument('name', array(
528: 'help' => __d('cake_console', 'An existing class to bake tests for.')
529: ))->addOption('plugin', array(
530: 'short' => 'p',
531: 'help' => __d('cake_console', 'CamelCased name of the plugin to bake tests for.')
532: ))->epilog(__d('cake_console', 'Omitting all arguments and options will enter into an interactive mode.'));
533: }
534:
535: }
536: