Skip to content

Commit

Permalink
Merge pull request #164 from stronk7/fix_testcasename_nomatch
Browse files Browse the repository at this point in the history
Make the fixer able to fix testcase name to match file name
  • Loading branch information
stronk7 authored Dec 21, 2021
2 parents c2f5090 + 198a0f8 commit d2e3b3f
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 d2e3b3f

Please sign in to comment.