5 * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Sebastian Bergmann <sebastian@phpunit.de>
40 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42 * @link http://www.phpunit.de/
43 * @since File available since Release 3.0.0
47 * A TestRunner for the Command Line Interface (CLI)
52 * @author Sebastian Bergmann <sebastian@phpunit.de>
53 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
54 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
55 * @version Release: 3.5.13
56 * @link http://www.phpunit.de/
57 * @since Class available since Release 3.0.0
59 class PHPUnit_TextUI_Command
64 protected $arguments = array(
65 'listGroups' => FALSE,
67 'syntaxCheck' => FALSE,
68 'useDefaultConfiguration' => TRUE
74 protected $options = array();
79 protected $longOptions = array(
82 'configuration=' => NULL,
83 'coverage-html=' => NULL,
84 'coverage-clover=' => NULL,
86 'exclude-group=' => NULL,
90 'include-path=' => NULL,
91 'list-groups' => NULL,
97 'process-isolation' => NULL,
99 'skeleton-class' => NULL,
100 'skeleton-test' => NULL,
102 'stop-on-error' => NULL,
103 'stop-on-failure' => NULL,
104 'stop-on-incomplete' => NULL,
105 'stop-on-skipped' => NULL,
107 'story-html=' => NULL,
108 'story-text=' => NULL,
110 'syntax-check' => NULL,
113 'testdox-html=' => NULL,
114 'testdox-text=' => NULL,
115 'no-configuration' => NULL,
116 'no-globals-backup' => NULL,
117 'static-backup' => NULL,
124 * @param boolean $exit
126 public static function main($exit = TRUE)
128 $command = new PHPUnit_TextUI_Command;
129 $command->run($_SERVER['argv'], $exit);
134 * @param boolean $exit
136 public function run(array $argv, $exit = TRUE)
138 $this->handleArguments($argv);
140 $runner = new PHPUnit_TextUI_TestRunner($this->arguments['loader']);
142 if (is_object($this->arguments['test']) &&
143 $this->arguments['test'] instanceof PHPUnit_Framework_Test) {
144 $suite = $this->arguments['test'];
146 $suite = $runner->getTest(
147 $this->arguments['test'],
148 $this->arguments['testFile'],
149 $this->arguments['syntaxCheck']
153 if (count($suite) == 0) {
154 $skeleton = new PHPUnit_Util_Skeleton_Test(
156 $this->arguments['testFile']
159 $result = $skeleton->generate(TRUE);
161 if (!$result['incomplete']) {
162 eval(str_replace(array('<?php', '?>'), '', $result['code']));
163 $suite = new PHPUnit_Framework_TestSuite(
164 $this->arguments['test'] . 'Test'
169 if ($this->arguments['listGroups']) {
170 PHPUnit_TextUI_TestRunner::printVersionString();
172 print "Available test group(s):\n";
174 $groups = $suite->getGroups();
177 foreach ($groups as $group) {
181 exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
184 unset($this->arguments['test']);
185 unset($this->arguments['testFile']);
188 $result = $runner->doRun($suite, $this->arguments);
191 catch (PHPUnit_Framework_Exception $e) {
192 print $e->getMessage() . "\n";
196 if (isset($result) && $result->wasSuccessful()) {
197 exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
200 else if (!isset($result) || $result->errorCount() > 0) {
201 exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
205 exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
211 * Handles the command-line arguments.
213 * A child class of PHPUnit_TextUI_Command can hook into the argument
214 * parsing by adding the switch(es) to the $longOptions array and point to a
215 * callback method that handles the switch(es) in the child class like this
219 * class MyCommand extends PHPUnit_TextUI_Command
221 * public function __construct()
223 * $this->longOptions['--my-switch'] = 'myHandler';
226 * // --my-switch foo -> myHandler('foo')
227 * protected function myHandler($value)
235 protected function handleArguments(array $argv)
238 $this->options = PHPUnit_Util_Getopt::getopt(
241 array_keys($this->longOptions)
245 catch (RuntimeException $e) {
246 PHPUnit_TextUI_TestRunner::showError($e->getMessage());
249 $skeletonClass = FALSE;
250 $skeletonTest = FALSE;
252 foreach ($this->options[0] as $option) {
253 switch ($option[0]) {
255 $this->arguments['colors'] = TRUE;
259 case '--bootstrap': {
260 $this->arguments['bootstrap'] = $option[1];
265 case '--configuration': {
266 $this->arguments['configuration'] = $option[1];
270 case '--coverage-clover': {
271 if (extension_loaded('tokenizer') &&
272 extension_loaded('xdebug')) {
273 $this->arguments['coverageClover'] = $option[1];
275 if (!extension_loaded('tokenizer')) {
277 'The tokenizer extension is not loaded.'
281 'The Xdebug extension is not loaded.'
288 case '--coverage-html': {
289 if (extension_loaded('tokenizer') &&
290 extension_loaded('xdebug')) {
291 $this->arguments['reportDirectory'] = $option[1];
293 if (!extension_loaded('tokenizer')) {
295 'The tokenizer extension is not loaded.'
299 'The Xdebug extension is not loaded.'
307 $ini = explode('=', $option[1]);
309 if (isset($ini[0])) {
310 if (isset($ini[1])) {
311 ini_set($ini[0], $ini[1]);
313 ini_set($ini[0], TRUE);
320 $this->arguments['debug'] = TRUE;
326 exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
331 $this->arguments['filter'] = $option[1];
336 $this->arguments['groups'] = explode(',', $option[1]);
340 case '--exclude-group': {
341 $this->arguments['excludeGroups'] = explode(
347 case '--include-path': {
348 $includePath = $option[1];
352 case '--list-groups': {
353 $this->arguments['listGroups'] = TRUE;
358 $this->arguments['loader'] = $option[1];
363 $this->arguments['logDbus'] = TRUE;
368 $this->arguments['jsonLogfile'] = $option[1];
372 case '--log-junit': {
373 $this->arguments['junitLogfile'] = $option[1];
378 $this->arguments['tapLogfile'] = $option[1];
382 case '--process-isolation': {
383 $this->arguments['processIsolation'] = TRUE;
384 $this->arguments['syntaxCheck'] = FALSE;
389 $this->arguments['repeat'] = (int)$option[1];
394 $this->arguments['printer'] = new PHPUnit_TextUI_ResultPrinter(
396 isset($this->arguments['verbose']) ? $this->arguments['verbose'] : FALSE
401 case '--stop-on-error': {
402 $this->arguments['stopOnError'] = TRUE;
406 case '--stop-on-failure': {
407 $this->arguments['stopOnFailure'] = TRUE;
411 case '--stop-on-incomplete': {
412 $this->arguments['stopOnIncomplete'] = TRUE;
416 case '--stop-on-skipped': {
417 $this->arguments['stopOnSkipped'] = TRUE;
421 case '--skeleton-test': {
422 $skeletonTest = TRUE;
423 $skeletonClass = FALSE;
427 case '--skeleton-class': {
428 $skeletonClass = TRUE;
429 $skeletonTest = FALSE;
434 $this->arguments['printer'] = new PHPUnit_Util_Log_TAP;
440 'The --story functionality is deprecated and ' .
441 'will be removed in the future.',
445 $this->arguments['printer'] = new PHPUnit_Extensions_Story_ResultPrinter_Text;
449 case '--story-html': {
451 'The --story-html functionality is deprecated and ' .
452 'will be removed in the future.',
456 $this->arguments['storyHTMLFile'] = $option[1];
460 case '--story-text': {
462 'The --story-text functionality is deprecated and ' .
463 'will be removed in the future.',
467 $this->arguments['storyTextFile'] = $option[1];
471 case '--syntax-check': {
472 $this->arguments['syntaxCheck'] = TRUE;
477 $this->arguments['printer'] = new PHPUnit_Util_TestDox_ResultPrinter_Text;
481 case '--testdox-html': {
482 $this->arguments['testdoxHTMLFile'] = $option[1];
486 case '--testdox-text': {
487 $this->arguments['testdoxTextFile'] = $option[1];
491 case '--no-configuration': {
492 $this->arguments['useDefaultConfiguration'] = FALSE;
496 case '--no-globals-backup': {
497 $this->arguments['backupGlobals'] = FALSE;
501 case '--static-backup': {
502 $this->arguments['backupStaticAttributes'] = TRUE;
507 $this->arguments['verbose'] = TRUE;
512 PHPUnit_TextUI_TestRunner::printVersionString();
513 exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
518 $this->arguments['wait'] = TRUE;
523 $this->arguments['strict'] = TRUE;
528 $optionName = str_replace('--', '', $option[0]);
530 if (isset($this->longOptions[$optionName])) {
531 $handler = $this->longOptions[$optionName];
534 else if (isset($this->longOptions[$optionName . '='])) {
535 $handler = $this->longOptions[$optionName . '='];
538 if (isset($handler) && is_callable(array($this, $handler))) {
539 $this->$handler($option[1]);
545 if (isset($this->arguments['printer']) &&
546 $this->arguments['printer'] instanceof PHPUnit_Extensions_Story_ResultPrinter_Text &&
547 isset($this->arguments['processIsolation']) &&
548 $this->arguments['processIsolation']) {
550 'The story result printer cannot be used in process isolation.'
554 $this->handleCustomTestSuite();
556 if (!isset($this->arguments['test'])) {
557 if (isset($this->options[1][0])) {
558 $this->arguments['test'] = $this->options[1][0];
561 if (isset($this->options[1][1])) {
562 $this->arguments['testFile'] = $this->options[1][1];
564 $this->arguments['testFile'] = '';
567 if (isset($this->arguments['test']) && is_file($this->arguments['test'])) {
568 $this->arguments['testFile'] = realpath($this->arguments['test']);
569 $this->arguments['test'] = substr($this->arguments['test'], 0, strrpos($this->arguments['test'], '.'));
573 if (isset($includePath)) {
576 $includePath . PATH_SEPARATOR . ini_get('include_path')
580 if (isset($this->arguments['bootstrap'])) {
581 $this->handleBootstrap($this->arguments['bootstrap'], $this->arguments['syntaxCheck']);
584 if ($this->arguments['loader'] !== NULL) {
585 $this->arguments['loader'] = $this->handleLoader($this->arguments['loader']);
588 if (isset($this->arguments['configuration']) &&
589 is_dir($this->arguments['configuration'])) {
590 $configurationFile = $this->arguments['configuration'] .
593 if (file_exists($configurationFile)) {
594 $this->arguments['configuration'] = realpath(
599 else if (file_exists($configurationFile . '.dist')) {
600 $this->arguments['configuration'] = realpath(
601 $configurationFile . '.dist'
606 else if (!isset($this->arguments['configuration']) &&
607 $this->arguments['useDefaultConfiguration']) {
608 if (file_exists('phpunit.xml')) {
609 $this->arguments['configuration'] = realpath('phpunit.xml');
610 } else if (file_exists('phpunit.xml.dist')) {
611 $this->arguments['configuration'] = realpath(
617 if (isset($this->arguments['configuration'])) {
619 $configuration = PHPUnit_Util_Configuration::getInstance(
620 $this->arguments['configuration']
624 catch (Exception $e) {
625 print $e->getMessage() . "\n";
626 exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
629 $phpunit = $configuration->getPHPUnitConfiguration();
631 if (isset($phpunit['syntaxCheck'])) {
632 $this->arguments['syntaxCheck'] = $phpunit['syntaxCheck'];
635 if (isset($phpunit['testSuiteLoaderClass'])) {
636 if (isset($phpunit['testSuiteLoaderFile'])) {
637 $file = $phpunit['testSuiteLoaderFile'];
642 $this->arguments['loader'] = $this->handleLoader(
643 $phpunit['testSuiteLoaderClass'], $file
647 $configuration->handlePHPConfiguration();
649 if (!isset($this->arguments['bootstrap'])) {
650 $phpunitConfiguration = $configuration->getPHPUnitConfiguration();
652 if (isset($phpunitConfiguration['bootstrap'])) {
653 $this->handleBootstrap($phpunitConfiguration['bootstrap'], $this->arguments['syntaxCheck']);
657 $browsers = $configuration->getSeleniumBrowserConfiguration();
659 if (!empty($browsers)) {
660 PHPUnit_Extensions_SeleniumTestCase::$browsers = $browsers;
663 if (!isset($this->arguments['test'])) {
664 $testSuite = $configuration->getTestSuiteConfiguration(
665 $this->arguments['syntaxCheck']
668 if ($testSuite !== NULL) {
669 $this->arguments['test'] = $testSuite;
674 if (isset($this->arguments['test']) && is_string($this->arguments['test']) && substr($this->arguments['test'], -5, 5) == '.phpt') {
675 $test = new PHPUnit_Extensions_PhptTestCase($this->arguments['test']);
677 $this->arguments['test'] = new PHPUnit_Framework_TestSuite;
678 $this->arguments['test']->addTest($test);
681 if (!isset($this->arguments['test']) ||
682 (isset($this->arguments['testDatabaseLogRevision']) && !isset($this->arguments['testDatabaseDSN']))) {
684 exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
687 if (!isset($this->arguments['syntaxCheck'])) {
688 $this->arguments['syntaxCheck'] = FALSE;
691 if ($skeletonClass || $skeletonTest) {
692 if (isset($this->arguments['test']) && $this->arguments['test'] !== FALSE) {
693 PHPUnit_TextUI_TestRunner::printVersionString();
695 if ($skeletonClass) {
696 $class = 'PHPUnit_Util_Skeleton_Class';
698 $class = 'PHPUnit_Util_Skeleton_Test';
703 $reflector = new ReflectionClass($class);
705 for ($i = 0; $i <= 3; $i++) {
706 if (isset($this->options[1][$i])) {
707 $args[] = $this->options[1][$i];
711 $skeleton = $reflector->newInstanceArgs($args);
715 catch (Exception $e) {
716 print $e->getMessage() . "\n";
717 exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
721 'Wrote skeleton for "%s" to "%s".' . "\n",
722 $skeleton->getOutClassName(),
723 $skeleton->getOutSourceFile()
726 exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
729 exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
735 * Handles the loading of the PHPUnit_Runner_TestSuiteLoader implementation.
737 * @param string $loaderClass
738 * @param string $loaderFile
740 protected function handleLoader($loaderClass, $loaderFile = '')
742 if (!class_exists($loaderClass, FALSE)) {
743 if ($loaderFile == '') {
744 $loaderFile = PHPUnit_Util_Filesystem::classNameToFilename(
749 $loaderFile = PHPUnit_Util_Filesystem::fileExistsInIncludePath(
753 if ($loaderFile !== FALSE) {
758 if (class_exists($loaderClass, FALSE)) {
759 $class = new ReflectionClass($loaderClass);
761 if ($class->implementsInterface('PHPUnit_Runner_TestSuiteLoader') &&
762 $class->isInstantiable()) {
763 $loader = $class->newInstance();
767 if (!isset($loader)) {
768 PHPUnit_TextUI_TestRunner::showError(
770 'Could not use "%s" as loader.',
781 * Loads a bootstrap file.
783 * @param string $filename
784 * @param boolean $syntaxCheck
786 protected function handleBootstrap($filename, $syntaxCheck = FALSE)
789 PHPUnit_Util_Fileloader::checkAndLoad($filename, $syntaxCheck);
792 catch (RuntimeException $e) {
793 PHPUnit_TextUI_TestRunner::showError($e->getMessage());
800 * @param string $message
801 * @param boolean $exit
803 protected function showMessage($message, $exit = TRUE)
805 PHPUnit_TextUI_TestRunner::printVersionString();
806 print $message . "\n";
809 exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
816 * Show the help message.
818 protected function showHelp()
820 PHPUnit_TextUI_TestRunner::printVersionString();
823 Usage: phpunit [switches] UnitTest [UnitTest.php]
824 phpunit [switches] <directory>
826 --log-junit <file> Log test execution in JUnit XML format to file.
827 --log-tap <file> Log test execution in TAP format to file.
828 --log-dbus Log test execution to DBUS.
829 --log-json <file> Log test execution in JSON format.
831 --coverage-html <dir> Generate code coverage report in HTML format.
832 --coverage-clover <file> Write code coverage data in Clover XML format.
834 --testdox-html <file> Write agile documentation in HTML format to file.
835 --testdox-text <file> Write agile documentation in Text format to file.
837 --filter <pattern> Filter which tests to run.
838 --group ... Only runs tests from the specified group(s).
839 --exclude-group ... Exclude tests from the specified group(s).
840 --list-groups List available test groups.
842 --loader <loader> TestSuiteLoader implementation to use.
843 --repeat <times> Runs the test(s) repeatedly.
845 --tap Report test execution progress in TAP format.
846 --testdox Report test execution progress in TestDox format.
848 --colors Use colors in output.
849 --stderr Write to STDERR instead of STDOUT.
850 --stop-on-error Stop execution upon first error.
851 --stop-on-failure Stop execution upon first error or failure.
852 --stop-on-skipped Stop execution upon first skipped test.
853 --stop-on-incomplete Stop execution upon first incomplete test.
854 --strict Mark a test as incomplete if no assertions are made.
855 --verbose Output more verbose information.
856 --wait Waits for a keystroke after each test.
858 --skeleton-class Generate Unit class for UnitTest in UnitTest.php.
859 --skeleton-test Generate UnitTest class for Unit in Unit.php.
861 --process-isolation Run each test in a separate PHP process.
862 --no-globals-backup Do not backup and restore \$GLOBALS for each test.
863 --static-backup Backup and restore static attributes for each test.
864 --syntax-check Try to check source files for syntax errors.
866 --bootstrap <file> A "bootstrap" PHP file that is run before the tests.
867 -c|--configuration <file> Read configuration from XML file.
868 --no-configuration Ignore default configuration file (phpunit.xml).
869 --include-path <path(s)> Prepend PHP's include_path with given path(s).
870 -d key[=value] Sets a php.ini value.
872 --help Prints this usage information.
873 --version Prints the version and exits.
875 --debug Output debugging information.
881 * Custom callback for test suite discovery.
883 protected function handleCustomTestSuite()