/** * @param string $suiteClassName * @param string $suiteClassFile * @return ReflectionClass * @throws RuntimeException */ public function load($suiteClassName, $suiteClassFile = '') { $suiteClassName = str_replace('.php', '', $suiteClassName); if (empty($suiteClassFile)) { $suiteClassFile = PHPUnit_Util_Filesystem::classNameToFilename($suiteClassName); } if (!class_exists($suiteClassName, FALSE)) { PHPUnit_Util_Class::collectStart(); $filename = PHPUnit_Util_Fileloader::checkAndLoad($suiteClassFile); $loadedClasses = PHPUnit_Util_Class::collectEnd(); } if (!class_exists($suiteClassName, FALSE) && !empty($loadedClasses)) { $offset = 0 - strlen($suiteClassName); foreach ($loadedClasses as $loadedClass) { $class = new ReflectionClass($loadedClass); if (substr($loadedClass, $offset) === $suiteClassName && $class->getFileName() == $filename) { $suiteClassName = $loadedClass; break; } } } if (!class_exists($suiteClassName, FALSE) && !empty($loadedClasses)) { $testCaseClass = 'PHPUnit_Framework_TestCase'; foreach ($loadedClasses as $loadedClass) { $class = new ReflectionClass($loadedClass); $classFile = $class->getFileName(); if ($class->isSubclassOf($testCaseClass) && !$class->isAbstract()) { $suiteClassName = $loadedClass; $testCaseClass = $loadedClass; if ($classFile == realpath($suiteClassFile)) { break; } } if ($class->hasMethod('suite')) { $method = $class->getMethod('suite'); if (!$method->isAbstract() && $method->isPublic() && $method->isStatic()) { $suiteClassName = $loadedClass; if ($classFile == realpath($suiteClassFile)) { break; } } } } } if (class_exists($suiteClassName, FALSE)) { $class = new ReflectionClass($suiteClassName); $filePath = $GLOBALS['base_dir'] . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'phpunit' . DIRECTORY_SEPARATOR . $suiteClassFile; if ($class->getFileName() == realpath($filePath)) { return $class; } } throw new PHPUnit_Framework_Exception(sprintf('Class %s could not be found in %s.', $suiteClassName, $suiteClassFile)); }
/** * 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. * * @param string $filename * @param boolean $syntaxCheck * @param array $phptOptions Array with ini settings for the php instance * run, key being the name if the setting, * value the ini value. * @throws InvalidArgumentException * @since Method available since Release 2.3.0 * @author Stefano F. Rausch <*****@*****.**> */ public function addTestFile($filename, $syntaxCheck = FALSE, $phptOptions = array()) { 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, $phptOptions)); return; } PHPUnit_Util_Class::collectStart(); $filename = PHPUnit_Util_Fileloader::checkAndLoad($filename, $syntaxCheck); $newClasses = PHPUnit_Util_Class::collectEnd(); $baseName = str_replace('.php', '', basename($filename)); foreach ($newClasses as $className) { if (substr($className, 0 - strlen($baseName)) == $baseName) { $class = new ReflectionClass($className); if ($class->getFileName() == $filename) { $newClasses = array($className); break; } } } $testsFound = FALSE; 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)); $testsFound = TRUE; } } else { if ($class->implementsInterface('PHPUnit_Framework_Test')) { $this->addTestSuite($class); $testsFound = TRUE; } } } } $this->numTests = -1; }
/** * 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. * * @param string $filename * @param array $phptOptions Array with ini settings for the php instance * run, key being the name if the setting, * value the ini value. * @throws InvalidArgumentException * @since Method available since Release 2.3.0 * @author Stefano F. Rausch <*****@*****.**> */ public function addTestFile($filename, $phptOptions = array()) { if (!is_string($filename)) { throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); } if (file_exists($filename) && substr($filename, -5) == '.phpt') { require_once 'PHPUnit/Extensions/PhptTestCase.php'; $this->addTest(new PHPUnit_Extensions_PhptTestCase($filename, $phptOptions)); return; } if (!file_exists($filename)) { $includePaths = explode(PATH_SEPARATOR, get_include_path()); foreach ($includePaths as $includePath) { $file = $includePath . DIRECTORY_SEPARATOR . $filename; if (file_exists($file)) { $filename = $file; break; } } } PHPUnit_Util_Class::collectStart(); PHPUnit_Util_Fileloader::load($filename); $newClasses = PHPUnit_Util_Class::collectEnd(); $baseName = str_replace('.php', '', basename($filename)); foreach ($newClasses as $className) { if (substr($className, 0 - strlen($baseName)) == $baseName) { $newClasses = array($className); break; } } $testsFound = FALSE; 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)); $testsFound = TRUE; } } else { if ($class->implementsInterface('PHPUnit_Framework_Test')) { $this->addTestSuite($class); $testsFound = TRUE; } } } } $this->numTests = -1; }
/** * 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. * * @param string $filename * @param boolean $syntaxCheck * @param array $phptOptions Array with ini settings for the php instance * run, key being the name if the setting, * value the ini value. * @throws InvalidArgumentException * @access public * @since Method available since Release 2.3.0 * @author Stefano F. Rausch <*****@*****.**> */ public function addTestFile($filename, $syntaxCheck = TRUE, $phptOptions = array()) { if (!is_string($filename)) { throw new InvalidArgumentException(); } if (file_exists($filename) && substr($filename, -5) == '.phpt') { $this->addTest(new PHPUnit_Extensions_PhptTestCase($filename, $phptOptions)); return; } if (!file_exists($filename)) { $includePaths = explode(PATH_SEPARATOR, get_include_path()); foreach ($includePaths as $includePath) { $file = $includePath . DIRECTORY_SEPARATOR . $filename; if (file_exists($file)) { $filename = $file; break; } } } PHPUnit_Util_Class::collectStart(); PHPUnit_Util_Fileloader::checkAndLoad($filename, $syntaxCheck); $newClasses = PHPUnit_Util_Class::collectEnd(); $testsFound = FALSE; 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)); $testsFound = TRUE; } } else { if ($class->implementsInterface('PHPUnit_Framework_Test')) { $this->addTestSuite($class); $testsFound = TRUE; } } } } if (!$testsFound) { $this->addTest(new PHPUnit_Framework_Warning('No tests found in file "' . $filename . '".')); } $this->numTests = -1; }
protected function handleCustomTestSuite() { $directory = getcwd(); $packages = $this->options[1]; if (isset($this->arguments['coverageClover']) || isset($this->arguments['reportDirectory'])) { $fillWhitelist = true; } else { $fillWhitelist = false; } $this->arguments['test'] = new PHPUnit_Framework_TestSuite(); $this->arguments['test']->setName('eZ Components'); if (empty($packages)) { $packages = $this->getPackages($directory); } foreach ($packages as $package) { $added = false; $slashCount = substr_count($package, DIRECTORY_SEPARATOR); if ($this->release == 'trunk' && $slashCount !== 0 || $this->release == 'stable' && $slashCount > 1) { if (file_exists($package)) { PHPUnit_Util_Class::collectStart(); require_once $package; $class = PHPUnit_Util_Class::collectEnd(); if (!empty($class)) { $this->arguments['test']->addTest(call_user_func(array(array_pop($class), 'suite'))); $added = true; $package = substr($package, 0, strpos($package, DIRECTORY_SEPARATOR)); } else { die("\n Cannot load: {$package}. \n"); } } } else { $suite = $this->getTestSuite($directory, $package); if (!is_null($suite)) { $this->arguments['test']->addTest($suite); $added = true; } } if ($fillWhitelist && $added) { foreach (glob($directory . '/' . $package . '/src/*_autoload.php') as $autoloadFile) { foreach (include $autoloadFile as $className => $fileName) { if (strpos($fileName, 'xmlwritersubstitute.php') === false) { PHPUnit_Util_Filter::addFileToWhitelist($directory . '/' . str_replace($package, $package . '/src', $fileName)); } } } } } if (isset($this->arguments['colors']) && $this->arguments['colors'] === true) { $colors = true; } else { $colors = false; } if (isset($this->arguments['debug']) && $this->arguments['debug'] === true) { $debug = true; } else { $debug = false; } if (isset($this->arguments['verbose']) && $this->arguments['verbose'] === true) { $verbose = true; } else { $verbose = false; } $this->arguments['printer'] = new ezcTestPrinter(NULL, $verbose, $colors, $debug); }
/** * @param string $suiteClassName * @param string $suiteClassFile * @param boolean $syntaxCheck * @return ReflectionClass * @throws RuntimeException */ public function load($suiteClassName, $suiteClassFile = '', $syntaxCheck = TRUE) { $suiteClassName = str_replace('.php', '', $suiteClassName); if (empty($suiteClassFile)) { $suiteClassFile = str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $suiteClassName) . '.php'; } if (!class_exists($suiteClassName, FALSE)) { if (!file_exists($suiteClassFile)) { $includePaths = explode(PATH_SEPARATOR, get_include_path()); foreach ($includePaths as $includePath) { $file = $includePath . DIRECTORY_SEPARATOR . $suiteClassFile; if (file_exists($file)) { $suiteClassFile = $file; break; } } } PHPUnit_Util_Class::collectStart(); PHPUnit_Util_Fileloader::checkAndLoad($suiteClassFile, $syntaxCheck); $loadedClasses = PHPUnit_Util_Class::collectEnd(); } if (!class_exists($suiteClassName, FALSE) && !empty($loadedClasses)) { $offset = 0 - strlen($suiteClassName); foreach ($loadedClasses as $loadedClass) { if (substr($loadedClass, $offset) === $suiteClassName) { $suiteClassName = $loadedClass; break; } } } if (!class_exists($suiteClassName, FALSE) && !empty($loadedClasses)) { $testCaseClass = 'PHPUnit_Framework_TestCase'; foreach ($loadedClasses as $loadedClass) { $class = new ReflectionClass($loadedClass); if ($class->isSubclassOf($testCaseClass)) { $suiteClassName = $loadedClass; $testCaseClass = $loadedClass; if ($class->getFileName() == realpath($suiteClassFile)) { break; } } if ($class->hasMethod('suite')) { $method = $class->getMethod('suite'); if (!$method->isAbstract() && $method->isPublic() && $method->isStatic()) { $suiteClassName = $loadedClass; break; } } } } if (class_exists($suiteClassName, FALSE)) { $class = new ReflectionClass($suiteClassName); if ($class->getFileName() == realpath($suiteClassFile)) { return $class; } } throw new RuntimeException(sprintf('Class %s could not be found in %s.', $suiteClassName, $suiteClassFile)); }
public static function collectTestsFromFile($filename) { PHPUnit_Util_Class::collectStart(); PHPUnit_Util_Fileloader::checkAndLoad($filename, FALSE); $newClasses = PHPUnit_Util_Class::collectEnd(); $baseName = str_replace('.php', '', basename($filename)); foreach ($newClasses as $className) { if (substr($className, 0 - strlen($baseName)) == $baseName) { $newClasses = array($className); break; } } $tests = array(); // of PHPUnit_Framework_Test 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()) { $newTest = $method->invoke(NULL, $className); // of type PHPUnit_Framework_Test $tests[] = $newTest; } } else { if ($class->implementsInterface('PHPUnit_Framework_Test')) { $tests[] = new PHPUnit_Framework_TestSuite($class); } } } } return $tests; }
protected function guess_class_from_path($file) { // Somebody is using just the file name, we need to look inside the file and guess the testcase // class name. Let's throw fatal error if there are more testcases in one file. PHPUnit_Util_Class::collectStart(); PHPUnit_Util_Fileloader::checkAndLoad($file); $includePathFilename = stream_resolve_include_path($file); $loadedClasses = PHPUnit_Util_Class::collectEnd(); $candidates = array(); foreach ($loadedClasses as $loadedClass) { $class = new ReflectionClass($loadedClass); if ($class->isSubclassOf('PHPUnit_Framework_TestCase') and !$class->isAbstract()) { if (realpath($includePathFilename) === realpath($class->getFileName())) { $candidates[] = $loadedClass; } } } if (count($candidates) == 0) { throw new PHPUnit_Framework_Exception(sprintf("File '%s' does not contain any test cases.", $file)); } if (count($candidates) > 1) { throw new PHPUnit_Framework_Exception(sprintf("File '%s' contains multiple test cases: " . implode(', ', $candidates), $file)); } $classname = reset($candidates); return new ReflectionClass($classname); }