Skip to content

Commit 76ce969

Browse files
authored
Merge pull request #59 from andrewnicols/unitTests
Check validity of unit test dataProviders
2 parents 8e1fc14 + ae762af commit 76ce969

24 files changed

+1359
-33
lines changed

moodle-extra/ruleset.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,19 @@
8181
8282
-->
8383

84+
<!--
85+
Detect issues with Unit Test dataProviders:
86+
- private providers
87+
- providers which do not exist
88+
- providers whose name is prefixed with _test
89+
- incorrect casing of dataProvider
90+
- dataProviders which do not return an array or Iterable
91+
- dataProviders which can be converted to a static method (PHPUnit 10 compatibility)
92+
-->
93+
<rule ref="moodle.PHPUnit.TestCaseProvider">
94+
<properties>
95+
<property name="autofixStaticProviders" value="true"/>
96+
</properties>
97+
</rule>
98+
8499
</ruleset>

moodle/Sniffs/PHPUnit/TestCaseCoversSniff.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@ public function process(File $file, $pointer) {
4949

5050
// Before starting any check, let's look for various things.
5151

52-
// Get the moodle branch being analysed.
53-
$moodleBranch = MoodleUtil::getMoodleBranch($file);
52+
// If we aren't checking Moodle 4.0dev (400) and up, nothing to check.
53+
// Make and exception for codechecker phpunit tests, so they are run always.
54+
if (!MoodleUtil::meetsMinimumMoodleVersion($file, 400) && !MoodleUtil::isUnitTestRunning()) {
55+
return; // @codeCoverageIgnore
56+
}
5457

55-
// Detect if we are running PHPUnit.
56-
$runningPHPUnit = defined('PHPUNIT_TEST') && PHPUNIT_TEST;
58+
// If the file is not a unit test file, nothing to check.
59+
if (!MoodleUtil::isUnitTest($file) && !MoodleUtil::isUnitTestRunning()) {
60+
return; // @codeCoverageIgnore
61+
}
5762

5863
// We have all we need from core, let's start processing the file.
5964

@@ -70,24 +75,6 @@ public function process(File $file, $pointer) {
7075
return; // @codeCoverageIgnore
7176
}
7277

73-
// If we aren't checking Moodle 4.0dev (400) and up, nothing to check.
74-
// Make and exception for codechecker phpunit tests, so they are run always.
75-
if (isset($moodleBranch) && $moodleBranch < 400 && !$runningPHPUnit) {
76-
return; // @codeCoverageIgnore
77-
}
78-
79-
// If the file isn't under tests directory, nothing to check.
80-
if (stripos($file->getFilename(), '/tests/') === false) {
81-
return; // @codeCoverageIgnore
82-
}
83-
84-
// If the file isn't called, _test.php, nothing to check.
85-
// Make an exception for codechecker own phpunit fixtures here, allowing any name for them.
86-
$fileName = basename($file->getFilename());
87-
if (substr($fileName, -9) !== '_test.php' && !$runningPHPUnit) {
88-
return; // @codeCoverageIgnore
89-
}
90-
9178
// Iterate over all the classes (hopefully only one, but that's not this sniff problem).
9279
$cStart = $pointer;
9380
while ($cStart = $file->findNext(T_CLASS, $cStart + 1)) {

moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function process(File $file, $pointer) {
6868
$moodleComponent = MoodleUtil::getMoodleComponent($file);
6969

7070
// Detect if we are running PHPUnit.
71-
$runningPHPUnit = defined('PHPUNIT_TEST') && PHPUNIT_TEST;
71+
$runningPHPUnit = MoodleUtil::isUnitTestRunning();
7272

7373
// We have all we need from core, let's start processing the file.
7474

@@ -81,17 +81,11 @@ public function process(File $file, $pointer) {
8181
return; // @codeCoverageIgnore
8282
}
8383

84-
// If the file isn't under tests directory, nothing to check.
85-
if (stripos($file->getFilename(), '/tests/') === false) {
84+
// If the file is not a unit test file, nothing to check.
85+
if (!MoodleUtil::isUnitTest($file) && !$runningPHPUnit) {
8686
return; // @codeCoverageIgnore
8787
}
88-
89-
// If the file isn't called, _test.php, nothing to check.
90-
// Make an exception for codechecker own phpunit fixtures here, allowing any name for them.
9188
$fileName = basename($file->getFilename());
92-
if (substr($fileName, -9) !== '_test.php' && !$runningPHPUnit) {
93-
return; // @codeCoverageIgnore
94-
}
9589

9690
// In order to cover the duplicates detection, we need to set some
9791
// properties (caches) here. It's extremely hard to do

0 commit comments

Comments
 (0)