diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc new file mode 100644 index 0000000000..b3b2015bf2 --- /dev/null +++ b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc @@ -0,0 +1,26 @@ +is_string( /* testObjectMethod */ $value ); +$obj?->is_object( /* testNullsafeObjectMethod */ $value ); +TypeChecker::is_int( /* testStaticMethod */ $value ); +is_scalar( my_function( /* testNestedNonTargetFunctionCall */ $value ) ); + +/* + * The below should be recognized as being inside a type test function call. + */ + +is_array( /* testUnqualifiedFunction */ $value ); +Is_Bool( /* testMixedCaseFunction */ $value ); +\is_string( /* testFullyQualifiedFunction */ $value ); +\IS_NUMERIC( /* testFullyQualifiedUpperCaseFunction */ $value ); +is_int( is_float( /* testNestedTypeTestCall */ $value ) ); diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php new file mode 100644 index 0000000000..cc731c6713 --- /dev/null +++ b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php @@ -0,0 +1,115 @@ +getTargetToken( $testMarker, \T_VARIABLE ); + $result = ContextHelper::is_in_type_test( self::$phpcsFile, $stackPtr ); + + $this->assertSame( $expectedResult, $result ); + } + + /** + * Data provider. + * + * @see testIsInTypeTest() + * + * @return array> + */ + public static function dataIsInTypeTest() { + return array( + // Cases that should return false. + 'bare_variable' => array( + 'testMarker' => '/* testBareVariable */', + 'expectedResult' => false, + ), + 'other_function_call' => array( + 'testMarker' => '/* testOtherFunctionCall */', + 'expectedResult' => false, + ), + 'partially_qualified_function' => array( + 'testMarker' => '/* testPartiallyQualifiedFunction */', + 'expectedResult' => false, + ), + 'fully_qualified_namespaced_function' => array( + 'testMarker' => '/* testFullyQualifiedNamespacedFunction */', + 'expectedResult' => false, + ), + 'namespace_relative_function' => array( + 'testMarker' => '/* testNamespaceRelativeFunction */', + 'expectedResult' => false, + ), + 'namespace_relative_sub_function' => array( + 'testMarker' => '/* testNamespaceRelativeSubFunction */', + 'expectedResult' => false, + ), + 'object_method' => array( + 'testMarker' => '/* testObjectMethod */', + 'expectedResult' => false, + ), + 'nullsafe_object_method' => array( + 'testMarker' => '/* testNullsafeObjectMethod */', + 'expectedResult' => false, + ), + 'static_method' => array( + 'testMarker' => '/* testStaticMethod */', + 'expectedResult' => false, + ), + 'nested_non_target_function_call' => array( + 'testMarker' => '/* testNestedNonTargetFunctionCall */', + 'expectedResult' => false, + ), + + // Cases that should return true. + 'unqualified_function' => array( + 'testMarker' => '/* testUnqualifiedFunction */', + 'expectedResult' => true, + ), + 'mixed_case_function' => array( + 'testMarker' => '/* testMixedCaseFunction */', + 'expectedResult' => true, + ), + 'fully_qualified_function' => array( + 'testMarker' => '/* testFullyQualifiedFunction */', + 'expectedResult' => true, + ), + 'fully_qualified_upper_case_function' => array( + 'testMarker' => '/* testFullyQualifiedUpperCaseFunction */', + 'expectedResult' => true, + ), + 'nested_type_test_call' => array( + 'testMarker' => '/* testNestedTypeTestCall */', + 'expectedResult' => true, + ), + ); + } +} diff --git a/WordPress/Tests/Security/NonceVerificationUnitTest.php b/WordPress/Tests/Security/NonceVerificationUnitTest.php index 081de9a00f..d5ee162048 100644 --- a/WordPress/Tests/Security/NonceVerificationUnitTest.php +++ b/WordPress/Tests/Security/NonceVerificationUnitTest.php @@ -18,7 +18,6 @@ * @since 0.13.0 Class name changed: this class is now namespaced. * @since 1.0.0 This sniff has been moved from the `CSRF` category to the `Security` category. * - * @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_type_test * @covers \WordPressCS\WordPress\Sniffs\Security\NonceVerificationSniff */ final class NonceVerificationUnitTest extends AbstractSniffUnitTest {