From 9d80834da40fabc975eeb8050e380e90b3273e70 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 22 Sep 2023 23:40:31 +0800 Subject: [PATCH] Test files must be named _test.php --- moodle/Tests/MoodleUtilTest.php | 24 ++++++++++++++++++------ moodle/Util/MoodleUtil.php | 5 +++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/moodle/Tests/MoodleUtilTest.php b/moodle/Tests/MoodleUtilTest.php index 651d587..4b39de1 100644 --- a/moodle/Tests/MoodleUtilTest.php +++ b/moodle/Tests/MoodleUtilTest.php @@ -476,27 +476,39 @@ public static function isUnitTestProvider(): array { return [ 'Not in tests directory' => [ - 'value' => '/path/to/standard/file.php', + 'value' => '/path/to/standard/file_test.php', 'return' => false, ], 'In tests directory' => [ - 'value' => '/path/to/standard/tests/file.php', + 'value' => '/path/to/standard/tests/file_test.php', 'return' => true, ], + 'In tests directory but missing _test suffix' => [ + 'value' => '/path/to/standard/tests/file.php', + 'return' => false, + ], + 'In tests directory but some idiot put a _test.php suffix on a directory' => [ + 'value' => '/path/to/standard/tests/some_test.php/file.php', + 'return' => false, + ], 'In test sub-directory' => [ - 'value' => '/path/to/standard/tests/sub/file.php', + 'value' => '/path/to/standard/tests/sub/file_test.php', 'return' => true, ], + 'In test sub-directory but missing _test suffix' => [ + 'value' => '/path/to/standard/tests/sub/file.php', + 'return' => false, + ], 'Generator' => [ - 'value' => '/path/to/standard/tests/generator/file.php', + 'value' => '/path/to/standard/tests/generator/file_test.php', 'return' => false, ], 'Fixture' => [ - 'value' => '/path/to/standard/tests/fixtures/file.php', + 'value' => '/path/to/standard/tests/fixtures/file_test.php', 'return' => false, ], 'Behat' => [ - 'value' => '/path/to/standard/tests/behat/behat_test_file.php', + 'value' => '/path/to/standard/tests/behat/behat_test_file_test.php', 'return' => false, ], ]; diff --git a/moodle/Util/MoodleUtil.php b/moodle/Util/MoodleUtil.php index 8befc45..c091b00 100644 --- a/moodle/Util/MoodleUtil.php +++ b/moodle/Util/MoodleUtil.php @@ -384,6 +384,11 @@ public static function getMoodleRoot(File $file = null, bool $selfPath = true) { */ public static function isUnitTest(File $phpcsFile): bool { + // If the file isn't called, _test.php, nothing to check. + if (stripos(basename($phpcsFile->getFilename()), '_test.php') === false) { + return false; + } + // If the file isn't under tests directory, nothing to check. if (stripos($phpcsFile->getFilename(), '/tests/') === false) { return false;