Skip to content
Browse files

Fix TRAC-1056.

  • Loading branch information...
1 parent 1f36365 commit c54fb5b89b656bcf33fd0b5fc73244e9a30a340a @sebastianbergmann committed May 25, 2010
Showing with 65 additions and 45 deletions.
  1. +23 −13 PHPUnit/Framework/TestSuite.php
  2. +41 −32 PHPUnit/Util/Test.php
  3. +1 −0 README.markdown
View
36 PHPUnit/Framework/TestSuite.php
@@ -512,11 +512,32 @@ public static function createTest(ReflectionClass $theClass, $name, array $class
// TestCase($name, $data)
else {
- $data = PHPUnit_Util_Test::getProvidedData($className, $name);
+ try {
+ $data = PHPUnit_Util_Test::getProvidedData(
+ $className, $name
+ );
+ }
+
+ catch (Exception $e) {
+ $message = sprintf(
+ 'The data provider specified for %s::%s is invalid.',
+ $className,
+ $name
+ );
+
+ $_message = $e->getMessage();
+
+ if (!empty($_message)) {
+ $message .= "\n" . $_message;
+ }
+
+ return new PHPUnit_Framework_Warning($message);
+ }
+
$groups = PHPUnit_Util_Test::getGroups($className, $name);
// Test method with @dataProvider.
- if (is_array($data) || $data instanceof Iterator) {
+ if (isset($data)) {
$test = new PHPUnit_Framework_TestSuite_DataProvider(
$className . '::' . $name
);
@@ -548,17 +569,6 @@ public static function createTest(ReflectionClass $theClass, $name, array $class
}
}
- // Test method with invalid @dataProvider.
- else if ($data === FALSE) {
- $test = new PHPUnit_Framework_Warning(
- sprintf(
- 'The data provider specified for %s::%s is invalid.',
- $className,
- $name
- )
- );
- }
-
else {
$test = new $className;
}
View
73 PHPUnit/Util/Test.php
@@ -239,54 +239,63 @@ public static function getExpectedException($docComment)
* @return mixed array|Iterator when a data provider is specified and exists
* false when a data provider is specified and does not exist
* null when no data provider is specified
- * @throws ReflectionException
* @since Method available since Release 3.2.0
*/
public static function getProvidedData($className, $methodName)
{
$reflector = new ReflectionMethod($className, $methodName);
$docComment = $reflector->getDocComment();
+ $data = NULL;
if (preg_match(self::REGEX_DATA_PROVIDER, $docComment, $matches)) {
- try {
- $dataProviderMethodNameNamespace = explode('\\', $matches[1]);
- $leaf = explode('::', array_pop($dataProviderMethodNameNamespace));
- $dataProviderMethodName = array_pop($leaf);
-
- if (!empty($dataProviderMethodNameNamespace)) {
- $dataProviderMethodNameNamespace = join('\\', $dataProviderMethodNameNamespace) . '\\';
- } else {
- $dataProviderMethodNameNamespace = '';
- }
+ $dataProviderMethodNameNamespace = explode('\\', $matches[1]);
+ $leaf = explode('::', array_pop($dataProviderMethodNameNamespace));
+ $dataProviderMethodName = array_pop($leaf);
- if (!empty($leaf)) {
- $dataProviderClassName = $dataProviderMethodNameNamespace . array_pop($leaf);
- } else {
- $dataProviderClassName = $className;
- }
+ if (!empty($dataProviderMethodNameNamespace)) {
+ $dataProviderMethodNameNamespace = join('\\', $dataProviderMethodNameNamespace) . '\\';
+ } else {
+ $dataProviderMethodNameNamespace = '';
+ }
- $dataProviderClass = new ReflectionClass($dataProviderClassName);
- $dataProviderMethod = $dataProviderClass->getMethod(
- $dataProviderMethodName
- );
+ if (!empty($leaf)) {
+ $dataProviderClassName = $dataProviderMethodNameNamespace . array_pop($leaf);
+ } else {
+ $dataProviderClassName = $className;
+ }
- if ($dataProviderMethod->isStatic()) {
- $object = NULL;
- } else {
- $object = $dataProviderClass->newInstance();
- }
+ $dataProviderClass = new ReflectionClass($dataProviderClassName);
+ $dataProviderMethod = $dataProviderClass->getMethod(
+ $dataProviderMethodName
+ );
- if ($dataProviderMethod->getNumberOfParameters() == 0) {
- return $dataProviderMethod->invoke($object);
- } else {
- return $dataProviderMethod->invoke($object, $methodName);
- }
+ if ($dataProviderMethod->isStatic()) {
+ $object = NULL;
+ } else {
+ $object = $dataProviderClass->newInstance();
}
- catch (ReflectionException $e) {
- return FALSE;
+ if ($dataProviderMethod->getNumberOfParameters() == 0) {
+ $data = $dataProviderMethod->invoke($object);
+ } else {
+ $data = $dataProviderMethod->invoke($object, $methodName);
+ }
+ }
+
+ if ($data !== NULL) {
+ foreach ($data as $key => $value) {
+ if (!is_array($value)) {
+ throw new InvalidArgumentException(
+ sprintf(
+ 'Data set %s is invalid.',
+ is_int($key) ? '#' . $key : '"' . $key . '"'
+ )
+ );
+ }
}
}
+
+ return $data;
}
/**
View
1 README.markdown
@@ -6,6 +6,7 @@ This is the list of changes for the PHPUnit 3.4 release series.
PHPUnit 3.4.14
--------------
+* Fixed TRAC-1056: Badly done data providers cause crashes.
* Fixed TRAC-1057: `phpunit_coverage.php` deletes all files in web server document root.
PHPUnit 3.4.13

0 comments on commit c54fb5b

Please sign in to comment.
Something went wrong with that request. Please try again.