Skip to content

Commit

Permalink
AlwaysReturnInFilter: update unit tests.
Browse files Browse the repository at this point in the history
* Adjust the "abstract method" test to:
    1. Expect a warning.
    2. Prevent the hook in getting confused with other functions in the same test file.
* Remove the "abstract method implementation" test as it wasn't testing anything.
    1. The sniff does not look for child classes, so wouldn't examine that code snippet anyway.
        Adding this functionality is not that useful either as in most cases, the child class will not be in the same file as the abstract parent class.
    2. And even if the sniff did examine it, it would still not recognize it as a child class as the class doesn't `extend` the abstract.
* Add a test for a typical case where declared functions do not have a scope opener/closer due to a tokenizer bug.
    PR squizlabs/PHP_CodeSniffer 3066 is open upstream to fix this.
* Add a "live coding" test case.
  • Loading branch information
jrfnl committed Sep 9, 2020
1 parent 015b740 commit 8e4077d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
29 changes: 16 additions & 13 deletions WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,26 @@ class bad_example_class_short_array { // Error.
}
}

abstract class good_example_abstract_class { // Ok.
abstract class warning_filter_points_to_abstract_method {
public function __construct() {
add_filter( 'good_example_class_filter', [ $this, 'class_filter' ] );
add_filter( 'good_example_abstract_class', [ $this, 'abstract_method' ] );
}

abstract public function class_filter( $param );
abstract public function abstract_method( $param ); // Warning.
}

class good_example_abstract_class_implementation { // Ok.
public function class_filter( $param ) {
if ( 1 === 1 ) {
if ( 1 === 0 ) {
return 'whoops';
} else {
return 'here!';
}
}
return 'This is Okay';
class tokenizer_bug_test {
public function __construct() {
add_filter( 'tokenizer_bug', [ $this, 'tokenizer_bug' ] );
}

public function tokenizer_bug( $param ): namespace\Foo {} // Ok (tokenizer bug in PHPCS < 3.5.7/3.6.0).
}

// Intentional parse error. This has to be the last test in the file!
class parse_error_test {
public function __construct() {
add_filter( 'parse_error', [ $this, 'parse_error' ] );
}

public function parse_error( $param ) // Ok, parse error ignored.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function getErrorList() {
* @return array <int line number> => <int number of warnings>
*/
public function getWarningList() {
return [];
return [
180 => 1,
];
}

}

0 comments on commit 8e4077d

Please sign in to comment.