1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21:
22: App::uses('Shell', 'Console');
23: App::uses('CakeTestSuiteDispatcher', 'TestSuite');
24: App::uses('CakeTestSuiteCommand', 'TestSuite');
25: App::uses('CakeTestLoader', 'TestSuite');
26:
27: 28: 29: 30: 31: 32:
33: class TestShell extends Shell {
34:
35: 36: 37: 38: 39:
40: protected $_dispatcher = null;
41:
42: 43: 44: 45: 46:
47: public function getOptionParser() {
48: $parser = new ConsoleOptionParser($this->name);
49: $parser->description(array(
50: __d('cake_console', 'The CakePHP Testsuite allows you to run test cases from the command line'),
51: ))->addArgument('category', array(
52: 'help' => __d('cake_console', 'The category for the test, or test file, to test.'),
53: 'required' => false,
54: ))->addArgument('file', array(
55: 'help' => __d('cake_console', 'The path to the file, or test file, to test.'),
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 JSON 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', 'More verbose output.'),
161: ));
162:
163: return $parser;
164: }
165:
166: 167: 168: 169: 170: 171:
172: public function initialize() {
173: $this->_dispatcher = new CakeTestSuiteDispatcher();
174: $success = $this->_dispatcher->loadTestFramework();
175: if (!$success) {
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: if (strpos($this->args[0], '.php')) {
197: $category = $this->_mapFileToCategory($this->args[0]);
198: $params['case'] = $this->_mapFileToCase($this->args[0], $category);
199: } else {
200: $category = $this->args[0];
201: if (isset($this->args[1])) {
202: $params['case'] = $this->args[1];
203: }
204: }
205:
206: if ($category === 'core') {
207: $params['core'] = true;
208: } elseif ($category === 'app') {
209: $params['app'] = true;
210: } else {
211: $params['plugin'] = $category;
212: }
213:
214: return $params;
215: }
216:
217: 218: 219: 220: 221:
222: protected function _runnerOptions() {
223: $options = array();
224: $params = $this->params;
225: unset($params['help']);
226:
227: if (!empty($params['no-colors'])) {
228: unset($params['no-colors'], $params['colors']);
229: } else {
230: $params['colors'] = true;
231: }
232:
233: foreach ($params as $param => $value) {
234: if ($value === false) {
235: continue;
236: }
237: $options[] = '--' . $param;
238: if (is_string($value)) {
239: $options[] = $value;
240: }
241: }
242: return $options;
243: }
244:
245: 246: 247: 248: 249:
250: public function main() {
251: $this->out(__d('cake_console', 'CakePHP Test Shell'));
252: $this->hr();
253:
254: $args = $this->_parseArgs();
255:
256: if (empty($args['case'])) {
257: return $this->available();
258: }
259:
260: $this->_run($args, $this->_runnerOptions());
261: }
262:
263: 264: 265: 266: 267: 268: 269:
270: protected function _run($runnerArgs, $options = array()) {
271: restore_error_handler();
272: restore_error_handler();
273:
274: $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
275: $testCli->run($options);
276: }
277:
278: 279: 280: 281: 282:
283: public function available() {
284: $params = $this->_parseArgs();
285: $testCases = CakeTestLoader::generateTestList($params);
286: $app = $params['app'];
287: $plugin = $params['plugin'];
288:
289: $title = "Core Test Cases:";
290: $category = 'core';
291: if ($app) {
292: $title = "App Test Cases:";
293: $category = 'app';
294: } elseif ($plugin) {
295: $title = Inflector::humanize($plugin) . " Test Cases:";
296: $category = $plugin;
297: }
298:
299: if (empty($testCases)) {
300: $this->out(__d('cake_console', "No test cases available \n\n"));
301: return $this->out($this->OptionParser->help());
302: }
303:
304: $this->out($title);
305: $i = 1;
306: $cases = array();
307: foreach ($testCases as $testCase) {
308: $case = str_replace('Test.php', '', $testCase);
309: $this->out("[$i] $case");
310: $cases[$i] = $case;
311: $i++;
312: }
313:
314: while ($choice = $this->in(__d('cake_console', 'What test case would you like to run?'), null, 'q')) {
315: if (is_numeric($choice) && isset($cases[$choice])) {
316: $this->args[0] = $category;
317: $this->args[1] = $cases[$choice];
318: $this->_run($this->_parseArgs(), $this->_runnerOptions());
319: break;
320: }
321:
322: if (is_string($choice) && in_array($choice, $cases)) {
323: $this->args[0] = $category;
324: $this->args[1] = $choice;
325: $this->_run($this->_parseArgs(), $this->_runnerOptions());
326: break;
327: }
328:
329: if ($choice === 'q') {
330: break;
331: }
332: }
333: }
334:
335: 336: 337: 338: 339: 340: 341: 342: 343:
344: protected function _mapFileToCase($file, $category, $throwOnMissingFile = true) {
345: if (!$category || (substr($file, -4) !== '.php')) {
346: return false;
347: }
348:
349: $_file = realpath($file);
350: if ($_file) {
351: $file = $_file;
352: }
353:
354: $testFile = $testCase = null;
355:
356: if (preg_match('@Test[\\\/]@', $file)) {
357:
358: if (substr($file, -8) === 'Test.php') {
359:
360: $testCase = substr($file, 0, -8);
361: $testCase = str_replace(DS, '/', $testCase);
362:
363: if ($testCase = preg_replace('@.*Test\/Case\/@', '', $testCase)) {
364:
365: if ($category === 'core') {
366: $testCase = str_replace('lib/Cake', '', $testCase);
367: }
368:
369: return $testCase;
370: }
371:
372: throw new Exception(__d('cake_dev', 'Test case %s cannot be run via this shell', $testFile));
373: }
374: }
375:
376: $file = substr($file, 0, -4);
377: if ($category === 'core') {
378:
379: $testCase = str_replace(DS, '/', $file);
380: $testCase = preg_replace('@.*lib/Cake/@', '', $file);
381: $testCase[0] = strtoupper($testCase[0]);
382: $testFile = CAKE . 'Test/Case/' . $testCase . 'Test.php';
383:
384: if (!file_exists($testFile) && $throwOnMissingFile) {
385: throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
386: }
387:
388: return $testCase;
389: }
390:
391: if ($category === 'app') {
392: $testFile = str_replace(APP, APP . 'Test/Case/', $file) . 'Test.php';
393: } else {
394: $testFile = preg_replace(
395: "@((?:plugins|Plugin)[\\/]{$category}[\\/])(.*)$@",
396: '\1Test/Case/\2Test.php',
397: $file
398: );
399: }
400:
401: if (!file_exists($testFile) && $throwOnMissingFile) {
402: throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
403: }
404:
405: $testCase = substr($testFile, 0, -8);
406: $testCase = str_replace(DS, '/', $testCase);
407: $testCase = preg_replace('@.*Test/Case/@', '', $testCase);
408:
409: return $testCase;
410: }
411:
412: 413: 414: 415: 416: 417:
418: protected function _mapFileToCategory($file) {
419: $_file = realpath($file);
420: if ($_file) {
421: $file = $_file;
422: }
423:
424: $file = str_replace(DS, '/', $file);
425: if (strpos($file, 'lib/Cake/') !== false) {
426: return 'core';
427: } elseif (preg_match('@(?:plugins|Plugin)/([^/]*)@', $file, $match)) {
428: return $match[1];
429: }
430: return 'app';
431: }
432:
433: }
434: