/** * @param ReflectionClass $theClass * @param string $name * @param array $classGroups * @return PHPUnit_Framework_Test */ public static function createTest(ReflectionClass $theClass, $name, array $classGroups = array()) { $className = $theClass->getName(); if (!$theClass->isInstantiable()) { return self::warning(sprintf('Cannot instantiate class "%s".', $className)); } $classDocComment = $theClass->getDocComment(); $method = new ReflectionMethod($className, $name); $methodDocComment = $method->getDocComment(); $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 { $data = PHPUnit_Util_Test::getProvidedData($className, $name); $groups = PHPUnit_Util_Test::getGroups($className, $name); if (is_array($data) || $data instanceof Iterator) { $test = new PHPUnit_Framework_TestSuite_DataProvider($className . '::' . $name); 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 ($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; }
/** * @param ReflectionClass $theClass * @param string $name * @return PHPUnit_Framework_Test * @throws PHPUnit_Framework_Exception */ 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; }
/** * Custom implementation of native PHPUnit_Framework_TestSuite::createTest method * - Test Case class should be instantiated with MTF Object Manager * - Data Provider Test Suite should be instantiated with MTF Object Manager * * @param \ReflectionClass $theClass * @param string $name * @param array $arguments * @return \PHPUnit_Framework_Test * @throws \PHPUnit_Framework_Exception */ public static function createTest(\ReflectionClass $theClass, $name, array $arguments = []) { $objectManager = \Mtf\ObjectManager::getInstance(); $class = $theClass->getName(); $arguments['name'] = $name; $backupSettings = \PHPUnit_Util_Test::getBackupSettings($class, $name); $preserveGlobalState = \PHPUnit_Util_Test::getPreserveGlobalStateSettings($class, $name); $runTestInSeparateProcess = \PHPUnit_Util_Test::getProcessIsolationSettings($class, $name); try { $data = \PHPUnit_Util_Test::getProvidedData($class, $name); } catch (\Exception $e) { /** @var EventManager $eventManager */ $eventManager = $objectManager->get('Mtf\\System\\Event\\Event'); $eventManager->dispatchEvent(['exception'], [$e]); $message = sprintf('The data provider specified for %s::%s is invalid.', $class, $name); $_message = $e->getMessage(); if (!empty($_message)) { $message .= "\n" . $_message; } $data = new \PHPUnit_Framework_Warning($message); } // Test method with @dataProvider. if (isset($data)) { $test = $objectManager->create('Mtf\\TestSuite\\InjectableDataProvider', ['class' => $class . '::' . $name]); if (empty($data)) { $data = new \PHPUnit_Framework_Warning(sprintf('No tests found in suite "%s".', $test->getName())); } $groups = \PHPUnit_Util_Test::getGroups($class, $name); if ($data instanceof \PHPUnit_Framework_Warning) { $test->addTest($data, $groups); } else { foreach ($data as $_dataName => $_data) { $_arguments = $arguments; $_arguments['data'] = $_data; $_arguments['dataName'] = $_dataName; $_test = $objectManager->create($class, $_arguments); 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 = $objectManager->create($class, $arguments); } 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; }