From ab8b0ea5825c1a833b4fcc456d04eeb4fe26dcd3 Mon Sep 17 00:00:00 2001 From: Andrew Coulton Date: Tue, 24 Nov 2015 18:09:07 +0000 Subject: [PATCH] Throw exception when @testWith annotation contains invalid datasets 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. --- src/Util/Test.php | 8 +++++++- tests/Util/TestTest.php | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Util/Test.php b/src/Util/Test.php index cef33b37819..617ef4c0cc5 100644 --- a/src/Util/Test.php +++ b/src/Util/Test.php @@ -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; } diff --git a/tests/Util/TestTest.php b/tests/Util/TestTest.php index 2ec3829b15b..752de36608a 100644 --- a/tests/Util/TestTest.php +++ b/tests/Util/TestTest.php @@ -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 *