1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18: 
 19: 
 20: App::uses('Shell', 'Console');
 21: App::uses('CakeTestSuiteDispatcher', 'TestSuite');
 22: App::uses('CakeTestSuiteCommand', 'TestSuite');
 23: App::uses('CakeTestLoader', 'TestSuite');
 24: 
 25:  26:  27:  28:  29:  30: 
 31: class TestShell extends Shell {
 32: 
 33:  34:  35:  36:  37: 
 38:     protected $_dispatcher = null;
 39: 
 40:  41:  42:  43:  44: 
 45:     public function getOptionParser() {
 46:         $parser = new ConsoleOptionParser($this->name);
 47: 
 48:         $parser->description(
 49:             __d('cake_console', 'The CakePHP Testsuite allows you to run test cases from the command line')
 50:         )->addArgument('category', array(
 51:             'help' => __d('cake_console', 'The category for the test, or test file, to test.'),
 52:             'required' => false
 53:         ))->addArgument('file', array(
 54:             'help' => __d('cake_console', 'The path to the file, or test file, to test.'),
 55:             'required' => false
 56:         ))->addOption('log-junit', array(
 57:             'help' => __d('cake_console', '<file> Log test execution in JUnit XML format to file.'),
 58:             'default' => false
 59:         ))->addOption('log-json', array(
 60:             'help' => __d('cake_console', '<file> Log test execution in JSON format to file.'),
 61:             'default' => false
 62:         ))->addOption('log-tap', array(
 63:             'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
 64:             'default' => false
 65:         ))->addOption('log-dbus', array(
 66:             'help' => __d('cake_console', 'Log test execution to DBUS.'),
 67:             'default' => false
 68:         ))->addOption('coverage-html', array(
 69:             'help' => __d('cake_console', '<dir> Generate code coverage report in HTML format.'),
 70:             'default' => false
 71:         ))->addOption('coverage-clover', array(
 72:             'help' => __d('cake_console', '<file> Write code coverage data in Clover XML format.'),
 73:             'default' => false
 74:         ))->addOption('coverage-text', array(
 75:             'help' => __d('cake_console', 'Output code coverage report in Text format.'),
 76:             'boolean' => true
 77:         ))->addOption('testdox-html', array(
 78:             'help' => __d('cake_console', '<file> Write agile documentation in HTML format to file.'),
 79:             'default' => false
 80:         ))->addOption('testdox-text', array(
 81:             'help' => __d('cake_console', '<file> Write agile documentation in Text format to file.'),
 82:             'default' => false
 83:         ))->addOption('filter', array(
 84:             'help' => __d('cake_console', '<pattern> Filter which tests to run.'),
 85:             'default' => false
 86:         ))->addOption('group', array(
 87:             'help' => __d('cake_console', '<name> Only runs tests from the specified group(s).'),
 88:             'default' => false
 89:         ))->addOption('exclude-group', array(
 90:             'help' => __d('cake_console', '<name> Exclude tests from the specified group(s).'),
 91:             'default' => false
 92:         ))->addOption('list-groups', array(
 93:             'help' => __d('cake_console', 'List available test groups.'),
 94:             'boolean' => true
 95:         ))->addOption('loader', array(
 96:             'help' => __d('cake_console', 'TestSuiteLoader implementation to use.'),
 97:             'default' => false
 98:         ))->addOption('repeat', array(
 99:             'help' => __d('cake_console', '<times> Runs the test(s) repeatedly.'),
100:             'default' => false
101:         ))->addOption('tap', array(
102:             'help' => __d('cake_console', 'Report test execution progress in TAP format.'),
103:             'boolean' => true
104:         ))->addOption('testdox', array(
105:             'help' => __d('cake_console', 'Report test execution progress in TestDox format.'),
106:             'default' => false,
107:             'boolean' => true
108:         ))->addOption('no-colors', array(
109:             'help' => __d('cake_console', 'Do not use colors in output.'),
110:             'boolean' => true
111:         ))->addOption('stderr', array(
112:             'help' => __d('cake_console', 'Write to STDERR instead of STDOUT.'),
113:             'boolean' => true
114:         ))->addOption('stop-on-error', array(
115:             'help' => __d('cake_console', 'Stop execution upon first error or failure.'),
116:             'boolean' => true
117:         ))->addOption('stop-on-failure', array(
118:             'help' => __d('cake_console', 'Stop execution upon first failure.'),
119:             'boolean' => true
120:         ))->addOption('stop-on-skipped', array(
121:             'help' => __d('cake_console', 'Stop execution upon first skipped test.'),
122:             'boolean' => true
123:         ))->addOption('stop-on-incomplete', array(
124:             'help' => __d('cake_console', 'Stop execution upon first incomplete test.'),
125:             'boolean' => true
126:         ))->addOption('strict', array(
127:             'help' => __d('cake_console', 'Mark a test as incomplete if no assertions are made.'),
128:             'boolean' => true
129:         ))->addOption('wait', array(
130:             'help' => __d('cake_console', 'Waits for a keystroke after each test.'),
131:             'boolean' => true
132:         ))->addOption('process-isolation', array(
133:             'help' => __d('cake_console', 'Run each test in a separate PHP process.'),
134:             'boolean' => true
135:         ))->addOption('no-globals-backup', array(
136:             'help' => __d('cake_console', 'Do not backup and restore $GLOBALS for each test.'),
137:             'boolean' => true
138:         ))->addOption('static-backup', array(
139:             'help' => __d('cake_console', 'Backup and restore static attributes for each test.'),
140:             'boolean' => true
141:         ))->addOption('syntax-check', array(
142:             'help' => __d('cake_console', 'Try to check source files for syntax errors.'),
143:             'boolean' => true
144:         ))->addOption('bootstrap', array(
145:             'help' => __d('cake_console', '<file> A "bootstrap" PHP file that is run before the tests.'),
146:             'default' => false
147:         ))->addOption('configuration', array(
148:             'help' => __d('cake_console', '<file> Read configuration from XML file.'),
149:             'default' => false
150:         ))->addOption('no-configuration', array(
151:             'help' => __d('cake_console', 'Ignore default configuration file (phpunit.xml).'),
152:             'boolean' => true
153:         ))->addOption('include-path', array(
154:             'help' => __d('cake_console', '<path(s)> Prepend PHP include_path with given path(s).'),
155:             'default' => false
156:         ))->addOption('directive', array(
157:             'help' => __d('cake_console', 'key[=value] Sets a php.ini value.'),
158:             'short' => 'd',
159:             'default' => false
160:         ))->addOption('fixture', array(
161:             'help' => __d('cake_console', 'Choose a custom fixture manager.')
162:         ))->addOption('debug', array(
163:             'help' => __d('cake_console', 'More verbose output.')
164:         ));
165: 
166:         return $parser;
167:     }
168: 
169: 170: 171: 172: 173: 174: 
175:     public function initialize() {
176:         $this->_dispatcher = new CakeTestSuiteDispatcher();
177:         $success = $this->_dispatcher->loadTestFramework();
178:         if (!$success) {
179:             throw new Exception(__d('cake_dev', 'Please install PHPUnit framework v3.7 <info>(http://www.phpunit.de)</info>'));
180:         }
181:     }
182: 
183: 184: 185: 186: 187: 
188:     protected function _parseArgs() {
189:         if (empty($this->args)) {
190:             return null;
191:         }
192:         $params = array(
193:             'core' => false,
194:             'app' => false,
195:             'plugin' => null,
196:             'output' => 'text',
197:         );
198: 
199:         if (strpos($this->args[0], '.php')) {
200:             $category = $this->_mapFileToCategory($this->args[0]);
201:             $params['case'] = $this->_mapFileToCase($this->args[0], $category);
202:         } else {
203:             $category = $this->args[0];
204:             if (isset($this->args[1])) {
205:                 $params['case'] = $this->args[1];
206:             }
207:         }
208: 
209:         if ($category === 'core') {
210:             $params['core'] = true;
211:         } elseif ($category === 'app') {
212:             $params['app'] = true;
213:         } else {
214:             $params['plugin'] = $category;
215:         }
216: 
217:         return $params;
218:     }
219: 
220: 221: 222: 223: 224: 
225:     protected function _runnerOptions() {
226:         $options = array();
227:         $params = $this->params;
228:         unset($params['help']);
229:         unset($params['quiet']);
230: 
231:         if (!empty($params['no-colors'])) {
232:             unset($params['no-colors'], $params['colors']);
233:         } else {
234:             $params['colors'] = true;
235:         }
236: 
237:         foreach ($params as $param => $value) {
238:             if ($value === false) {
239:                 continue;
240:             }
241:             if ($param === 'directive') {
242:                 $options[] = '-d';
243:             } else {
244:                 $options[] = '--' . $param;
245:             }
246:             if (is_string($value)) {
247:                 $options[] = $value;
248:             }
249:         }
250:         return $options;
251:     }
252: 
253: 254: 255: 256: 257: 
258:     public function main() {
259:         $this->out(__d('cake_console', 'CakePHP Test Shell'));
260:         $this->hr();
261: 
262:         $args = $this->_parseArgs();
263: 
264:         if (empty($args['case'])) {
265:             return $this->available();
266:         }
267: 
268:         $this->_run($args, $this->_runnerOptions());
269:     }
270: 
271: 272: 273: 274: 275: 276: 277: 
278:     protected function _run($runnerArgs, $options = array()) {
279:         restore_error_handler();
280:         restore_error_handler();
281: 
282:         $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
283:         $testCli->run($options);
284:     }
285: 
286: 287: 288: 289: 290: 
291:     public function available() {
292:         $params = $this->_parseArgs();
293:         $testCases = CakeTestLoader::generateTestList($params);
294:         $app = isset($params['app']) ? $params['app'] : null;
295:         $plugin = isset($params['plugin']) ? $params['plugin'] : null;
296: 
297:         $title = "Core Test Cases:";
298:         $category = 'core';
299:         if ($app) {
300:             $title = "App Test Cases:";
301:             $category = 'app';
302:         } elseif ($plugin) {
303:             $title = Inflector::humanize($plugin) . " Test Cases:";
304:             $category = $plugin;
305:         }
306: 
307:         if (empty($testCases)) {
308:             $this->out(__d('cake_console', "No test cases available \n\n"));
309:             return $this->out($this->OptionParser->help());
310:         }
311: 
312:         $this->out($title);
313:         $i = 1;
314:         $cases = array();
315:         foreach ($testCases as $testCase) {
316:             $case = str_replace('Test.php', '', $testCase);
317:             $this->out("[$i] $case");
318:             $cases[$i] = $case;
319:             $i++;
320:         }
321: 
322:         while ($choice = $this->in(__d('cake_console', 'What test case would you like to run?'), null, 'q')) {
323:             if (is_numeric($choice) && isset($cases[$choice])) {
324:                 $this->args[0] = $category;
325:                 $this->args[1] = $cases[$choice];
326:                 $this->_run($this->_parseArgs(), $this->_runnerOptions());
327:                 break;
328:             }
329: 
330:             if (is_string($choice) && in_array($choice, $cases)) {
331:                 $this->args[0] = $category;
332:                 $this->args[1] = $choice;
333:                 $this->_run($this->_parseArgs(), $this->_runnerOptions());
334:                 break;
335:             }
336: 
337:             if ($choice === 'q') {
338:                 break;
339:             }
340:         }
341:     }
342: 
343: 344: 345: 346: 347: 348: 349: 350: 351: 
352:     protected function _mapFileToCase($file, $category, $throwOnMissingFile = true) {
353:         if (!$category || (substr($file, -4) !== '.php')) {
354:             return false;
355:         }
356: 
357:         $_file = realpath($file);
358:         if ($_file) {
359:             $file = $_file;
360:         }
361: 
362:         $testFile = $testCase = null;
363:         $testCaseFolder = str_replace(APP, '', APP_TEST_CASES);
364:         if (preg_match('@Test[\\\/]@', $file)) {
365:             if (substr($file, -8) === 'Test.php') {
366:                 $testCase = substr($file, 0, -8);
367:                 $testCase = str_replace(DS, '/', $testCase);
368:                 $testCaseFolderEscaped = str_replace('/', '\/', $testCaseFolder);
369:                 $testCase = preg_replace('@.*' . $testCaseFolderEscaped . '\/@', '', $testCase);
370:                 if (!empty($testCase)) {
371:                     if ($category === 'core') {
372:                         $testCase = str_replace('lib/Cake', '', $testCase);
373:                     }
374:                     return $testCase;
375:                 }
376:                 throw new Exception(__d('cake_dev', 'Test case %s cannot be run via this shell', $testFile));
377:             }
378:         }
379: 
380:         $file = substr($file, 0, -4);
381:         if ($category === 'core') {
382: 
383:             $testCase = str_replace(DS, '/', $file);
384:             $testCase = preg_replace('@.*lib/Cake/@', '', $file);
385:             $testCase[0] = strtoupper($testCase[0]);
386:             $testFile = CAKE . 'Test/Case/' . $testCase . 'Test.php';
387: 
388:             if (!file_exists($testFile) && $throwOnMissingFile) {
389:                 throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
390:             }
391: 
392:             return $testCase;
393:         }
394: 
395:         if ($category === 'app') {
396:             $testFile = str_replace(APP, APP_TEST_CASES . '/', $file) . 'Test.php';
397:         } else {
398:             $testFile = preg_replace(
399:                 "@((?:plugins|Plugin)[\\/]{$category}[\\/])(.*)$@",
400:                 '\1' . $testCaseFolder . '/\2Test.php',
401:                 $file
402:             );
403:         }
404: 
405:         if (!file_exists($testFile) && $throwOnMissingFile) {
406:             throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
407:         }
408: 
409:         $testCase = substr($testFile, 0, -8);
410:         $testCase = str_replace(DS, '/', $testCase);
411:         $testCase = preg_replace('@.*' . $testCaseFolder . '/@', '', $testCase);
412:         return $testCase;
413:     }
414: 
415: 416: 417: 418: 419: 420: 
421:     protected function _mapFileToCategory($file) {
422:         $_file = realpath($file);
423:         if ($_file) {
424:             $file = $_file;
425:         }
426: 
427:         $file = str_replace(DS, '/', $file);
428:         if (strpos($file, 'lib/Cake/') !== false) {
429:             return 'core';
430:         } elseif (preg_match('@(?:plugins|Plugin)/([^/]*)@', $file, $match)) {
431:             return $match[1];
432:         }
433:         return 'app';
434:     }
435: 
436: }
437: