From 9d861b333a44423972ffe7e819c510e14752445b 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 | 14 +++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) 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..2b425364a12 100644 --- a/tests/Util/TestTest.php +++ b/tests/Util/TestTest.php @@ -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 *