forked from moodlehq/moodle-cs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cover all the new function declaration sniffs with own test
This covers both the 2 psr12 imported ones and, also, our new MethodDeclarationSpacingSniff.
- Loading branch information
Showing
8 changed files
with
391 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
moodle/Tests/Sniffs/Methods/MethodDeclarationSpacingSniffTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
// This file is part of Moodle - https://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Methods; | ||
|
||
use MoodleHQ\MoodleCS\moodle\Tests\MoodleCSBaseTestCase; | ||
|
||
// phpcs:disable moodle.NamingConventions | ||
|
||
/** | ||
* Test the MethodDeclarationSpacing sniff. | ||
* | ||
* @copyright 2024 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com} | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* | ||
* @covers \MoodleHQ\MoodleCS\moodle\Sniffs\Methods\MethodDeclarationSpacingSniff | ||
*/ | ||
class MethodDeclarationSpacingSniffTest extends MoodleCSBaseTestCase | ||
{ | ||
public function testMethodDeclarationSpacing(): void { | ||
// Define the standard, sniff and fixture to use. | ||
$this->set_standard('moodle'); | ||
$this->set_sniff('moodle.Methods.MethodDeclarationSpacing'); | ||
$this->set_fixture(__DIR__ . '/../../fixtures/Methods/MethodDeclarationSpacing.php'); | ||
|
||
// Define expected results (errors and warnings). Format, array of: | ||
// - line => number of problems, or | ||
// - line => array of contents for message / source problem matching. | ||
// - line => string of contents for message / source problem matching (only 1). | ||
$errors = [ | ||
43 => 4, | ||
45 => [ | ||
'Expected 0 spaces after "("; 2 found', | ||
'Expected 0 spaces after "method2"; 2 found', | ||
'Expected 1 space after "function"; 2 found', | ||
'Expected 1 space after "final"; 2 found', | ||
], | ||
49 => [ | ||
'ZeroExpectedAfter', | ||
'ZeroExpectedAfter', | ||
'OneExpectedAfter', | ||
], | ||
53 => 1, | ||
61 => 3, | ||
]; | ||
$warnings = []; | ||
$this->set_errors($errors); | ||
$this->set_warnings($warnings); | ||
|
||
// Let's do all the hard work! | ||
$this->verify_cs_results(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
moodle/Tests/fixtures/Methods/MethodDeclarationSpacing.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures. | ||
|
||
// All the methods in this class are correct. | ||
class correct_methods { | ||
abstract public static function method1(); | ||
|
||
final public static function method2() { | ||
return true; | ||
} | ||
|
||
public function method3() { | ||
return true; | ||
} | ||
|
||
public /* comment */ function method4() { | ||
// We don't do anything with with closures. | ||
$a = function () { | ||
return true; | ||
}; | ||
return true; | ||
} | ||
} | ||
|
||
class nested_function_correct { | ||
public function getAnonymousClass() { | ||
return new class() { | ||
public function nested_function() {} | ||
}; | ||
} | ||
} | ||
|
||
// We don't do anything with with global scope functions. | ||
function global_function ( ) { | ||
return true; | ||
} | ||
|
||
// All the methods in this class are incorrect. | ||
// (note that the MethodDeclarationSpacing DOES NOT fix all the | ||
// keywords in this class, because some are handled by other sniffs. | ||
// Take a look to the class documentation for more information). | ||
class incorrect_methods { | ||
abstract public static function method3 ( ); | ||
|
||
final public static function method2 ( ) { | ||
return true; | ||
} | ||
|
||
public function method3 ( ) { | ||
return true; | ||
} | ||
|
||
public function method4 chocolate() { | ||
return true; | ||
} | ||
} | ||
|
||
class nested_function_incorrect { | ||
public function getAnonymousClass() { | ||
return new class() { | ||
public function nested_function ( ) {} | ||
}; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
moodle/Tests/fixtures/Methods/MethodDeclarationSpacing.php.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures. | ||
|
||
// All the methods in this class are correct. | ||
class correct_methods { | ||
abstract public static function method1(); | ||
|
||
final public static function method2() { | ||
return true; | ||
} | ||
|
||
public function method3() { | ||
return true; | ||
} | ||
|
||
public /* comment */ function method4() { | ||
// We don't do anything with with closures. | ||
$a = function () { | ||
return true; | ||
}; | ||
return true; | ||
} | ||
} | ||
|
||
class nested_function_correct { | ||
public function getAnonymousClass() { | ||
return new class() { | ||
public function nested_function() {} | ||
}; | ||
} | ||
} | ||
|
||
// We don't do anything with with global scope functions. | ||
function global_function ( ) { | ||
return true; | ||
} | ||
|
||
// All the methods in this class are incorrect. | ||
// (note that the MethodDeclarationSpacing DOES NOT fix all the | ||
// keywords in this class, because some are handled by other sniffs. | ||
// Take a look to the class documentation for more information). | ||
class incorrect_methods { | ||
abstract public static function method3(); | ||
|
||
final public static function method2() { | ||
return true; | ||
} | ||
|
||
public function method3() { | ||
return true; | ||
} | ||
|
||
public function method4chocolate() { | ||
return true; | ||
} | ||
} | ||
|
||
class nested_function_incorrect { | ||
public function getAnonymousClass() { | ||
return new class() { | ||
public function nested_function() {} | ||
}; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
moodle/Tests/fixtures/psr12_functions_nullabletypedeclaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures. | ||
|
||
// All the return types in this class are correct. | ||
class correct_methods { | ||
public function return_type_nullable_ok(): ?int { | ||
return 1; | ||
} | ||
} | ||
|
||
function return_type_nullable_ok(): ?int { | ||
return 1; | ||
} | ||
|
||
// All the return types in this class have problems.. | ||
class incorrect_methods { | ||
public function return_type_nullable_wrong(): ? int { | ||
return 1; | ||
} | ||
} | ||
|
||
function all_type_nullable_wrong(? int $one, ? int $two): ? int { | ||
return 1; | ||
} |
24 changes: 24 additions & 0 deletions
24
moodle/Tests/fixtures/psr12_functions_nullabletypedeclaration.php.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures. | ||
|
||
// All the return types in this class are correct. | ||
class correct_methods { | ||
public function return_type_nullable_ok(): ?int { | ||
return 1; | ||
} | ||
} | ||
|
||
function return_type_nullable_ok(): ?int { | ||
return 1; | ||
} | ||
|
||
// All the return types in this class have problems.. | ||
class incorrect_methods { | ||
public function return_type_nullable_wrong(): ?int { | ||
return 1; | ||
} | ||
} | ||
|
||
function all_type_nullable_wrong(?int $one, ?int $two): ?int { | ||
return 1; | ||
} |
46 changes: 46 additions & 0 deletions
46
moodle/Tests/fixtures/psr12_functions_returntypedeclaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures. | ||
|
||
// All the return types in this class are correct. | ||
class correct_methods { | ||
public function no_return_type() { | ||
return; | ||
} | ||
|
||
public function return_void(): void { | ||
return null; | ||
} | ||
|
||
public function return_type(): int { | ||
return 1; | ||
} | ||
|
||
public function return_type_nullable(): ?int { | ||
return 1; | ||
} | ||
|
||
public function return_type_union(): int|string|null { | ||
return null; | ||
} | ||
} | ||
|
||
// All the return types in this class have problems.. | ||
class incorrect_methods { | ||
|
||
public function return_wrong_colon_many_spacing() : int { | ||
return 1; | ||
} | ||
|
||
public function return_wrong_type_many_spacing(): int { | ||
return 1; | ||
} | ||
|
||
public function return_wrong_type_no_spacing():int { | ||
return 1; | ||
} | ||
|
||
// Note this corresponds to the PSR12.Functions.NullableTypeDeclaration Sniff, so it's correct for this fixture. | ||
public function return_wrong_nullable_many_spacing(): ? int { | ||
return 1; | ||
} | ||
} |
Oops, something went wrong.