diff --git a/moodle/Tests/MoodleStandardTest.php b/moodle/Tests/MoodleStandardTest.php index 046178c..b4bcaf6 100644 --- a/moodle/Tests/MoodleStandardTest.php +++ b/moodle/Tests/MoodleStandardTest.php @@ -32,6 +32,63 @@ */ class MoodleStandardTest extends MoodleCSBaseTestCase { + /** + * Test the PSR12.Functions.ReturnTypeDeclaration sniff. + * + * @covers \PHP_CodeSniffer\Standards\PSR12\Sniffs\Functions\ReturnTypeDeclarationSniff + */ + public function test_psr12_functions_returntypedeclaration() { + + // Define the standard, sniff and fixture to use. + $this->set_standard('moodle'); + $this->set_sniff('PSR12.Functions.ReturnTypeDeclaration'); + $this->set_fixture(__DIR__ . '/fixtures/psr12_functions_returntypedeclaration.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 = [ + 30 => 'SpaceBeforeColon', + 34 => 'SpaceBeforeReturnType', + 38 => 'SpaceBeforeReturnType', + ]; + $warnings = []; + $this->set_errors($errors); + $this->set_warnings($warnings); + + // Let's do all the hard work! + $this->verify_cs_results(); + } + + /** + * Test the PSR12.Functions.NullableTypeDeclaration sniff. + * + * @covers \PHP_CodeSniffer\Standards\PSR12\Sniffs\Functions\NullableTypeDeclarationSniff + */ + public function test_psr12_functions_nullabletypedeclaration() { + + // Define the standard, sniff and fixture to use. + $this->set_standard('moodle'); + $this->set_sniff('PSR12.Functions.NullableTypeDeclaration'); + $this->set_fixture(__DIR__ . '/fixtures/psr12_functions_nullabletypedeclaration.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 = [ + 17 => 'WhitespaceFound', + 22 => 3, + ]; + $warnings = []; + $this->set_errors($errors); + $this->set_warnings($warnings); + + // Let's do all the hard work! + $this->verify_cs_results(); + } + /** * Test the PSR2.Methods.MethodDeclaration sniff. * diff --git a/moodle/Tests/Sniffs/Methods/MethodDeclarationSpacingSniffTest.php b/moodle/Tests/Sniffs/Methods/MethodDeclarationSpacingSniffTest.php new file mode 100644 index 0000000..365cd10 --- /dev/null +++ b/moodle/Tests/Sniffs/Methods/MethodDeclarationSpacingSniffTest.php @@ -0,0 +1,66 @@ +. + +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(); + } +} diff --git a/moodle/Tests/fixtures/Methods/MethodDeclarationSpacing.php b/moodle/Tests/fixtures/Methods/MethodDeclarationSpacing.php new file mode 100644 index 0000000..b3d3dfb --- /dev/null +++ b/moodle/Tests/fixtures/Methods/MethodDeclarationSpacing.php @@ -0,0 +1,64 @@ +