From 1139c798566612175a96538b0484af01803595c4 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sun, 5 Dec 2021 17:58:37 +0100 Subject: [PATCH] Make the fixer able to fix testcase name to match file name --- moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php | 8 +++++++- .../fixtures/phpunit/testcasenames_nomatch.php.fixed | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 moodle/tests/fixtures/phpunit/testcasenames_nomatch.php.fixed diff --git a/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php b/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php index 90eead4c..9db10077 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 @@ +