Skip to content

Commit a6000dc

Browse files
committed
ContextHelper::is_token_namespaced(): add basic tests
1 parent adaf62d commit a6000dc

3 files changed

Lines changed: 112 additions & 1 deletion

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* The below should NOT be recognized as namespaced tokens.
5+
*/
6+
7+
/* testUnqualified */ my_function();
8+
/* testFullyQualified */ $const = \MY_CONSTANT;
9+
10+
/*
11+
* The below should be recognized as namespaced tokens.
12+
*/
13+
14+
/* testPartiallyQualified */ MyNamespace\my_function();
15+
/* testFullyQualifiedNamespaced */ $var = new \MyNamespace\MyClass();
16+
/* testNamespaceRelative */ $const = namespace\MY_CONSTANT; // This is a false positive. The method should return false for this once it can resolve relative namespaces.
17+
/* testNamespaceRelativeSub */ namespace\Sub\my_function();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 PHPCSUtils\TestUtils\UtilityMethodTestCase;
13+
use WordPressCS\WordPress\Helpers\ContextHelper;
14+
15+
/**
16+
* Tests for the `ContextHelper::is_token_namespaced()` utility method.
17+
*
18+
* @since 3.4.0
19+
*
20+
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_token_namespaced
21+
*/
22+
final class IsTokenNamespacedUnitTest extends UtilityMethodTestCase {
23+
24+
/**
25+
* Test is_token_namespaced() returns false if the token does not exist.
26+
*
27+
* @return void
28+
*/
29+
public function testIsTokenNamespacedReturnsFalseIfTokenDoesNotExist() {
30+
$this->assertFalse( ContextHelper::is_token_namespaced( self::$phpcsFile, -1 ) );
31+
}
32+
33+
/**
34+
* Test is_token_namespaced().
35+
*
36+
* @dataProvider dataIsTokenNamespaced
37+
*
38+
* @param string $testMarker The comment which prefaces the target token in the test file.
39+
* @param bool $expectedResult The expected return value.
40+
* @param string $tokenContent The token content to use for the target token.
41+
*
42+
* @return void
43+
*/
44+
public function testIsTokenNamespaced( $testMarker, $expectedResult, $tokenContent ) {
45+
$stackPtr = $this->getTargetToken( $testMarker, \T_STRING, $tokenContent );
46+
$result = ContextHelper::is_token_namespaced( self::$phpcsFile, $stackPtr );
47+
48+
$this->assertSame( $expectedResult, $result );
49+
}
50+
51+
/**
52+
* Data provider.
53+
*
54+
* @see testIsTokenNamespaced()
55+
*
56+
* @return array<string, array<string, bool|string>>
57+
*/
58+
public static function dataIsTokenNamespaced() {
59+
return array(
60+
// Cases that should return false.
61+
'unqualified' => array(
62+
'testMarker' => '/* testUnqualified */',
63+
'expectedResult' => false,
64+
'tokenContent' => 'my_function',
65+
),
66+
'fully_qualified' => array(
67+
'testMarker' => '/* testFullyQualified */',
68+
'expectedResult' => false,
69+
'tokenContent' => 'MY_CONSTANT',
70+
),
71+
72+
// Cases that should return true.
73+
'partially_qualified' => array(
74+
'testMarker' => '/* testPartiallyQualified */',
75+
'expectedResult' => true,
76+
'tokenContent' => 'my_function',
77+
),
78+
'fully_qualified_namespaced' => array(
79+
'testMarker' => '/* testFullyQualifiedNamespaced */',
80+
'expectedResult' => true,
81+
'tokenContent' => 'MyClass',
82+
),
83+
'namespace_relative' => array(
84+
'testMarker' => '/* testNamespaceRelative */',
85+
'expectedResult' => true,
86+
'tokenContent' => 'MY_CONSTANT',
87+
),
88+
'namespace_relative_sub' => array(
89+
'testMarker' => '/* testNamespaceRelativeSub */',
90+
'expectedResult' => true,
91+
'tokenContent' => 'my_function',
92+
),
93+
);
94+
}
95+
}

WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*
2020
* @covers \WordPressCS\WordPress\AbstractFunctionRestrictionsSniff
2121
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::has_object_operator_before
22-
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_token_namespaced
2322
* @covers \WordPressCS\WordPress\Sniffs\WP\DiscouragedFunctionsSniff
2423
*/
2524
final class DiscouragedFunctionsUnitTest extends AbstractSniffUnitTest {

0 commit comments

Comments
 (0)