Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: test.php 7945 2008-12-19 02:16:01Z gwoo $ */
3
/**
4
 * The TestTask handles creating and updating test files.
5
 *
6
 * Long description for file
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
11
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
12
 *
13
 * Licensed under The MIT License
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @filesource
17
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
18
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
19
 * @package       cake
20
 * @subpackage    cake.cake.console.libs.tasks
21
 * @since         CakePHP(tm) v 1.2
22
 * @version       $Revision: 7945 $
23
 * @modifiedby    $LastChangedBy: gwoo $
24
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
25
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
26
 */
27
/**
28
 * Task class for creating and updating test files.
29
 *
30
 * @package       cake
31
 * @subpackage    cake.cake.console.libs.tasks
32
 */
33
class TestTask extends Shell {
34
/**
35
 * Name of plugin
36
 *
37
 * @var string
38
 * @access public
39
 */
40
	var $plugin = null;
41
/**
42
 * path to TESTS directory
43
 *
44
 * @var string
45
 * @access public
46
 */
47
	var $path = TESTS;
48
/**
49
 * Execution method always used for tasks
50
 *
51
 * @access public
52
 */
53
	function execute() {
54
		if (empty($this->args)) {
55
			$this->__interactive();
56
		}
57
 
58
		if (count($this->args) == 1) {
59
			$this->__interactive($this->args[0]);
60
		}
61
 
62
		if (count($this->args) > 1) {
63
			$class = Inflector::underscore($this->args[0]);
64
			if ($this->bake($class, $this->args[1])) {
65
				$this->out('done');
66
			}
67
		}
68
	}
69
/**
70
 * Handles interactive baking
71
 *
72
 * @access private
73
 */
74
	function __interactive($class = null) {
75
		$this->hr();
76
		$this->out(sprintf("Bake Tests\nPath: %s", $this->path));
77
		$this->hr();
78
 
79
		$key = null;
80
		$options = array('Behavior', 'Helper', 'Component', 'Model', 'Controller');
81
 
82
		if ($class !== null) {
83
			$class = Inflector::camelize($class);
84
			if (in_array($class, $options)) {
85
				$key = array_search($class);
86
			}
87
		}
88
 
89
		while ($class == null) {
90
 
91
				$this->hr();
92
				$this->out("Select a class:");
93
				$this->hr();
94
 
95
				$keys = array();
96
				foreach ($options as $key => $option) {
97
					$this->out(++$key . '. ' . $option);
98
					$keys[] = $key;
99
				}
100
				$keys[] = 'q';
101
 
102
				$key = $this->in(__("Enter the class to test or (q)uit", true), $keys, 'q');
103
 
104
			if ($key != 'q') {
105
				if (isset($options[--$key])) {
106
					$class = $options[$key];
107
				}
108
 
109
				if ($class) {
110
					$name = $this->in(__("Enter the name for the test or (q)uit", true), null, 'q');
111
					if ($name !== 'q') {
112
						$case = null;
113
						while ($case !== 'q') {
114
							$case = $this->in(__("Enter a test case or (q)uit", true), null, 'q');
115
							if ($case !== 'q') {
116
								$cases[] = $case;
117
							}
118
						}
119
						if ($this->bake($class, $name, $cases)) {
120
							$this->out(__("Test baked\n", true));
121
							$type = null;
122
						}
123
						$class = null;
124
					}
125
				}
126
			} else {
127
				$this->_stop();
128
			}
129
		}
130
	}
131
/**
132
 * Writes File
133
 *
134
 * @access public
135
 */
136
	function bake($class, $name = null, $cases = array()) {
137
		if (!$name) {
138
			return false;
139
		}
140
 
141
		if (!is_array($cases)) {
142
			$cases = array($cases);
143
		}
144
 
145
		if (strpos($this->path, $class) === false) {
146
			$this->path .= 'cases' . DS . Inflector::tableize($class) . DS;
147
		}
148
 
149
		$class = Inflector::classify($class);
150
		$name = Inflector::classify($name);
151
 
152
		$import = $name;
153
		if (isset($this->plugin)) {
154
			$import = $this->plugin . '.' . $name;
155
		}
156
		$extras = $this->__extras($class);
157
		$out = "App::import('$class', '$import');\n";
158
		if ($class == 'Model') {
159
			$class = null;
160
		}
161
		$out .= "class Test{$name} extends {$name}{$class} {\n";
162
		$out .= "{$extras}";
163
		$out .= "}\n\n";
164
		$out .= "class {$name}{$class}Test extends CakeTestCase {\n";
165
		$out .= "\n\tfunction startTest() {";
166
		$out .= "\n\t\t\$this->{$name} = new Test{$name}();";
167
		$out .= "\n\t}\n";
168
		$out .= "\n\tfunction test{$name}Instance() {\n";
169
		$out .= "\t\t\$this->assertTrue(is_a(\$this->{$name}, '{$name}{$class}'));\n\t}\n";
170
		foreach ($cases as $case) {
171
			$case = Inflector::classify($case);
172
			$out .= "\n\tfunction test{$case}() {\n\n\t}\n";
173
		}
174
		$out .= "}\n";
175
 
176
		$this->out("Baking unit test for $name...");
177
		$this->out($out);
178
		$ok = $this->in(__('Is this correct?'), array('y', 'n'), 'y');
179
		if ($ok == 'n') {
180
			return false;
181
		}
182
 
183
		$header = '$Id';
184
		$content = "<?php \n/* SVN FILE: $header$ */\n/* ". $name ." Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
185
		return $this->createFile($this->path . Inflector::underscore($name) . '.test.php', $content);
186
	}
187
/**
188
 * Handles the extra stuff needed
189
 *
190
 * @access private
191
 */
192
	function __extras($class) {
193
		$extras = null;
194
		switch ($class) {
195
			case 'Model':
196
				$extras = "\n\tvar \$cacheSources = false;";
197
				$extras .= "\n\tvar \$useDbConfig = 'test_suite';\n";
198
			break;
199
		}
200
		return $extras;
201
	}
202
}
203
?>