1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: App::uses('AppShell', 'Console/Command');
22: App::uses('CakeTestSuiteDispatcher', 'TestSuite');
23: App::uses('CakeTestSuiteCommand', 'TestSuite');
24: App::uses('CakeTestLoader', 'TestSuite');
25:
26: 27: 28: 29: 30: 31:
32: class TestsuiteShell extends AppShell {
33:
34: 35: 36: 37: 38:
39: protected $_dispatcher = null;
40:
41: 42: 43: 44: 45:
46: public function getOptionParser() {
47: $parser = new ConsoleOptionParser($this->name);
48: $parser->description(array(
49: __d('cake_console', 'The CakePHP Testsuite allows you to run test cases from the command line'),
50: __d('cake_console', 'If run with no command line arguments, a list of available core test cases will be shown')
51: ))->addArgument('category', array(
52: 'help' => __d('cake_console', 'app, core or name of a plugin.'),
53: 'required' => true
54: ))->addArgument('file', array(
55: 'help' => __d('cake_console', 'file name with folder prefix and without the test.php suffix.'),
56: 'required' => false,
57: ))->addOption('log-junit', array(
58: 'help' => __d('cake_console', '<file> Log test execution in JUnit XML format to file.'),
59: 'default' => false
60: ))->addOption('log-json', array(
61: 'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
62: 'default' => false
63: ))->addOption('log-tap', array(
64: 'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
65: 'default' => false
66: ))->addOption('log-dbus', array(
67: 'help' => __d('cake_console', 'Log test execution to DBUS.'),
68: 'default' => false
69: ))->addOption('coverage-html', array(
70: 'help' => __d('cake_console', '<dir> Generate code coverage report in HTML format.'),
71: 'default' => false
72: ))->addOption('coverage-clover', array(
73: 'help' => __d('cake_console', '<file> Write code coverage data in Clover XML format.'),
74: 'default' => false
75: ))->addOption('testdox-html', array(
76: 'help' => __d('cake_console', '<file> Write agile documentation in HTML format to file.'),
77: 'default' => false
78: ))->addOption('testdox-text', array(
79: 'help' => __d('cake_console', '<file> Write agile documentation in Text format to file.'),
80: 'default' => false
81: ))->addOption('filter', array(
82: 'help' => __d('cake_console', '<pattern> Filter which tests to run.'),
83: 'default' => false
84: ))->addOption('group', array(
85: 'help' => __d('cake_console', '<name> Only runs tests from the specified group(s).'),
86: 'default' => false
87: ))->addOption('exclude-group', array(
88: 'help' => __d('cake_console', '<name> Exclude tests from the specified group(s).'),
89: 'default' => false
90: ))->addOption('list-groups', array(
91: 'help' => __d('cake_console', 'List available test groups.'),
92: 'boolean' => true
93: ))->addOption('loader', array(
94: 'help' => __d('cake_console', 'TestSuiteLoader implementation to use.'),
95: 'default' => false
96: ))->addOption('repeat', array(
97: 'help' => __d('cake_console', '<times> Runs the test(s) repeatedly.'),
98: 'default' => false
99: ))->addOption('tap', array(
100: 'help' => __d('cake_console', 'Report test execution progress in TAP format.'),
101: 'boolean' => true
102: ))->addOption('testdox', array(
103: 'help' => __d('cake_console', 'Report test execution progress in TestDox format.'),
104: 'default' => false,
105: 'boolean' => true
106: ))->addOption('no-colors', array(
107: 'help' => __d('cake_console', 'Do not use colors in output.'),
108: 'boolean' => true
109: ))->addOption('stderr', array(
110: 'help' => __d('cake_console', 'Write to STDERR instead of STDOUT.'),
111: 'boolean' => true
112: ))->addOption('stop-on-error', array(
113: 'help' => __d('cake_console', 'Stop execution upon first error or failure.'),
114: 'boolean' => true
115: ))->addOption('stop-on-failure', array(
116: 'help' => __d('cake_console', 'Stop execution upon first failure.'),
117: 'boolean' => true
118: ))->addOption('stop-on-skipped ', array(
119: 'help' => __d('cake_console', 'Stop execution upon first skipped test.'),
120: 'boolean' => true
121: ))->addOption('stop-on-incomplete', array(
122: 'help' => __d('cake_console', 'Stop execution upon first incomplete test.'),
123: 'boolean' => true
124: ))->addOption('strict', array(
125: 'help' => __d('cake_console', 'Mark a test as incomplete if no assertions are made.'),
126: 'boolean' => true
127: ))->addOption('wait', array(
128: 'help' => __d('cake_console', 'Waits for a keystroke after each test.'),
129: 'boolean' => true
130: ))->addOption('process-isolation', array(
131: 'help' => __d('cake_console', 'Run each test in a separate PHP process.'),
132: 'boolean' => true
133: ))->addOption('no-globals-backup', array(
134: 'help' => __d('cake_console', 'Do not backup and restore $GLOBALS for each test.'),
135: 'boolean' => true
136: ))->addOption('static-backup ', array(
137: 'help' => __d('cake_console', 'Backup and restore static attributes for each test.'),
138: 'boolean' => true
139: ))->addOption('syntax-check', array(
140: 'help' => __d('cake_console', 'Try to check source files for syntax errors.'),
141: 'boolean' => true
142: ))->addOption('bootstrap', array(
143: 'help' => __d('cake_console', '<file> A "bootstrap" PHP file that is run before the tests.'),
144: 'default' => false
145: ))->addOption('configuration', array(
146: 'help' => __d('cake_console', '<file> Read configuration from XML file.'),
147: 'default' => false
148: ))->addOption('no-configuration', array(
149: 'help' => __d('cake_console', 'Ignore default configuration file (phpunit.xml).'),
150: 'boolean' => true
151: ))->addOption('include-path', array(
152: 'help' => __d('cake_console', '<path(s)> Prepend PHP include_path with given path(s).'),
153: 'default' => false
154: ))->addOption('directive', array(
155: 'help' => __d('cake_console', 'key[=value] Sets a php.ini value.'),
156: 'default' => false
157: ))->addOption('fixture', array(
158: 'help' => __d('cake_console', 'Choose a custom fixture manager.'),
159: ))->addOption('debug', array(
160: 'help' => __d('cake_console', 'Enable full output of testsuite. (supported in PHPUnit 3.6.0 and greater)'),
161: ));
162:
163: return $parser;
164: }
165:
166: 167: 168: 169: 170: 171:
172: public function initialize() {
173: $this->_dispatcher = new CakeTestSuiteDispatcher();
174: $sucess = $this->_dispatcher->loadTestFramework();
175: if (!$sucess) {
176: throw new Exception(__d('cake_dev', 'Please install PHPUnit framework <info>(http://www.phpunit.de)</info>'));
177: }
178: }
179:
180: 181: 182: 183: 184:
185: protected function _parseArgs() {
186: if (empty($this->args)) {
187: return;
188: }
189: $params = array(
190: 'core' => false,
191: 'app' => false,
192: 'plugin' => null,
193: 'output' => 'text',
194: );
195:
196: $category = $this->args[0];
197:
198: if ($category == 'core') {
199: $params['core'] = true;
200: } elseif ($category == 'app') {
201: $params['app'] = true;
202: } elseif ($category != 'core') {
203: $params['plugin'] = $category;
204: }
205:
206: if (isset($this->args[1])) {
207: $params['case'] = $this->args[1];
208: }
209: return $params;
210: }
211:
212: 213: 214: 215: 216:
217: protected function _runnerOptions() {
218: $options = array();
219: $params = $this->params;
220: unset($params['help']);
221:
222: if (!empty($params['no-colors'])) {
223: unset($params['no-colors'], $params['colors']);
224: } else {
225: $params['colors'] = true;
226: }
227:
228: foreach ($params as $param => $value) {
229: if ($value === false) {
230: continue;
231: }
232: $options[] = '--' . $param;
233: if (is_string($value)) {
234: $options[] = $value;
235: }
236: }
237: return $options;
238: }
239:
240: 241: 242: 243: 244:
245: public function main() {
246: $this->out(__d('cake_console', 'CakePHP Test Shell'));
247: $this->hr();
248:
249: $args = $this->_parseArgs();
250:
251: if (empty($args['case'])) {
252: return $this->available();
253: }
254:
255: $this->_run($args, $this->_runnerOptions());
256: }
257:
258: 259: 260: 261: 262: 263: 264:
265: protected function _run($runnerArgs, $options = array()) {
266: restore_error_handler();
267: restore_error_handler();
268:
269: $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
270: $testCli->run($options);
271: }
272:
273: 274: 275: 276: 277:
278: public function available() {
279: $params = $this->_parseArgs();
280: $testCases = CakeTestLoader::generateTestList($params);
281: $app = $params['app'];
282: $plugin = $params['plugin'];
283:
284: $title = "Core Test Cases:";
285: $category = 'core';
286: if ($app) {
287: $title = "App Test Cases:";
288: $category = 'app';
289: } elseif ($plugin) {
290: $title = Inflector::humanize($plugin) . " Test Cases:";
291: $category = $plugin;
292: }
293:
294: if (empty($testCases)) {
295: $this->out(__d('cake_console', "No test cases available \n\n"));
296: return $this->out($this->OptionParser->help());
297: }
298:
299: $this->out($title);
300: $i = 1;
301: $cases = array();
302: foreach ($testCases as $testCaseFile => $testCase) {
303: $case = str_replace('Test.php', '', $testCase);
304: $this->out("[$i] $case");
305: $cases[$i] = $case;
306: $i++;
307: }
308:
309: while ($choice = $this->in(__d('cake_console', 'What test case would you like to run?'), null, 'q')) {
310: if (is_numeric($choice) && isset($cases[$choice])) {
311: $this->args[0] = $category;
312: $this->args[1] = $cases[$choice];
313: $this->_run($this->_parseArgs(), $this->_runnerOptions());
314: break;
315: }
316:
317: if (is_string($choice) && in_array($choice, $cases)) {
318: $this->args[0] = $category;
319: $this->args[1] = $choice;
320: $this->_run($this->_parseArgs(), $this->_runnerOptions());
321: break;
322: }
323:
324: if ($choice == 'q') {
325: break;
326: }
327: }
328: }
329: }
330: