Skip to content

Commit

Permalink
IsUnitTestTrait: make sure namespace and class names are checked case…
Browse files Browse the repository at this point in the history
…-insensitively

As PHP treats namespace names and class names case-insensitively, this helper should do so too.

This is a bug fix as the sniff would previously throw false positives for test classes where the case of the class name/extended class name did not match.

Tested by adding an extra test to the `WordPress.Files.FilesName` sniff.
  • Loading branch information
jrfnl committed Jul 4, 2023
1 parent ca8a41a commit 0a23302
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
26 changes: 19 additions & 7 deletions WordPress/Helpers/IsUnitTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ trait IsUnitTestTrait {

// PHPUnit native test cases.
'PHPUnit_Framework_TestCase' => true,
'PHPUnit\Framework\TestCase' => true,
'PHPUnit\\Framework\\TestCase' => true,
// PHPUnit native TestCase class when imported via use statement.
'TestCase' => true,
);
Expand Down Expand Up @@ -143,9 +143,15 @@ protected function get_all_test_classes() {
}
}

/*
* Lowercase all names, both custom as well as "known", as PHP treats namespaced names case-insensitively.
*/
$custom_test_classes = array_map( 'strtolower', $custom_test_classes );
$known_test_classes = array_change_key_case( $this->known_test_classes, \CASE_LOWER );

$this->all_test_classes = RulesetPropertyHelper::merge_custom_array(
$custom_test_classes,
$this->known_test_classes
$known_test_classes
);

// Store the original value so the comparison can succeed.
Expand Down Expand Up @@ -184,15 +190,19 @@ protected function is_test_class( File $phpcsFile, $stackPtr ) {
// Add any potentially extra custom test classes to the known test classes list.
$known_test_classes = $this->get_all_test_classes();

$namespace = strtolower( Namespaces::determineNamespace( $phpcsFile, $stackPtr ) );

// Is the class/trait one of the known test classes ?
$namespace = Namespaces::determineNamespace( $phpcsFile, $stackPtr );
$className = ObjectDeclarations::getName( $phpcsFile, $stackPtr );
if ( '' !== $namespace ) {
if ( isset( $known_test_classes[ $namespace . '\\' . $className ] ) ) {
if ( empty( $className ) === false ) {
$className = strtolower( $className );
if ( '' !== $namespace ) {
if ( isset( $known_test_classes[ $namespace . '\\' . $className ] ) ) {
return true;
}
} elseif ( isset( $known_test_classes[ $className ] ) ) {
return true;
}
} elseif ( isset( $known_test_classes[ $className ] ) ) {
return true;
}

// Does the class/trait extend one of the known test classes ?
Expand All @@ -201,6 +211,8 @@ protected function is_test_class( File $phpcsFile, $stackPtr ) {
return false;
}

$extendedClassName = strtolower( $extendedClassName );

if ( '\\' === $extendedClassName[0] ) {
if ( isset( $known_test_classes[ substr( $extendedClassName, 1 ) ] ) ) {
return true;
Expand Down
1 change: 1 addition & 0 deletions WordPress/Tests/Files/FileNameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ final class FileNameUnitTest extends AbstractSniffUnitTest {
*/
'test-sample-phpunit.inc' => 0,
'test-sample-phpunit6.inc' => 0,
'test-sample-phpunit6-case-insensitive.inc' => 0,
'test-sample-wpunit.inc' => 0,
'test-sample-custom-unit.1.inc' => 0,
'test-sample-custom-unit.2.inc' => 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
// Take note of the `PHPunit` vs `PHPUnit` in the namespace and the `Testcase` vs `TestCase` for the extended class name.
class TestSample extends PHPunit\Framework\Testcase {}

0 comments on commit 0a23302

Please sign in to comment.