You are here

public function PHPUnit_Framework_TestSuite::addTestFile in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/phpunit/phpunit/src/Framework/TestSuite.php \PHPUnit_Framework_TestSuite::addTestFile()

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>PHPUnit_Framework_Warning</code> will be created instead, leaving the current test run untouched.

@since Method available since Release 2.3.0

Parameters

string $filename:

Throws

PHPUnit_Framework_Exception

2 calls to PHPUnit_Framework_TestSuite::addTestFile()
PHPUnit_Extensions_PhptTestSuite::__construct in vendor/phpunit/phpunit/src/Extensions/PhptTestSuite.php
Constructs a new TestSuite for .phpt test cases.
PHPUnit_Framework_TestSuite::addTestFiles in vendor/phpunit/phpunit/src/Framework/TestSuite.php
Wrapper for addTestFile() that adds multiple test files.

File

vendor/phpunit/phpunit/src/Framework/TestSuite.php, line 314

Class

PHPUnit_Framework_TestSuite
A TestSuite is a composite of Tests. It runs a collection of test cases.

Code

public function addTestFile($filename) {
  if (!is_string($filename)) {
    throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  }
  if (file_exists($filename) && substr($filename, -5) == '.phpt') {
    $this
      ->addTest(new PHPUnit_Extensions_PhptTestCase($filename));
    return;
  }

  // The given file may contain further stub classes in addition to the
  // test class itself. Figure out the actual test class.
  $classes = get_declared_classes();
  $filename = PHPUnit_Util_Fileloader::checkAndLoad($filename);
  $newClasses = array_diff(get_declared_classes(), $classes);

  // The diff is empty in case a parent class (with test methods) is added
  // AFTER a child class that inherited from it. To account for that case,
  // cumulate all discovered classes, so the parent class may be found in
  // a later invocation.
  if ($newClasses) {

    // On the assumption that test classes are defined first in files,
    // process discovered classes in approximate LIFO order, so as to
    // avoid unnecessary reflection.
    $this->foundClasses = array_merge($newClasses, $this->foundClasses);
  }

  // The test class's name must match the filename, either in full, or as
  // a PEAR/PSR-0 prefixed shortname ('NameSpace_ShortName'), or as a
  // PSR-1 local shortname ('NameSpace\ShortName'). The comparison must be
  // anchored to prevent false-positive matches (e.g., 'OtherShortName').
  $shortname = basename($filename, '.php');
  $shortnameRegEx = '/(?:^|_|\\\\)' . preg_quote($shortname, '/') . '$/';
  foreach ($this->foundClasses as $i => $className) {
    if (preg_match($shortnameRegEx, $className)) {
      $class = new ReflectionClass($className);
      if ($class
        ->getFileName() == $filename) {
        $newClasses = array(
          $className,
        );
        unset($this->foundClasses[$i]);
        break;
      }
    }
  }
  foreach ($newClasses as $className) {
    $class = new ReflectionClass($className);
    if (!$class
      ->isAbstract()) {
      if ($class
        ->hasMethod(PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME)) {
        $method = $class
          ->getMethod(PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME);
        if ($method
          ->isStatic()) {
          $this
            ->addTest($method
            ->invoke(null, $className));
        }
      }
      elseif ($class
        ->implementsInterface('PHPUnit_Framework_Test')) {
        $this
          ->addTestSuite($class);
      }
    }
  }
  $this->numTests = -1;
}