You are here

public static function PHPUnit_Framework_TestSuite::createTest in Zircon Profile 8.0

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

Parameters

ReflectionClass $theClass:

string $name:

Return value

PHPUnit_Framework_Test

Throws

PHPUnit_Framework_Exception

1 call to PHPUnit_Framework_TestSuite::createTest()
PHPUnit_Framework_TestSuite::addTestMethod in vendor/phpunit/phpunit/src/Framework/TestSuite.php

File

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

Class

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

Code

public static function createTest(ReflectionClass $theClass, $name) {
  $className = $theClass
    ->getName();
  if (!$theClass
    ->isInstantiable()) {
    return self::warning(sprintf('Cannot instantiate class "%s".', $className));
  }
  $backupSettings = PHPUnit_Util_Test::getBackupSettings($className, $name);
  $preserveGlobalState = PHPUnit_Util_Test::getPreserveGlobalStateSettings($className, $name);
  $runTestInSeparateProcess = PHPUnit_Util_Test::getProcessIsolationSettings($className, $name);
  $constructor = $theClass
    ->getConstructor();
  if ($constructor !== null) {
    $parameters = $constructor
      ->getParameters();

    // TestCase() or TestCase($name)
    if (count($parameters) < 2) {
      $test = new $className();
    }
    else {
      try {
        $data = PHPUnit_Util_Test::getProvidedData($className, $name);
      } catch (PHPUnit_Framework_IncompleteTestError $e) {
        $message = sprintf('Test for %s::%s marked incomplete by data provider', $className, $name);
        $_message = $e
          ->getMessage();
        if (!empty($_message)) {
          $message .= "\n" . $_message;
        }
        $data = self::incompleteTest($className, $name, $message);
      } catch (PHPUnit_Framework_SkippedTestError $e) {
        $message = sprintf('Test for %s::%s skipped by data provider', $className, $name);
        $_message = $e
          ->getMessage();
        if (!empty($_message)) {
          $message .= "\n" . $_message;
        }
        $data = self::skipTest($className, $name, $message);
      } catch (Throwable $_t) {
        $t = $_t;
      } catch (Exception $_t) {
        $t = $_t;
      }
      if (isset($t)) {
        $message = sprintf('The data provider specified for %s::%s is invalid.', $className, $name);
        $_message = $t
          ->getMessage();
        if (!empty($_message)) {
          $message .= "\n" . $_message;
        }
        $data = self::warning($message);
      }

      // Test method with @dataProvider.
      if (isset($data)) {
        $test = new PHPUnit_Framework_TestSuite_DataProvider($className . '::' . $name);
        if (empty($data)) {
          $data = self::warning(sprintf('No tests found in suite "%s".', $test
            ->getName()));
        }
        $groups = PHPUnit_Util_Test::getGroups($className, $name);
        if ($data instanceof PHPUnit_Framework_Warning || $data instanceof PHPUnit_Framework_SkippedTestCase || $data instanceof PHPUnit_Framework_IncompleteTestCase) {
          $test
            ->addTest($data, $groups);
        }
        else {
          foreach ($data as $_dataName => $_data) {
            $_test = new $className($name, $_data, $_dataName);
            if ($runTestInSeparateProcess) {
              $_test
                ->setRunTestInSeparateProcess(true);
              if ($preserveGlobalState !== null) {
                $_test
                  ->setPreserveGlobalState($preserveGlobalState);
              }
            }
            if ($backupSettings['backupGlobals'] !== null) {
              $_test
                ->setBackupGlobals($backupSettings['backupGlobals']);
            }
            if ($backupSettings['backupStaticAttributes'] !== null) {
              $_test
                ->setBackupStaticAttributes($backupSettings['backupStaticAttributes']);
            }
            $test
              ->addTest($_test, $groups);
          }
        }
      }
      else {
        $test = new $className();
      }
    }
  }
  if (!isset($test)) {
    throw new PHPUnit_Framework_Exception('No valid test provided.');
  }
  if ($test instanceof PHPUnit_Framework_TestCase) {
    $test
      ->setName($name);
    if ($runTestInSeparateProcess) {
      $test
        ->setRunTestInSeparateProcess(true);
      if ($preserveGlobalState !== null) {
        $test
          ->setPreserveGlobalState($preserveGlobalState);
      }
    }
    if ($backupSettings['backupGlobals'] !== null) {
      $test
        ->setBackupGlobals($backupSettings['backupGlobals']);
    }
    if ($backupSettings['backupStaticAttributes'] !== null) {
      $test
        ->setBackupStaticAttributes($backupSettings['backupStaticAttributes']);
    }
  }
  return $test;
}