Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception when @testWith annotation contains invalid datasets #1968

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would json_last_error_msg() not be available?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was only added in PHP 5.5.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, and PHPUnit 4.8 still needs to support that. Repressed that fact, sorry.

Copy link
Contributor Author

@acoulton acoulton Jun 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, PHP 5.5 is so long ago I had repressed it at first too :)

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