test.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: cake_2console_2libs_2tasks_2test_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * The TestTask handles creating and updating test files.
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.console.libs.tasks
00023  * @since           CakePHP(tm) v 1.2
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * Task class for creating and updating test files.
00031  *
00032  * @package     cake
00033  * @subpackage  cake.cake.console.libs.tasks
00034  */
00035 class TestTask extends Shell {
00036 /**
00037  * Name of plugin
00038  *
00039  * @var string
00040  * @access public
00041  */     
00042     var $plugin = null;
00043 /**
00044  * path to TESTS directory
00045  *
00046  * @var string
00047  * @access public
00048  */
00049     var $path = TESTS;
00050 /**
00051  * Execution method always used for tasks
00052  *
00053  * @access public
00054  */
00055     function execute() {
00056         if (empty($this->args)) {
00057             $this->__interactive();
00058         }
00059         
00060         if (count($this->args) == 1) {
00061             $this->__interactive($this->args[0]);
00062         }
00063 
00064         if (count($this->args) > 1) {
00065             $class = Inflector::underscore($this->args[0]);
00066             if ($this->bake($class, $this->args[1])) {
00067                 $this->out('done');
00068             }
00069         }
00070     }
00071 /**
00072  * Handles interactive baking
00073  *
00074  * @access private
00075  */
00076     function __interactive($class = null) {
00077         $this->hr();
00078         $this->out(sprintf("Bake Tests\nPath: %s", $this->path));
00079         $this->hr();
00080         
00081         $key = null;    
00082         $options = array('Behavior', 'Helper', 'Component', 'Model', 'Controller');
00083         
00084         if ($class !== null) {
00085             $class = Inflector::camelize($class);
00086             if (in_array($class, $options)) {
00087                 $key = array_search($class);
00088             }
00089         }
00090         
00091         while ($class == null) {
00092             
00093                 $this->hr();
00094                 $this->out("Select a class:");
00095                 $this->hr();
00096                 
00097                 $keys = array();
00098                 foreach ($options as $key => $option) {
00099                     $this->out(++$key . '. ' . $option);
00100                     $keys[] = $key;
00101                 }
00102                 $keys[] = 'q';
00103                 
00104                 $key = $this->in(__("Enter the class to test or (q)uit", true), $keys, 'q');
00105                 
00106             if ($key != 'q') {
00107                 if (isset($options[--$key])) {
00108                     $class = $options[$key];
00109                 }
00110                 
00111                 if ($class) {
00112                     $this->path .= 'cases' . DS . Inflector::tableize($class) . DS;
00113             
00114                     $name = $this->in(__("Enter the name for the test or (q)uit", true), null, 'q');
00115                     if ($name !== 'q') {
00116                         $case = null;
00117                         while ($case !== 'q') {
00118                             $case = $this->in(__("Enter a test case or (q)uit", true), null, 'q');
00119                             if ($case !== 'q') {
00120                                 $cases[] = $case;
00121                             }
00122                         }
00123                         if ($this->bake($class, $name, $cases)) {
00124                             $this->out(__("Test baked\n", true));
00125                             $type = null;
00126                         }
00127                         $class = null;
00128                     }
00129                 }
00130             } else {
00131                 $this->_stop();
00132             }
00133         }
00134     }
00135 /**
00136  * Writes File
00137  *
00138  * @access public
00139  */ 
00140     function bake($class, $name = null, $cases = array()) {
00141         if (!$name) {
00142             return false;
00143         }
00144         
00145         if (!is_array($cases)) {
00146             $cases = array($cases);
00147         }
00148                 
00149         $name = Inflector::camelize($name);
00150         $import = $name;
00151         if (isset($this->plugin)) {
00152             $import = $this->plugin . '.' . $name;
00153         }
00154         $extras = $this->__extras($class);
00155         $out = "App::import('$class', '$import');\n";
00156         if ($class == 'Model') {
00157             $class = null;
00158         }
00159         $out .= "class Test{$name} extends {$name}{$class} {\n";
00160         $out .= "{$extras}";
00161         $out .= "}\n\n";
00162         $out .= "class {$name}{$class}Test extends CakeTestCase {\n";
00163         $out .= "\n\tfunction start() {\n\t\tparent::start();\n\t\t\$this->{$name} = new Test{$name}();\n\t}\n";
00164         $out .= "\n\tfunction test{$name}Instance() {\n";
00165         $out .= "\t\t\$this->assertTrue(is_a(\$this->{$name}, '{$name}{$class}'));\n\t}\n";
00166         foreach ($cases as $case) {
00167             $case = Inflector::classify($case);
00168             $out .= "\n\tfunction test{$case}() {\n\n\t}\n";
00169         }
00170         $out .= "}\n";
00171         
00172         $this->out("Baking unit test for $name...");
00173         $this->out($out);
00174         $ok = $this->in(__('Is this correct?'), array('y', 'n'), 'y');
00175         if ($ok == 'n') {
00176             return false;
00177         }
00178 
00179         $header = '$Id';
00180         $content = "<?php \n/* SVN FILE: $header$ */\n/* ". $name ." Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
00181         return $this->createFile($this->path . Inflector::underscore($name) . '.test.php', $content);
00182     }
00183 /**
00184  * Handles the extra stuff needed 
00185  *
00186  * @access private
00187  */ 
00188     function __extras($class) {
00189         $extras = null;
00190         switch ($class) {
00191             case 'Model':
00192                 $extras = "\n\tvar \$cacheSources = false;\n";
00193             break;
00194         }
00195         return $extras;
00196     }
00197 }
00198 ?>