Skip to content

Commit

Permalink
Report missing testcase classes in the line the classes are
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stronk7 committed Jan 9, 2022
1 parent 95ff201 commit 09ee487
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
}

Expand Down
3 changes: 2 additions & 1 deletion moodle/tests/phpunit_testcasenames_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
],
Expand Down

0 comments on commit 09ee487

Please sign in to comment.