From 09ee487763faee113d08e3ede267eeaa246448ad Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sun, 9 Jan 2022 11:55:49 +0100 Subject: [PATCH] Report missing testcase classes in the line the classes are Instead of reporting the moodle.PHPUnit.TestCaseNames.Missing error in line 0, when classes have been found incorrect, issue an error on the lines they are declared. If no classes are found, then the reporting line will continue being 0. Covered with tests. --- moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php | 7 ++++++- moodle/tests/phpunit_testcasenames_test.php | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php b/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php index 4cd3586e..dd9456f6 100644 --- a/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php +++ b/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php @@ -113,7 +113,9 @@ public function process(File $file, $pointer) { // verify that it extends something and that has a test_ method. $class = ''; $classFound = false; + $classPointers = []; // Save all class pointers to report later if no class is found. while ($cStart = $file->findNext(T_CLASS, $pointer)) { + $classPointers[] = $cStart; $pointer = $cStart + 1; // Move the pointer to the class start. // Only if the class is extending something. @@ -146,7 +148,10 @@ public function process(File $file, $pointer) { // No testcase class found, this is plain-wrong. if (!$classFound) { - $file->addError('PHPUnit test file missing any valid testcase class declaration', 0, 'Missing'); + $classPointers = $classPointers ?: [0]; + foreach ($classPointers as $classPointer) { + $file->addError('PHPUnit test file missing any valid testcase class declaration', $classPointer, 'Missing'); + } return; // If arrived here we don't have a valid class, we are finished. } diff --git a/moodle/tests/phpunit_testcasenames_test.php b/moodle/tests/phpunit_testcasenames_test.php index 2b42cfa7..4d3c2ebe 100644 --- a/moodle/tests/phpunit_testcasenames_test.php +++ b/moodle/tests/phpunit_testcasenames_test.php @@ -45,7 +45,8 @@ public function provider_phpunit_testcasenames() { 'Missing' => [ 'fixture' => 'fixtures/phpunit/testcasenames_missing.php', 'errors' => [ - 1 => '@Message: PHPUnit test file missing any valid testcase class', + 7 => '@Message: PHPUnit test file missing any valid testcase class', + 14 => 'Missing', ], 'warnings' => [], ],