Example #1
0
 /**
  * Wraps both <code>addTest()</code> and <code>addTestSuite</code>
  * as well as the separate import statements for the user's convenience.
  *
  * If the named file cannot be read or there are no new tests that can be
  * added, a <code>PHPUnit2_Framework_Warning</code> will be created instead,
  * leaving the current test run untouched.
  *
  * @param  string $filename
  * @throws Exception
  * @access public
  * @since  Method available since Release 2.3.0
  * @author Stefano F. Rausch <*****@*****.**>
  */
 public function addTestFile($filename)
 {
     if (!is_string($filename) || !file_exists($filename)) {
         throw new Exception();
     }
     $declaredClasses = get_declared_classes();
     PHPUnit2_Util_Fileloader::checkAndLoad($filename);
     $newClasses = array_values(array_diff(get_declared_classes(), $declaredClasses));
     $testsFound = 0;
     foreach ($newClasses as $class) {
         if (preg_match('"Tests?$"', $class)) {
             try {
                 $suiteMethod = new ReflectionMethod($class, PHPUnit2_Runner_BaseTestRunner::SUITE_METHODNAME);
                 $this->addTest($suiteMethod->invoke(NULL));
             } catch (ReflectionException $e) {
                 $this->addTestSuite(new ReflectionClass($class));
             }
             $testsFound++;
         }
     }
     if ($testsFound == 0) {
         $this->addTest(new PHPUnit2_Framework_Warning('No tests found in file ' . $filename));
     }
 }
 /**
  * @param  string  $suiteClassName
  * @param  string  $suiteClassFile
  * @return ReflectionClass
  * @throws Exception
  * @access public
  */
 public function load($suiteClassName, $suiteClassFile = '')
 {
     $suiteClassName = str_replace('.php', '', $suiteClassName);
     $suiteClassFile = !empty($suiteClassFile) ? $suiteClassFile : str_replace('_', '/', $suiteClassName) . '.php';
     if (!class_exists($suiteClassName)) {
         if (!file_exists($suiteClassFile)) {
             $config = new PEAR_Config();
             $includePaths = explode(PATH_SEPARATOR, get_include_path());
             $includePaths[] = $config->get('test_dir');
             foreach ($includePaths as $includePath) {
                 $file = $includePath . DIRECTORY_SEPARATOR . $suiteClassFile;
                 if (file_exists($file)) {
                     $suiteClassFile = $file;
                     break;
                 }
             }
         }
         PHPUnit2_Util_Fileloader::checkAndLoad($suiteClassFile);
     }
     if (class_exists($suiteClassName)) {
         return new ReflectionClass($suiteClassName);
     } else {
         throw new Exception(sprintf('Class %s could not be found in %s.', $suiteClassName, $suiteClassFile));
     }
 }
Example #3
0
 /**
  * @param  array $arguments
  * @throws Exception
  * @access protected
  */
 protected function start($arguments)
 {
     $coverageDataFile = FALSE;
     $coverageHTMLFile = FALSE;
     $coverageTextFile = FALSE;
     $testdoxHTMLFile = FALSE;
     $testdoxTextFile = FALSE;
     $xmlLogfile = FALSE;
     $wait = FALSE;
     $possibleOptions = array('help', 'loader=', 'log-xml=', 'skeleton', 'testdox-html=', 'testdox-text=', 'version', 'wait');
     if (extension_loaded('xdebug')) {
         $possibleOptions[] = 'coverage-data=';
         $possibleOptions[] = 'coverage-html=';
         $possibleOptions[] = 'coverage-text=';
     }
     $options = Console_Getopt::getopt($arguments, '', $possibleOptions);
     if (PEAR::isError($options)) {
         $this->showError($options->getMessage());
     }
     $test = isset($options[1][0]) ? $options[1][0] : FALSE;
     $testFile = isset($options[1][1]) ? $options[1][1] : $test . '.php';
     foreach ($options[0] as $option) {
         switch ($option[0]) {
             case '--coverage-data':
                 $coverageDataFile = $option[1];
                 break;
             case '--coverage-html':
                 $coverageHTMLFile = $option[1];
                 break;
             case '--coverage-text':
                 $coverageTextFile = $option[1];
                 break;
             case '--help':
                 $this->showHelp();
                 exit(self::SUCCESS_EXIT);
                 break;
             case '--testdox-html':
                 $testdoxHTMLFile = $option[1];
                 break;
             case '--testdox-text':
                 $testdoxTextFile = $option[1];
                 break;
             case '--loader':
                 if (!class_exists($option[1])) {
                     PHPUnit2_Util_Fileloader::checkAndLoad(str_replace('_', '/', $option[1]) . '.php');
                 }
                 if (class_exists($option[1])) {
                     $class = new ReflectionClass($option[1]);
                     if ($class->implementsInterface('PHPUnit2_Runner_TestSuiteLoader') && $class->isInstantiable()) {
                         $this->loader = $class->newInstance();
                     }
                 }
                 if ($this->loader === NULL) {
                     $this->showError(sprintf('Could not use "%s" as loader.', $option[1]));
                 }
                 break;
             case '--log-xml':
                 $xmlLogfile = $option[1];
                 break;
             case '--skeleton':
                 if ($test !== FALSE) {
                     self::printVersionString();
                     try {
                         require_once 'PHPUnit2/Util/Skeleton.php';
                         $skeleton = new PHPUnit2_Util_Skeleton($test, $testFile);
                         $skeleton->write();
                     } catch (Exception $e) {
                         print $e->getMessage() . "\n";
                         printf("Could not write test class skeleton for %s to %s.\n", $test, $test . 'Test.php');
                         exit(self::FAILURE_EXIT);
                     }
                     printf("Wrote test class skeleton for %s to %s.\n", $test, $test . 'Test.php');
                     exit(self::SUCCESS_EXIT);
                 }
                 break;
             case '--version':
                 self::printVersionString();
                 exit(self::SUCCESS_EXIT);
                 break;
             case '--wait':
                 $wait = TRUE;
                 break;
         }
     }
     if ($test === FALSE) {
         $this->showHelp();
         exit(self::SUCCESS_EXIT);
     }
     try {
         return $this->doRun($this->getTest($test, $testFile), $coverageDataFile, $coverageHTMLFile, $coverageTextFile, $testdoxHTMLFile, $testdoxTextFile, $xmlLogfile, $wait);
     } catch (Exception $e) {
         throw new Exception('Could not create and run test suite: ' . $e->getMessage());
     }
 }