diff --git a/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php b/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php index 7b57de3..0112409 100644 --- a/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php +++ b/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php @@ -29,7 +29,7 @@ use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil; use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Files\File; -use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Utils\FunctionDeclarations; class TestCaseNamesSniff implements Sniff { @@ -122,6 +122,16 @@ public function process(File $file, $pointer) { $method = ''; $methodFound = false; while ($mStart = $file->findNext(T_FUNCTION, $pointer, $tokens[$cStart]['scope_closer'])) { + $info = FunctionDeclarations::getProperties($file, $mStart); + if (!$info['has_body']) { + // Some methods have no body (for example, abstract). + // Therefore there is no scope_closer + // There may be a parenthesis closer, or a semi-colon, + // but there is no easy way to determine this. + // Fall back to finding the next function based on the pointer position. + $pointer = $mStart + 1; + continue; + } $pointer = $tokens[$mStart]['scope_closer']; // Next iteration look after the end of current method. if (strpos($file->getDeclarationName($mStart), 'test_') === 0) { $methodFound = true; diff --git a/moodle/Tests/PHPUnitTestCaseNamesTest.php b/moodle/Tests/PHPUnitTestCaseNamesTest.php index ca64fe8..191519e 100644 --- a/moodle/Tests/PHPUnitTestCaseNamesTest.php +++ b/moodle/Tests/PHPUnitTestCaseNamesTest.php @@ -52,6 +52,11 @@ public function provider_phpunit_testcasenames() { ], 'warnings' => [], ], + 'Abstract methods in a testcase' => [ + 'fixture' => 'fixtures/phpunit/testcasenames_with_abstract_test.php', + 'errors' => [], + 'warnings' => [], + ], 'NoMatch' => [ 'fixture' => 'fixtures/phpunit/testcasenames_nomatch.php', 'errors' => [], diff --git a/moodle/Tests/fixtures/phpunit/testcasenames_with_abstract_test.php b/moodle/Tests/fixtures/phpunit/testcasenames_with_abstract_test.php new file mode 100644 index 0000000..0ad69a4 --- /dev/null +++ b/moodle/Tests/fixtures/phpunit/testcasenames_with_abstract_test.php @@ -0,0 +1,17 @@ +generate_test_data(); + // Test something with the data. + } +}