Skip to content

Commit

Permalink
Merge pull request #63 from andrewnicols/abstract_test_methods
Browse files Browse the repository at this point in the history
Handle the case where an abstract testcase exists
  • Loading branch information
stronk7 authored Sep 18, 2023
2 parents 76ce969 + 86a2281 commit a2f7fdd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
12 changes: 11 additions & 1 deletion moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\FunctionDeclarations;

class TestCaseNamesSniff implements Sniff {

Expand Down Expand Up @@ -122,6 +122,16 @@ public function process(File $file, $pointer) {
$method = '';
$methodFound = false;
while ($mStart = $file->findNext(T_FUNCTION, $pointer, $tokens[$cStart]['scope_closer'])) {
$info = FunctionDeclarations::getProperties($file, $mStart);
if (!$info['has_body']) {
// Some methods have no body (for example, abstract).
// Therefore there is no scope_closer
// There may be a parenthesis closer, or a semi-colon,
// but there is no easy way to determine this.
// Fall back to finding the next function based on the pointer position.
$pointer = $mStart + 1;
continue;
}
$pointer = $tokens[$mStart]['scope_closer']; // Next iteration look after the end of current method.
if (strpos($file->getDeclarationName($mStart), 'test_') === 0) {
$methodFound = true;
Expand Down
5 changes: 5 additions & 0 deletions moodle/Tests/PHPUnitTestCaseNamesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function provider_phpunit_testcasenames() {
],
'warnings' => [],
],
'Abstract methods in a testcase' => [
'fixture' => 'fixtures/phpunit/testcasenames_with_abstract_test.php',
'errors' => [],
'warnings' => [],
],
'NoMatch' => [
'fixture' => 'fixtures/phpunit/testcasenames_nomatch.php',
'errors' => [],
Expand Down
17 changes: 17 additions & 0 deletions moodle/Tests/fixtures/phpunit/testcasenames_with_abstract_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace local_codechecker;
defined("MOODLE_INTERNAL") || die(); // Make this always the 1st line in all CS fixtures.

/**
* Class which is an abstract testcase.
*/
abstract class testcasenames_with_abstract_test extends base_test {
abstract public function generate_test_data(): array;

abstract public function generate_example_data();

public function test_something(): void {
$data = $this->generate_test_data();
// Test something with the data.
}
}

0 comments on commit a2f7fdd

Please sign in to comment.