test.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: test.php 8260 2009-07-28 20:01:42Z DarkAngelBGE $ */
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. (http://www.cakefoundation.org)
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00018  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package       cake
00020  * @subpackage    cake.cake.console.libs.tasks
00021  * @since         CakePHP(tm) v 1.2
00022  * @version       $Revision: 8260 $
00023  * @modifiedby    $LastChangedBy: DarkAngelBGE $
00024  * @lastmodified  $Date: 2009-07-28 16:01:42 -0400 (Tue, 28 Jul 2009) $
00025  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 /**
00028  * Task class for creating and updating test files.
00029  *
00030  * @package       cake
00031  * @subpackage    cake.cake.console.libs.tasks
00032  */
00033 class TestTask extends Shell {
00034 /**
00035  * Name of plugin
00036  *
00037  * @var string
00038  * @access public
00039  */
00040     var $plugin = null;
00041 /**
00042  * path to TESTS directory
00043  *
00044  * @var string
00045  * @access public
00046  */
00047     var $path = TESTS;
00048 /**
00049  * Execution method always used for tasks
00050  *
00051  * @access public
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  * Handles interactive baking
00071  *
00072  * @access private
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  * Writes File
00133  *
00134  * @access public
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  * Handles the extra stuff needed
00189  *
00190  * @access private
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 ?>

Generated on Sun Nov 22 00:30:52 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7