Skip to content

Commit

Permalink
Merge pull request #68 from andrewnicols/providerNameContainsParents
Browse files Browse the repository at this point in the history
Remove parens from dataProvider names
  • Loading branch information
stronk7 authored Sep 22, 2023
2 parents 0a4fc09 + f634dbf commit c67abee
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
18 changes: 18 additions & 0 deletions moodle/Sniffs/PHPUnit/TestCaseProviderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ protected function checkDataProvider(
);
}

if (substr($methodName, -2) === '()') {
$fix = $file->addFixableWarning(
'Data provider should not end with "()". "%s" provided.',
$pointer + 2,
'dataProviderSyntaxMethodnameContainsParenthesis',
[
$methodName,
]
);

$methodName = substr($methodName, 0, -2);
if ($fix) {
$file->fixer->beginChangeset();
$file->fixer->replaceToken($pointer + 2, $methodName);
$file->fixer->endChangeset();
}
}

// Find the method itself.
$classPointer = $file->findPrevious(T_CLASS, $pointer - 1);
$providerPointer = MoodleUtil::findClassMethodPointer($file, $classPointer, $methodName);
Expand Down
8 changes: 8 additions & 0 deletions moodle/Tests/PHPUnitTestCaseProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function provider_phpunit_data_providers() {
'warnings' => [
],
],
'Provider has Parenthesis' => [
'fixture' => 'fixtures/phpunit/provider/provider_parents_test.php',
'errors' => [
],
'warnings' => [
6 => 'Data provider should not end with "()". "provider()" provided.',
],
],
'Provider Visibility' => [
'fixture' => 'fixtures/phpunit/provider/provider_visibility_test.php',
'errors' => [
Expand Down
15 changes: 15 additions & 0 deletions moodle/Tests/fixtures/phpunit/provider/provider_parents_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures.

class provider_casing_test extends base_test {
/**
* @dataProvider provider()
*/
public function test_one(): void {
// Nothing to test.
}

public static function provider(): array {
return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures.

class provider_casing_test extends base_test {
/**
* @dataProvider provider
*/
public function test_one(): void {
// Nothing to test.
}

public static function provider(): array {
return [];
}
}

0 comments on commit c67abee

Please sign in to comment.