diff --git a/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php b/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php index 61e943a9..ae77da98 100644 --- a/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php +++ b/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php @@ -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). diff --git a/moodle/tests/fixtures/phpunit/testcasenames_nomatch.php.fixed b/moodle/tests/fixtures/phpunit/testcasenames_nomatch.php.fixed new file mode 100644 index 00000000..57187b0b --- /dev/null +++ b/moodle/tests/fixtures/phpunit/testcasenames_nomatch.php.fixed @@ -0,0 +1,11 @@ +