Skip to content

Commit 416ae66

Browse files
committed
ContextHelper::is_in_type_test(): add tests
1 parent 8643436 commit 416ae66

3 files changed

Lines changed: 131 additions & 1 deletion

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* The below should NOT be recognized as being inside a type test function call.
5+
*/
6+
7+
some_function( /* testOtherFunctionCall */ $value );
8+
MyNamespace\is_numeric( /* testPartiallyQualifiedFunction */ $value );
9+
\MyNamespace\is_array( /* testFullyQualifiedNamespacedFunction */ $value );
10+
namespace\is_bool( /* testNamespaceRelativeFunction */ $value ); // The method should start recognizing this as a type test function once it can resolve relative namespaces.
11+
namespace\Sub\is_float( /* testNamespaceRelativeSubFunction */ $value );
12+
$obj->is_string( /* testObjectMethod */ $value );
13+
$obj?->is_object( /* testNullsafeObjectMethod */ $value );
14+
TypeChecker::is_int( /* testStaticMethod */ $value );
15+
is_scalar( my_function( /* testNestedFunctionCall */ $value ) );
16+
17+
/*
18+
* The below should be recognized as being inside a type test function call.
19+
*/
20+
21+
is_array( /* testUnqualifiedFunction */ $value );
22+
Is_Bool( /* testMixedCaseFunction */ $value );
23+
\is_string( /* testFullyQualifiedFunction */ $value );
24+
\IS_NUMERIC( /* testFullyQualifiedUpperCaseFunction */ $value );
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* WordPress Coding Standard.
4+
*
5+
* @package WPCS\WordPressCodingStandards
6+
* @link https://github.com/WordPress/WordPress-Coding-Standards
7+
* @license https://opensource.org/licenses/MIT MIT
8+
*/
9+
10+
namespace WordPressCS\WordPress\Tests\Helpers\ContextHelper;
11+
12+
use WordPressCS\WordPress\Helpers\ContextHelper;
13+
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
14+
15+
/**
16+
* Tests for the `ContextHelper::is_in_type_test()` utility method.
17+
*
18+
* @since 3.4.0
19+
*
20+
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_type_test
21+
*/
22+
final class IsInTypeTestUnitTest extends UtilityMethodTestCase {
23+
24+
/**
25+
* Test is_in_type_test().
26+
*
27+
* @dataProvider dataIsInTypeTest
28+
*
29+
* @param string $testMarker The comment which prefaces the target token in the test file.
30+
* @param bool $expectedResult The expected return value.
31+
*
32+
* @return void
33+
*/
34+
public function testIsInTypeTest( $testMarker, $expectedResult ) {
35+
$stackPtr = $this->getTargetToken( $testMarker, \T_VARIABLE );
36+
$result = ContextHelper::is_in_type_test( self::$phpcsFile, $stackPtr );
37+
38+
$this->assertSame( $expectedResult, $result );
39+
}
40+
41+
/**
42+
* Data provider.
43+
*
44+
* @see testIsInTypeTest()
45+
*
46+
* @return array<string, array<string, bool|string>>
47+
*/
48+
public static function dataIsInTypeTest() {
49+
return array(
50+
// Cases that should return false.
51+
'other_function_call' => array(
52+
'testMarker' => '/* testOtherFunctionCall */',
53+
'expectedResult' => false,
54+
),
55+
'partially_qualified_function' => array(
56+
'testMarker' => '/* testPartiallyQualifiedFunction */',
57+
'expectedResult' => false,
58+
),
59+
'fully_qualified_namespaced_function' => array(
60+
'testMarker' => '/* testFullyQualifiedNamespacedFunction */',
61+
'expectedResult' => false,
62+
),
63+
'namespace_relative_function' => array(
64+
'testMarker' => '/* testNamespaceRelativeFunction */',
65+
'expectedResult' => false,
66+
),
67+
'namespace_relative_sub_function' => array(
68+
'testMarker' => '/* testNamespaceRelativeSubFunction */',
69+
'expectedResult' => false,
70+
),
71+
'object_method' => array(
72+
'testMarker' => '/* testObjectMethod */',
73+
'expectedResult' => false,
74+
),
75+
'nullsafe_object_method' => array(
76+
'testMarker' => '/* testNullsafeObjectMethod */',
77+
'expectedResult' => false,
78+
),
79+
'static_method' => array(
80+
'testMarker' => '/* testStaticMethod */',
81+
'expectedResult' => false,
82+
),
83+
'nested_function_call' => array(
84+
'testMarker' => '/* testNestedFunctionCall */',
85+
'expectedResult' => false,
86+
),
87+
88+
// Cases that should return true.
89+
'unqualified_function' => array(
90+
'testMarker' => '/* testUnqualifiedFunction */',
91+
'expectedResult' => true,
92+
),
93+
'mixed_case_function' => array(
94+
'testMarker' => '/* testMixedCaseFunction */',
95+
'expectedResult' => true,
96+
),
97+
'fully_qualified_function' => array(
98+
'testMarker' => '/* testFullyQualifiedFunction */',
99+
'expectedResult' => true,
100+
),
101+
'fully_qualified_upper_case_function' => array(
102+
'testMarker' => '/* testFullyQualifiedUpperCaseFunction */',
103+
'expectedResult' => true,
104+
),
105+
);
106+
}
107+
}

WordPress/Tests/Security/NonceVerificationUnitTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
* @since 0.13.0 Class name changed: this class is now namespaced.
1919
* @since 1.0.0 This sniff has been moved from the `CSRF` category to the `Security` category.
2020
*
21-
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_type_test
2221
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_isset_or_empty
2322
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_array_comparison
2423
* @covers \WordPressCS\WordPress\Sniffs\Security\NonceVerificationSniff

0 commit comments

Comments
 (0)