Skip to content

Commit

Permalink
Throw exception when @testwith annotation contains invalid datasets
Browse files Browse the repository at this point in the history
The existing code was silently ignoring invalid JSON if it had
found at least one valid dataset. It was using this behaviour
to detect the end of the @testwith annotation (see for eg the
testTestWithSimpleTextAfter test, which fails if a simple
throw on invalid JSON is used).

This fix treats a line as being part of the @testwith if it begins
with a `[` character - since all datasets are required to be JSON
arrays this should be reliable.

It can then reliably throw if invalid JSON is found for any of the
datasets.
  • Loading branch information
acoulton committed Mar 21, 2016
1 parent 1a1b632 commit ab8b0ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Util/Test.php
Expand Up @@ -469,9 +469,15 @@ public static function getDataFromTestWithAnnotation($docComment)
$data = array();
foreach (explode("\n", $annotationContent) as $candidateRow) {
$candidateRow = trim($candidateRow);
if ($candidateRow[0] !== '[') {
break;
}
$dataSet = json_decode($candidateRow, true);
if (json_last_error() != JSON_ERROR_NONE) {
break;
$error = function_exists('json_last_error_msg') ? json_last_error_msg() : json_last_error();
throw new PHPUnit_Framework_Exception(
'The dataset for the @testWith annotation cannot be parsed: '.$error
);
}
$data[] = $dataSet;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Util/TestTest.php
Expand Up @@ -353,6 +353,18 @@ public function testTestWithThrowsProperExceptionIfDatasetCannotBeParsed()
*/');
}

public function testTestWithThrowsProperExceptionIfMultiLineDatasetCannotBeParsed()
{
$this->setExpectedExceptionRegExp(
'PHPUnit_Framework_Exception',
'/^The dataset for the @testWith annotation cannot be parsed:/'
);
PHPUnit_Util_Test::getDataFromTestWithAnnotation('/**
* @testWith ["valid"]
* [invalid]
*/');
}

/**
* @covers PHPUnit_Util_Test::getDependencies
*
Expand Down

0 comments on commit ab8b0ea

Please sign in to comment.