Skip to content

Commit 5a9d329

Browse files
committed
Add test cases for 4 sniffs that were missing them
There were 4 old Sniffs not covered with tests at all. Here we do cover them, they are really simple sniffs, plus tweak a little bit existing tests to cover some unhandled cases. With this coverage should rocker to ~95%, yay!
1 parent 2076873 commit 5a9d329

15 files changed

+295
-3
lines changed

moodle/Sniffs/Commenting/InlineCommentSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ public function process(File $phpcsFile, $stackPtr) {
406406

407407
if ($commentText === '') {
408408
$error = 'Blank comments are not allowed';
409-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty');
409+
$fix = $phpcsFile->addFixableError($error, $lastCommentToken, 'Empty');
410410
if ($fix === true) {
411-
$phpcsFile->fixer->replaceToken($stackPtr, '');
411+
$phpcsFile->fixer->replaceToken($lastCommentToken, '');
412412
}
413413

414414
return ($lastCommentToken + 1);

moodle/Sniffs/PHP/IncludingFileSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function process(File $file, $stackptr) {
4646
if ($tokens[$stackptr + 1]['code'] !== T_OPEN_PARENTHESIS) {
4747
$error = '"%s" must be immediately followed by an open parenthesis';
4848
$data = array($tokens[$stackptr]['content']);
49-
$file->addError($error, $stackptr, 'BracketsNotRequired', $data);
49+
$file->addError($error, $stackptr, 'BracketsRequired', $data);
5050
}
5151

5252
$incondition = (count($tokens[$stackptr]['conditions']) !== 0) ? true : false;

moodle/Tests/MoodleStandardTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public function test_moodle_commenting_inlinecomment() {
106106
135 => 0,
107107
136 => 0,
108108
137 => 0,
109+
143 => '@Message: Tab found before comment',
110+
144 => '@Source: moodle.Commenting.InlineComment.SpacingBefore',
111+
146 => '@Message: Blank comments are not allowed',
109112
]);
110113
$this->set_warnings([
111114
4 => 0,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace MoodleHQ\MoodleCS\moodle\Tests;
18+
19+
// phpcs:disable moodle.NamingConventions
20+
21+
/**
22+
* Test the IncludingFile sniff.
23+
*
24+
* @package local_codechecker
25+
* @category test
26+
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*
29+
* @covers \MoodleHQ\MoodleCS\moodle\Sniffs\PHP\IncludingFileSniff
30+
*/
31+
class PHPIncludingFileTest extends MoodleCSBaseTest {
32+
33+
public function test_php_includingfile() {
34+
// Define the standard, sniff and fixture to use.
35+
$this->set_standard('moodle');
36+
$this->set_sniff('moodle.PHP.IncludingFile');
37+
$this->set_fixture(__DIR__ . '/fixtures/php/includingfile.php');
38+
39+
// Define expected results (errors and warnings). Format, array of:
40+
// - line => number of problems, or
41+
// - line => array of contents for message / source problem matching.
42+
// - line => string of contents for message / source problem matching (only 1).
43+
$this->set_errors([
44+
9 => '@Message: "require" must be immediately followed by an open parenthesis',
45+
10 => '@Source: moodle.PHP.IncludingFile.BracketsRequired',
46+
13 => 1,
47+
14 => 1,
48+
17 => '@Source: moodle.PHP.IncludingFile.UseRequire',
49+
18 => '@Source: moodle.PHP.IncludingFile.UseRequireOnce',
50+
]);
51+
$this->set_warnings([]);
52+
53+
// Let's do all the hard work!
54+
$this->verify_cs_results();
55+
}
56+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace MoodleHQ\MoodleCS\moodle\Tests;
18+
19+
// phpcs:disable moodle.NamingConventions
20+
21+
/**
22+
* Test the IncludingFile sniff.
23+
*
24+
* @package local_codechecker
25+
* @category test
26+
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*
29+
* @covers \MoodleHQ\MoodleCS\moodle\Sniffs\PHP\MemberVarScopeSniff
30+
*/
31+
class PHPMemberVarScopeTest extends MoodleCSBaseTest {
32+
33+
public function test_php_membervarscope() {
34+
// Define the standard, sniff and fixture to use.
35+
$this->set_standard('moodle');
36+
$this->set_sniff('moodle.PHP.MemberVarScope');
37+
$this->set_fixture(__DIR__ . '/fixtures/php/membervarscope.php');
38+
39+
// Define expected results (errors and warnings). Format, array of:
40+
// - line => number of problems, or
41+
// - line => array of contents for message / source problem matching.
42+
// - line => string of contents for message / source problem matching (only 1).
43+
$this->set_errors([
44+
8 => '@Message: Scope modifier not specified for member variable "$missingprop"',
45+
9 => '@Source: moodle.PHP.MemberVarScope.Missing',
46+
]);
47+
$this->set_warnings([]);
48+
49+
// Let's do all the hard work!
50+
$this->verify_cs_results();
51+
}
52+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace MoodleHQ\MoodleCS\moodle\Tests;
18+
19+
// phpcs:disable moodle.NamingConventions
20+
21+
/**
22+
* Test the SpaceAfterComma sniff.
23+
*
24+
* @package local_codechecker
25+
* @category test
26+
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*
29+
* @covers \MoodleHQ\MoodleCS\moodle\Sniffs\WhiteSpace\SpaceAfterCommaSniff
30+
*/
31+
class WhiteSpaceSpaceAfterCommaTest extends MoodleCSBaseTest {
32+
33+
public function test_whitespace_spaceaftercomma() {
34+
// Define the standard, sniff and fixture to use.
35+
$this->set_standard('moodle');
36+
$this->set_sniff('moodle.WhiteSpace.SpaceAfterComma');
37+
$this->set_fixture(__DIR__ . '/fixtures/whitespace/spaceaftercomma.php');
38+
39+
// Define expected results (errors and warnings). Format, array of:
40+
// - line => number of problems, or
41+
// - line => array of contents for message / source problem matching.
42+
// - line => string of contents for message / source problem matching (only 1).
43+
$this->set_errors([
44+
5 => 2,
45+
]);
46+
$this->set_warnings([]);
47+
48+
// Let's do all the hard work!
49+
$this->verify_cs_results();
50+
}
51+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace MoodleHQ\MoodleCS\moodle\Tests;
18+
19+
// phpcs:disable moodle.NamingConventions
20+
21+
/**
22+
* Test the WhiteSpaceInStrings sniff.
23+
*
24+
* @package local_codechecker
25+
* @category test
26+
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*
29+
* @covers \MoodleHQ\MoodleCS\moodle\Sniffs\WhiteSpace\WhiteSpaceInStringsSniff
30+
*/
31+
class WhiteSpaceWhiteSpaceInStringsTest extends MoodleCSBaseTest {
32+
33+
public function test_whitespace_whitespaceinstrings() {
34+
// Define the standard, sniff and fixture to use.
35+
$this->set_standard('moodle');
36+
$this->set_sniff('moodle.WhiteSpace.WhiteSpaceInStrings');
37+
$this->set_fixture(__DIR__ . '/fixtures/whitespace/whitespaceinstrings.php');
38+
39+
// Define expected results (errors and warnings). Format, array of:
40+
// - line => number of problems, or
41+
// - line => array of contents for message / source problem matching.
42+
// - line => string of contents for message / source problem matching (only 1).
43+
$this->set_errors([
44+
5 => '@Message: Whitespace found at end of line within string',
45+
7 => '@Message: Tab found within whitespace',
46+
]);
47+
$this->set_warnings([]);
48+
49+
// Let's do all the hard work!
50+
$this->verify_cs_results();
51+
}
52+
}

moodle/Tests/fixtures/moodle_comenting_inlinecomment.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@ define([], function() {
4242
alert('foo');
4343
};
4444

45+
var Watermelon = function(title) {
46+
var self = this;
47+
}; // End of code block comments.
48+
4549
return Selector;
4650
});

moodle/Tests/fixtures/moodle_comenting_inlinecomment.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,14 @@ final function afunction() {}
135135
$something = 1;// @codeCoverageIgnoreStart
136136
$something = 1; // @codeCoverageIgnoreEnd
137137
$something = 1; // @codeCoverageIgnoreAnythingInvented
138+
139+
function blah() {
140+
} // End of code block comments.
141+
142+
function dah() {
143+
// A tab in the comment is not nice.
144+
// Neither 2 spaces in the comment are.
145+
$something = 1;
146+
//
147+
148+
// Blank comments, like previous one, are ugly too.

moodle/Tests/fixtures/moodle_php_forbidden_global_use.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ public function bad() {
5757
return $PAGE->url;
5858
}
5959
}
60+
61+
class block_another extends block_base {
62+
public $something;
63+
}

0 commit comments

Comments
 (0)