createTest() public static method

public static createTest ( ReflectionClass $theClass, string $name ) : PHPUnit_Framework_Test
$theClass ReflectionClass
$name string
return PHPUnit_Framework_Test
Example #1
0
 protected function createTestFromPhpUnitMethod(\ReflectionClass $class, \ReflectionMethod $method)
 {
     if (!\PHPUnit_Framework_TestSuite::isTestMethod($method)) {
         return;
     }
     $test = \PHPUnit_Framework_TestSuite::createTest($class, $method->name);
     if ($test instanceof \PHPUnit_Framework_TestSuite_DataProvider) {
         foreach ($test->tests() as $t) {
             $this->enhancePhpunitTest($t);
         }
         return $test;
     }
     $this->enhancePhpunitTest($test);
     return $test;
 }
Example #2
0
 protected function createTestFromPhpUnitMethod(\ReflectionClass $class, \ReflectionMethod $method)
 {
     if (!\PHPUnit_Framework_TestSuite::isTestMethod($method) and strpos($method->name, 'should') !== 0) {
         return;
     }
     $test = \PHPUnit_Framework_TestSuite::createTest($class, $method->name);
     if ($test instanceof TestCase\Test) {
         $test->setBootstrap($this->settings['bootstrap']);
         $test->setDispatcher($this->dispatcher);
         $test->setGuyClass($this->settings['class_name']);
         $groups = \PHPUnit_Util_Test::getGroups($class->name, $method->name);
         $test->getScenario()->groups($groups);
     } else {
         if ($this->settings['bootstrap']) {
             require_once $this->settings['bootstrap'];
         }
     }
     return $test;
 }
 public static function main()
 {
     global $isLocal;
     // check number of arguments
     $isLocal = !isset($_SERVER['HTTP_USER_AGENT']);
     $arguments = array('listGroups' => FALSE, 'loader' => NULL, 'useDefaultConfiguration' => TRUE);
     $loader = NULL;
     $startPos = 1;
     $canCountTest = true;
     global $config_file;
     if ($isLocal && $_SERVER['argv'][1] == "-config" || !$isLocal && strcmp($config_file, "") != 0 && strcmp($config_file, "/*config_xml*/") != 0) {
         // check if configuration specified
         $canCountTest = false;
         $path = $isLocal ? $_SERVER['argv'][2] : $config_file;
         //$_GET["config_xml"];
         $arguments['configuration'] = $path;
         $startPos = 3;
         $configuration = PHPUnit_Util_Configuration::getInstance($path);
         $phpunit = $configuration->getPHPUnitConfiguration();
         if (isset($phpunit['testSuiteLoaderClass'])) {
             if (isset($phpunit['testSuiteLoaderFile'])) {
                 $file = $phpunit['testSuiteLoaderFile'];
             } else {
                 $file = '';
             }
             $command = new PHPUnit_TextUI_Command();
             $loader = $command->handleLoader($phpunit['testSuiteLoaderClass'], $file);
             $arguments['loader'] = $loader;
         }
         $configuration->handlePHPConfiguration();
         $phpunitConfiguration = $configuration->getPHPUnitConfiguration();
         if (isset($phpunitConfiguration['bootstrap'])) {
             PHPUnit_Util_Fileloader::load($phpunitConfiguration['bootstrap']);
         }
         $browsers = $configuration->getSeleniumBrowserConfiguration();
         if (!empty($browsers)) {
             require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
             PHPUnit_Extensions_SeleniumTestCase::$browsers = $browsers;
         }
     }
     if ($isLocal && $_SERVER['argv'][$startPos] == "-group" || !$isLocal && isset($_GET["groups"])) {
         $arguments['groups'] = explode(',', $isLocal ? $_SERVER['argv'][$startPos + 1] : $_GET["groups"]);
         $startPos += 2;
     }
     if ($isLocal && $_SERVER['argv'][$startPos] == "-exclude-group" || !$isLocal && isset($_GET["exclude_groups"])) {
         $arguments['excludeGroups'] = explode(',', $isLocal ? $_SERVER['argv'][$startPos + 1] : $_GET["exclude_groups"]);
         $startPos += 2;
     }
     $check = $isLocal ? $_SERVER['argv'][$startPos] : $_GET["mode"];
     if ($check == "c") {
         $suiteClassName = $isLocal ? $_SERVER['argv'][$startPos + 1] : $_GET["class"];
         $suiteClassFile = $isLocal ? $_SERVER['argv'][$startPos + 2] : $_GET["file"];
         try {
             // $testClass = ();
             if ($loader == NULL) {
                 $loader = new PHPUnit_Runner_StandardTestSuiteLoader();
             }
             $testClass = $loader->load($suiteClassName, $suiteClassFile, FALSE);
         } catch (Exception $e) {
             myExceptionHandler($e);
             return;
         }
         try {
             // if class is a suite
             $suiteMethod = $testClass->getMethod('suite');
             if ($suiteMethod->isAbstract() || !$suiteMethod->isPublic() || !$suiteMethod->isStatic()) {
                 return;
             }
             try {
                 // ?? suite does not have testName argument
                 $test = $suiteMethod->invoke(NULL, $testClass->getName());
                 $test->setName($suiteClassName);
                 if ($canCountTest) {
                     print traceCommand("testCount", "count", (string) sizeof($test));
                 }
                 self::runTest($test, $suiteClassFile, $arguments);
             } catch (ReflectionException $e) {
                 myExceptionHandler($e);
                 return;
             }
         } catch (ReflectionException $e) {
             $test = new PHPUnit_Framework_TestSuite($testClass);
             if ($canCountTest) {
                 print traceCommand("testCount", "count", (string) sizeof($test));
             }
             self::runTest($test, $suiteClassFile, $arguments);
         }
     } else {
         if ($check == "d") {
             // if run directory
             // in remote case we put this script in the test directory
             $suiteDirName = $isLocal ? $_SERVER['argv'][$startPos + 1] : dirname(__FILE__);
             if (is_dir($suiteDirName) && !is_file($suiteDirName . '.php')) {
                 $testCollector = new PHPUnit_Runner_IncludePathTestCollector(array($suiteDirName));
                 // $test = new PHPUnit_Framework_TestSuite($suiteDirName);
                 $filenames = $testCollector->collectTests();
                 $number = 0;
                 $alltests = array();
                 foreach ($filenames as $filename) {
                     $tests = self::collectTestsFromFile($filename);
                     foreach ($tests as $currenttest) {
                         $number += sizeof($currenttest);
                         $alltests[] = $currenttest;
                         $alltests[] = $filename;
                     }
                 }
                 if ($canCountTest) {
                     print traceCommand("testCount", "count", (string) $number);
                 }
                 for ($i = 0; $i < count($alltests); $i += 2) {
                     self::runTest($alltests[$i], $alltests[$i + 1], $arguments);
                 }
                 return;
             }
         } else {
             if ($check == 'f') {
                 // if run all in file
                 $filename = $isLocal ? $_SERVER['argv'][$startPos + 1] : $_GET["file"];
                 $tests = self::collectTestsFromFile($filename);
                 $test = new PHPUnit_Framework_TestSuite();
                 $number = 0;
                 foreach ($tests as $currenttest) {
                     if ($tests) {
                         $test->addTest($currenttest);
                         $number += sizeof($currenttest);
                     }
                 }
                 if ($canCountTest) {
                     print traceCommand("testCount", "count", $number);
                 }
                 foreach ($tests as $currentTest) {
                     self::runTest($currentTest, $filename, $arguments);
                 }
                 return;
             } else {
                 if ($check == 'm') {
                     $suiteMethodName = $isLocal ? $_SERVER['argv'][$startPos + 1] : $_GET["method"];
                     $suiteClassName = $isLocal ? $_SERVER['argv'][$startPos + 2] : $_GET["class"];
                     $suiteClassFile = $isLocal ? $_SERVER['argv'][$startPos + 3] : $_GET["file"];
                     try {
                         $testClass = new PHPUnit_Runner_StandardTestSuiteLoader();
                         $testClass = $testClass->load($suiteClassName, $suiteClassFile, FALSE);
                     } catch (Exception $e) {
                         myExceptionHandler($e);
                         return;
                     }
                     try {
                         // if class is a suite
                         $suiteMethod = $testClass->getMethod($suiteMethodName);
                         if ($suiteMethodName == 'suite') {
                             if ($suiteMethod->isAbstract() || !$suiteMethod->isPublic() || !$suiteMethod->isStatic()) {
                                 return;
                             }
                             try {
                                 $test = $suiteMethod->invoke(NULL, $testClass->getName());
                                 $test->setName($suiteClassName);
                                 if ($canCountTest) {
                                     print traceCommand("testCount", "count", (string) sizeof($test));
                                 }
                                 self::runTest($test, $suiteClassFile, $arguments);
                             } catch (ReflectionException $e) {
                                 myExceptionHandler($e);
                                 return;
                             }
                         } else {
                             $test = PHPUnit_Framework_TestSuite::createTest($testClass, $suiteMethodName);
                             $testSuite = new PHPUnit_Framework_TestSuite();
                             $testSuite->addTest($test);
                             $testSuite->setName($suiteClassName);
                             if ($canCountTest) {
                                 print traceCommand("testCount", "count", (string) sizeof($test));
                             }
                             self::runTest($testSuite, $suiteClassFile, $arguments);
                         }
                     } catch (ReflectionException $e) {
                         myExceptionHandler($e);
                         return;
                     }
                 } else {
                     if ($check == 'x') {
                         $testSuite = $configuration->getTestSuiteConfiguration();
                         self::runTest($testSuite, "", $arguments);
                     }
                 }
             }
         }
     }
 }
 /**
  * @param  string $className
  * @return PHPUnit_Framework_TestSuite
  */
 public static function suite($className)
 {
     $suite = new PHPUnit_Extensions_SeleniumTestSuite();
     $suite->setName($className);
     $class = new ReflectionClass($className);
     $classGroups = PHPUnit_Util_Test::getGroups($className);
     $staticProperties = $class->getStaticProperties();
     // Create tests from Selenese/HTML files.
     if (isset($staticProperties['seleneseDirectory']) && is_dir($staticProperties['seleneseDirectory'])) {
         $files = array_merge(self::getSeleneseFiles($staticProperties['seleneseDirectory'], '.htm'), self::getSeleneseFiles($staticProperties['seleneseDirectory'], '.html'));
         // Create tests from Selenese/HTML files for multiple browsers.
         if (!empty($staticProperties['browsers'])) {
             foreach ($staticProperties['browsers'] as $browser) {
                 $browserSuite = new PHPUnit_Framework_TestSuite();
                 $browserSuite->setName($className . ': ' . $browser['name']);
                 foreach ($files as $file) {
                     self::addConfiguredTestTo($browserSuite, new $className($file, array(), '', $browser), $classGroups);
                 }
                 $suite->addTest($browserSuite);
             }
         } else {
             foreach ($files as $file) {
                 self::addConfiguredTestTo($suite, new $className($file), $classGroups);
             }
         }
     }
     // Create tests from test methods for multiple browsers.
     if (!empty($staticProperties['browsers'])) {
         foreach ($staticProperties['browsers'] as $browser) {
             $browserSuite = new PHPUnit_Framework_TestSuite();
             $browserSuite->setName($className . ': ' . $browser['name']);
             foreach ($class->getMethods() as $method) {
                 if (PHPUnit_Framework_TestSuite::isPublicTestMethod($method)) {
                     $name = $method->getName();
                     $test = PHPUnit_Framework_TestSuite::createTest($class, $name);
                     if ($test instanceof PHPUnit_Framework_TestCase) {
                         $test->setupSpecificBrowser($browser);
                         $groups = PHPUnit_Util_Test::getGroups($className, $name);
                         self::addConfiguredTestTo($browserSuite, $test, $groups);
                     } else {
                         $browserSuite->addTest($test);
                     }
                 }
             }
             $suite->addTest($browserSuite);
         }
     } else {
         foreach ($class->getMethods() as $method) {
             if (PHPUnit_Framework_TestSuite::isPublicTestMethod($method)) {
                 $name = $method->getName();
                 $test = PHPUnit_Framework_TestSuite::createTest($class, $name);
                 if ($test instanceof PHPUnit_Framework_TestCase) {
                     $groups = PHPUnit_Util_Test::getGroups($className, $name);
                     self::addConfiguredTestTo($suite, $test, $groups);
                 } else {
                     $suite->addTest($test);
                 }
             }
         }
     }
     return $suite;
 }
Example #5
0
 /**
  * Creates test from PHPUnit method.
  *
  * @since 1.0.0
  *
  * @access protected
  * @param \ReflectionClass $class The class object.
  * @param \ReflectionMethod $method The method object.
  * @return \PHPUnit_Framework_Test The test object.
  */
 protected function createTestFromPhpUnitMethod(\ReflectionClass $class, \ReflectionMethod $method)
 {
     if (!\PHPUnit_Framework_TestSuite::isTestMethod($method)) {
         return;
     }
     $test = \PHPUnit_Framework_TestSuite::createTest($class, $method->name);
     if ($test instanceof \PHPUnit_Framework_TestSuite_DataProvider) {
         foreach ($test->tests() as $t) {
             $this->enhancePhpunitTest($t);
         }
     } else {
         $this->enhancePhpunitTest($test);
     }
     $test->setBackupGlobals(false);
     $test->setBackupStaticAttributes(false);
     $test->setRunTestInSeparateProcess(false);
     $test->setInIsolation(false);
     $test->setPreserveGlobalState(false);
     return $test;
 }