Skip to content

Commit

Permalink
Cover all the new function declaration sniffs with own test
Browse files Browse the repository at this point in the history
This covers both the 2 psr12 imported ones and, also,
our new MethodDeclarationSpacingSniff.
  • Loading branch information
stronk7 committed Feb 14, 2024
1 parent 98fdaea commit 29c3ee3
Show file tree
Hide file tree
Showing 8 changed files with 391 additions and 0 deletions.
57 changes: 57 additions & 0 deletions moodle/Tests/MoodleStandardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
66 changes: 66 additions & 0 deletions moodle/Tests/Sniffs/Methods/MethodDeclarationSpacingSniffTest.php
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 moodle/Tests/fixtures/Methods/MethodDeclarationSpacing.php
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 moodle/Tests/fixtures/Methods/MethodDeclarationSpacing.php.fixed
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 moodle/Tests/fixtures/psr12_functions_nullabletypedeclaration.php
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;
}
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 moodle/Tests/fixtures/psr12_functions_returntypedeclaration.php
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;
}
}
Loading

0 comments on commit 29c3ee3

Please sign in to comment.