Permalink
Browse files

- Merge [4633].

  • Loading branch information...
1 parent 2f41dae commit 07aec63f96f1f1421ec9fc9ffe3775a516241578 @sebastianbergmann committed Feb 11, 2009
View
5 PHPUnit/Extensions/GroupTestSuite.php
@@ -89,11 +89,8 @@ public function __construct(PHPUnit_Framework_TestSuite $suite, array $groups)
foreach ($tests as $test) {
if ($test instanceof PHPUnit_Framework_TestCase) {
- $class = new ReflectionClass($test);
- $method = $class->getMethod($test->getName(FALSE));
-
$testGroups = PHPUnit_Util_Test::getGroups(
- $method->getDocComment(), PHPUnit_Util_Test::getGroups($class)
+ get_class($test), $test->getName(FALSE)
);
foreach ($groups as $group) {
View
15 PHPUnit/Extensions/SeleniumTestCase.php
@@ -153,7 +153,6 @@ public static function suite($className)
$suite->setName($className);
$class = new ReflectionClass($className);
- $classGroups = PHPUnit_Util_Test::getGroups($class->getDocComment());
$staticProperties = $class->getStaticProperties();
// Create tests from Selenese/HTML files.
@@ -197,10 +196,9 @@ public static function suite($className)
foreach ($class->getMethods() as $method) {
if (PHPUnit_Framework_TestSuite::isPublicTestMethod($method)) {
- $name = $method->getName();
- $methodDocComment = $method->getDocComment();
- $data = PHPUnit_Util_Test::getProvidedData($className, $name, $methodDocComment);
- $groups = PHPUnit_Util_Test::getGroups($methodDocComment, $classGroups);
+ $name = $method->getName();
+ $data = PHPUnit_Util_Test::getProvidedData($className, $name);
+ $groups = PHPUnit_Util_Test::getGroups($className, $name);
// Test method with @dataProvider.
if (is_array($data) || $data instanceof Iterator) {
@@ -235,10 +233,9 @@ public static function suite($className)
else {
foreach ($class->getMethods() as $method) {
if (PHPUnit_Framework_TestSuite::isPublicTestMethod($method)) {
- $name = $method->getName();
- $methodDocComment = $method->getDocComment();
- $data = PHPUnit_Util_Test::getProvidedData($className, $name, $methodDocComment);
- $groups = PHPUnit_Util_Test::getGroups($methodDocComment, $classGroups);
+ $name = $method->getName();
+ $data = PHPUnit_Util_Test::getProvidedData($className, $name);
+ $groups = PHPUnit_Util_Test::getGroups($className, $name);
// Test method with @dataProvider.
if (is_array($data) || $data instanceof Iterator) {
View
29 PHPUnit/Framework/TestCase.php
@@ -346,6 +346,19 @@ public function count()
}
/**
+ * Returns the annotations for this test.
+ *
+ * @return array
+ * @since Method available since Release 3.4.0
+ */
+ public function getAnnotations()
+ {
+ return PHPUnit_Util_Test::parseTestMethodAnnotations(
+ get_class($this), $this->name
+ );
+ }
+
+ /**
* Gets the name of a TestCase.
*
* @param boolean $withDataSet
@@ -420,14 +433,8 @@ public function setUseErrorHandler($useErrorHandler)
protected function setUseErrorHandlerFromAnnotation()
{
try {
- $className = get_class($this);
- $class = new ReflectionClass($className);
- $classDocComment = $class->getDocComment();
- $method = new ReflectionMethod($className, $this->name);
- $methodDocComment = $method->getDocComment();
-
$useErrorHandler = PHPUnit_Util_Test::getErrorHandlerSettings(
- $classDocComment, $methodDocComment
+ get_class($this), $this->name
);
if ($useErrorHandler !== NULL) {
@@ -454,14 +461,8 @@ public function setUseOutputBuffering($useOutputBuffering)
protected function setUseOutputBufferingFromAnnotation()
{
try {
- $className = get_class($this);
- $class = new ReflectionClass($className);
- $classDocComment = $class->getDocComment();
- $method = new ReflectionMethod($className, $this->name);
- $methodDocComment = $method->getDocComment();
-
$useOutputBuffering = PHPUnit_Util_Test::getOutputBufferingSettings(
- $classDocComment, $methodDocComment
+ get_class($this), $this->name
);
if ($useOutputBuffering !== NULL) {
View
34 PHPUnit/Framework/TestSuite.php
@@ -231,23 +231,11 @@ public function __construct($theClass = '', $name = '')
return;
}
- $className = $theClass->getName();
- $classDocComment = $theClass->getDocComment();
- $names = array();
- $classGroups = PHPUnit_Util_Test::getGroups($classDocComment);
- $classDependencies = PHPUnit_Util_Test::getDependencies($classDocComment);
+ $names = array();
foreach ($theClass->getMethods() as $method) {
if (strpos($method->getDeclaringClass()->getName(), 'PHPUnit_') !== 0) {
- $methodDocComment = $method->getDocComment();
-
- $this->addTestMethod(
- $theClass,
- $method,
- PHPUnit_Util_Test::getDependencies($methodDocComment, $classDependencies),
- PHPUnit_Util_Test::getGroups($methodDocComment, $classGroups),
- $names
- );
+ $this->addTestMethod($theClass, $method, $names);
}
}
@@ -491,7 +479,7 @@ public static function createTest(ReflectionClass $theClass, $name, array $class
$method = new ReflectionMethod($className, $name);
$methodDocComment = $method->getDocComment();
$runTestInSeparateProcess = FALSE;
- $backupSettings = PHPUnit_Util_Test::getBackupSettings($classDocComment, $methodDocComment);
+ $backupSettings = PHPUnit_Util_Test::getBackupSettings($className, $name);
if (!$theClass->isInstantiable()) {
return self::warning(
@@ -518,8 +506,7 @@ public static function createTest(ReflectionClass $theClass, $name, array $class
// TestCase($name, $data)
else {
- $data = PHPUnit_Util_Test::getProvidedData($className, $name, $methodDocComment);
- $groups = PHPUnit_Util_Test::getGroups($methodDocComment, $classGroups);
+ $data = PHPUnit_Util_Test::getProvidedData($className, $name);
if (is_array($data) || $data instanceof Iterator) {
$test = new PHPUnit_Framework_TestSuite(
@@ -528,7 +515,8 @@ public static function createTest(ReflectionClass $theClass, $name, array $class
foreach ($data as $_dataName => $_data) {
$test->addTest(
- new $className($name, $_data, $_dataName), $groups
+ new $className($name, $_data, $_dataName),
+ PHPUnit_Util_Test::getGroups($className, $name)
);
if ($runTestInSeparateProcess) {
@@ -771,11 +759,9 @@ public function markTestSuiteSkipped($message = '')
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method
- * @param array $dependencies
- * @param array $groups
* @param array $names
*/
- protected function addTestMethod(ReflectionClass $class, ReflectionMethod $method, array $dependencies, array $groups, array &$names)
+ protected function addTestMethod(ReflectionClass $class, ReflectionMethod $method, array &$names)
{
$name = $method->getName();
@@ -786,15 +772,15 @@ protected function addTestMethod(ReflectionClass $class, ReflectionMethod $metho
if ($this->isPublicTestMethod($method)) {
$names[] = $name;
- $test = self::createTest($class, $name, $groups);
+ $test = self::createTest($class, $name);
if (!$test instanceof PHPUnit_Framework_TestSuite) {
$test->setDependencies(
- PHPUnit_Util_Test::getDependencies($method->getDocComment(), $dependencies)
+ PHPUnit_Util_Test::getDependencies($class->getName(), $name)
);
}
- $this->addTest($test, $groups);
+ $this->addTest($test, PHPUnit_Util_Test::getGroups($class->getName(), $name));
}
else if ($this->isTestMethod($method)) {
View
23 PHPUnit/Tests/Util/TestTest.php
@@ -62,29 +62,6 @@
*/
class Util_TestTest extends PHPUnit_Framework_TestCase
{
- public function testGetDependencies()
- {
- $this->assertEquals(
- array('method'),
- PHPUnit_Util_Test::getDependencies('@depends method')
- );
-
- $this->assertEquals(
- array('class::method'),
- PHPUnit_Util_Test::getDependencies('@depends class::method')
- );
-
- $this->assertEquals(
- array('namespace\class::method'),
- PHPUnit_Util_Test::getDependencies('@depends namespace\class::method')
- );
-
- $this->assertEquals(
- array('namespace\namespace\class::method'),
- PHPUnit_Util_Test::getDependencies('@depends namespace\namespace\class::method')
- );
- }
-
public function testGetExpectedException()
{
$this->assertEquals(
View
309 PHPUnit/Util/Test.php
@@ -62,15 +62,11 @@
*/
class PHPUnit_Util_Test
{
- const REGEX_BACKUP_GLOBALS = '/@backupGlobals\s+([a-zA-Z0-9._-]+)/';
- const REGEX_BACKUP_STATIC_ATTRIBUTES = '/@backupStaticAttributes\s+([a-zA-Z0-9._-]+)/';
const REGEX_COVERS = '/@covers[\s]+([\!<>\:\.\w]+)([\s]+<extended>)?/';
const REGEX_DATA_PROVIDER = '/@dataProvider\s+([a-zA-Z0-9._:-\\\]+)/';
- const REGEX_DEPENDS = '/@depends\s+([a-zA-Z0-9._:-\\\]+)/';
- const REGEX_USE_ERROR_HANDLER = '/@errorHandler\s+([a-zA-Z0-9._-]+)/';
const REGEX_EXPECTED_EXCEPTION = '(@expectedException\s+([:.\w\\\]+)(?:[\t ]+(\S*))?(?:[\t ]+(\S*))?\s*$)m';
- const REGEX_GROUP = '/@group\s+([a-zA-Z0-9._-]+)/';
- const REGEX_USE_OUTPUT_BUFFERING = '/@outputBuffering\s+([a-zA-Z0-9._-]+)/';
+
+ private static $annotationCache = array();
/**
* @param PHPUnit_Framework_Test $test
@@ -199,23 +195,6 @@ public static function getLinesToBeCovered($className, $methodName)
}
/**
- * Returns the dependencies for a test class or method.
- *
- * @param string $docComment
- * @param array $dependencies
- * @return array
- * @since Method available since Release 3.4.0
- */
- public static function getDependencies($docComment, array $dependencies = array())
- {
- if (preg_match_all(self::REGEX_DEPENDS, $docComment, $matches)) {
- $dependencies = array_unique(array_merge($dependencies, $matches[1]));
- }
-
- return $dependencies;
- }
-
- /**
* Returns the expected exception for a test.
*
* @param string $docComment
@@ -246,33 +225,20 @@ public static function getExpectedException($docComment)
}
/**
- * Returns the groups for a test class or method.
- *
- * @param string $docComment
- * @param array $groups
- * @return array
- * @since Method available since Release 3.2.0
- */
- public static function getGroups($docComment, array $groups = array())
- {
- if (preg_match_all(self::REGEX_GROUP, $docComment, $matches)) {
- $groups = array_unique(array_merge($groups, $matches[1]));
- }
-
- return $groups;
- }
-
- /**
* Returns the provided data for a method.
*
* @param string $className
* @param string $methodName
* @param string $docComment
* @return array
+ * @throws ReflectionException
* @since Method available since Release 3.2.0
*/
- public static function getProvidedData($className, $methodName, $docComment)
+ public static function getProvidedData($className, $methodName)
{
+ $reflector = new ReflectionMethod($className, $methodName);
+ $docComment = $reflector->getDocComment();
+
if (preg_match(self::REGEX_DATA_PROVIDER, $docComment, $matches)) {
try {
$dataProviderMethodNameNamespace = explode('\\', $matches[1]);
@@ -315,89 +281,6 @@ public static function getProvidedData($className, $methodName, $docComment)
}
/**
- * Returns the backup settings for a test.
- *
- * @param string $classDocComment
- * @param string $methodDocComment
- * @return array
- * @since Method available since Release 3.4.0
- */
- public static function getBackupSettings($classDocComment, $methodDocComment)
- {
- return array(
- 'backupGlobals' => self::getSettings(
- $classDocComment, $methodDocComment, self::REGEX_BACKUP_GLOBALS
- ),
- 'backupStaticAttributes' => self::getSettings(
- $classDocComment, $methodDocComment, self::REGEX_BACKUP_STATIC_ATTRIBUTES
- )
- );
- }
-
- /**
- * Returns the error handler settings for a test.
- *
- * @param string $classDocComment
- * @param string $methodDocComment
- * @return boolean
- * @since Method available since Release 3.4.0
- */
- public static function getErrorHandlerSettings($classDocComment, $methodDocComment)
- {
- return self::getSettings(
- $classDocComment, $methodDocComment, self::REGEX_USE_ERROR_HANDLER
- );
- }
-
- /**
- * Returns the output buffering settings for a test.
- *
- * @param string $classDocComment
- * @param string $methodDocComment
- * @return boolean
- * @since Method available since Release 3.4.0
- */
- public static function getOutputBufferingSettings($classDocComment, $methodDocComment)
- {
- return self::getSettings(
- $classDocComment, $methodDocComment, self::REGEX_USE_OUTPUT_BUFFERING
- );
- }
-
- /**
- * @param string $classDocComment
- * @param string $methodDocComment
- * @return boolean
- * @since Method available since Release 3.4.0
- */
- private static function getSettings($classDocComment, $methodDocComment, $regex)
- {
- $result = NULL;
-
- if (preg_match($regex, $classDocComment, $matches)) {
- if ($matches[1] == 'enabled') {
- $result = TRUE;
- }
-
- else if ($matches[1] == 'disabled') {
- $result = FALSE;
- }
- }
-
- if (preg_match($regex, $methodDocComment, $matches)) {
- if ($matches[1] == 'enabled') {
- $result = TRUE;
- }
-
- else if ($matches[1] == 'disabled') {
- $result = FALSE;
- }
- }
-
- return $result;
- }
-
- /**
* Returns the files and lines a test method wants to cover.
*
* @param string $method
@@ -474,5 +357,183 @@ private static function resolveCoversToReflectionObjects($method, $extended)
return $codeToCoverList;
}
+
+ /**
+ * @param string $className
+ * @param string $methodName
+ * @return array
+ * @throws ReflectionException
+ * @since Method available since Release 3.4.0
+ */
+ public static function parseTestMethodAnnotations($className, $methodName)
+ {
+ if (!isset(self::$annotationCache[$className])) {
+ $class = new ReflectionClass($className);
+ self::$annotationCache[$className] = self::parseAnnotations($class->getDocComment());
+ }
+
+ if (!isset(self::$annotationCache[$className . '::' . $methodName])) {
+ $method = new ReflectionMethod($className, $methodName);
+ self::$annotationCache[$className . '::' . $methodName] = self::parseAnnotations($method->getDocComment());
+ }
+
+ return array(
+ 'class' => self::$annotationCache[$className],
+ 'method' => self::$annotationCache[$className . '::' . $methodName]
+ );
+ }
+
+ /**
+ * @param string $docblock
+ * @return array
+ * @since Method available since Release 3.4.0
+ */
+ private static function parseAnnotations($docblock)
+ {
+ $annotations = array();
+
+ if (preg_match_all('(@(?P<name>[A-Za-z_-]+)\s+(?P<value>.*?)\s*$)m', $docblock, $matches)) {
+ $numMatches = count($matches[0]);
+
+ for ($i = 0; $i < $numMatches; ++$i) {
+ $annotations[$matches['name'][$i]][] = $matches['value'][$i];
+ }
+ }
+
+ return $annotations;
+ }
+
+ /**
+ * Returns the backup settings for a test.
+ *
+ * @param string $className
+ * @param string $methodName
+ * @return array
+ * @since Method available since Release 3.4.0
+ */
+ public static function getBackupSettings($className, $methodName)
+ {
+ return array(
+ 'backupGlobals' => self::getBooleanAnnotationSetting(
+ $className, $methodName, 'backupGlobals'
+ ),
+ 'backupStaticAttributes' => self::getBooleanAnnotationSetting(
+ $className, $methodName, 'backupStaticAttributes'
+ )
+ );
+ }
+
+ /**
+ * Returns the dependencies for a test class or method.
+ *
+ * @param string $className
+ * @param string $methodName
+ * @return array
+ * @since Method available since Release 3.4.0
+ */
+ public static function getDependencies($className, $methodName)
+ {
+ $annotations = self::parseTestMethodAnnotations($className, $methodName);
+ $dependencies = array();
+
+ if (isset($annotations['class']['depends'])) {
+ $dependencies = $annotations['class']['depends'];
+ }
+
+ if (isset($annotations['method']['depends'])) {
+ $dependencies = array_merge($dependencies, $annotations['method']['depends']);
+ }
+
+ return array_unique($dependencies);
+ }
+
+ /**
+ * Returns the error handler settings for a test.
+ *
+ * @param string $className
+ * @param string $methodName
+ * @return boolean
+ * @since Method available since Release 3.4.0
+ */
+ public static function getErrorHandlerSettings($className, $methodName)
+ {
+ return self::getBooleanAnnotationSetting(
+ $className, $methodName, 'errorHandler'
+ );
+ }
+
+ /**
+ * Returns the groups for a test class or method.
+ *
+ * @param string $className
+ * @param string $methodName
+ * @return array
+ * @since Method available since Release 3.2.0
+ */
+ public static function getGroups($className, $methodName)
+ {
+ $annotations = self::parseTestMethodAnnotations($className, $methodName);
+ $groups = array();
+
+ if (isset($annotations['class']['group'])) {
+ $groups = $annotations['class']['group'];
+ }
+
+ if (isset($annotations['method']['group'])) {
+ $groups = array_merge($groups, $annotations['method']['group']);
+ }
+
+ return array_unique($groups);
+ }
+
+ /**
+ * Returns the output buffering settings for a test.
+ *
+ * @param string $className
+ * @param string $methodName
+ * @return boolean
+ * @since Method available since Release 3.4.0
+ */
+ public static function getOutputBufferingSettings($className, $methodName)
+ {
+ return self::getBooleanAnnotationSetting(
+ $className, $methodName, 'outputBuffering'
+ );
+ }
+
+ /**
+ * @param string $className
+ * @param string $methodName
+ * @param string $settingName
+ * @return boolean
+ * @since Method available since Release 3.4.0
+ */
+ private static function getBooleanAnnotationSetting($className, $methodName, $settingName)
+ {
+ $annotations = self::parseTestMethodAnnotations($className, $methodName);
+ $result = NULL;
+
+ if (isset($annotations['class'][$settingName])) {
+ if ($annotations['class'][$settingName][0] == 'enabled') {
+ $result = TRUE;
+ }
+
+ else if ($annotations['class'][$settingName][0] == 'disabled') {
+ $result = FALSE;
+ }
+ }
+
+ if (isset($annotations['method'][$settingName])) {
+ if ($annotations['method'][$settingName][0] == 'enabled') {
+ $result = TRUE;
+ }
+
+ else if ($annotations['method'][$settingName][0] == 'disabled') {
+ $result = FALSE;
+ }
+ }
+
+ return $result;
+ }
}
?>

0 comments on commit 07aec63

Please sign in to comment.