Skip to content

Commit

Permalink
Make the fixer able to fix testcase name to match file name
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Dec 5, 2021
1 parent 9c5e650 commit 1139c79
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,14 @@ public function process(File $file, $pointer) {
// Check if the file name and the class name match, warn if not.
$baseName = pathinfo($fileName, PATHINFO_FILENAME);
if ($baseName !== $class) {
$file->addWarning('PHPUnit testcase name "%s" does not match file name "%s"', $cStart,
$fix = $file->addFixableWarning('PHPUnit testcase name "%s" does not match file name "%s"', $cStart,
'NoMatch', [$class, $baseName]);

if ($fix === true) {
if ($cNameToken = $file->findNext(T_STRING, $cStart + 1, $tokens[$cStart]['scope_opener'])) {
$file->fixer->replaceToken($cNameToken, $baseName);
}
}
}

// Check if the class has been already found (this is useful when running against a lot of files).
Expand Down
11 changes: 11 additions & 0 deletions moodle/tests/fixtures/phpunit/testcasenames_nomatch.php.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace local_codechecker;
defined("MOODLE_INTERNAL") || die(); // Make this always the 1st line in all CS fixtures.

/**
* Correct class but with name not matching the file name.
*/
class testcasenames_nomatch extends local_codechecker_testcase {
public function test_something() {
}
}

0 comments on commit 1139c79

Please sign in to comment.