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 728bc71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Util/Test.php
Original file line number Diff line number Diff line change
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
14 changes: 13 additions & 1 deletion tests/Util/TestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,25 @@ public function testTestWithThrowsProperExceptionIfDatasetCannotBeParsed()
{
$this->setExpectedExceptionRegExp(
'PHPUnit_Framework_Exception',
'/^The dataset for the @testWith annotation cannot be parsed.$/'
'/^The dataset for the @testWith annotation cannot be parsed:$/'
);
PHPUnit_Util_Test::getDataFromTestWithAnnotation('/**
* @testWith [s]
*/');
}

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 728bc71

Please sign in to comment.