From a6000dcdee1aa518ba5a20d404ee69302a3c7539 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 18 Aug 2025 15:00:58 -0300 Subject: [PATCH] ContextHelper::is_token_namespaced(): add basic tests --- .../IsTokenNamespacedUnitTest.inc | 17 ++++ .../IsTokenNamespacedUnitTest.php | 95 +++++++++++++++++++ .../Tests/WP/DiscouragedFunctionsUnitTest.php | 1 - 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 WordPress/Tests/Helpers/ContextHelper/IsTokenNamespacedUnitTest.inc create mode 100644 WordPress/Tests/Helpers/ContextHelper/IsTokenNamespacedUnitTest.php diff --git a/WordPress/Tests/Helpers/ContextHelper/IsTokenNamespacedUnitTest.inc b/WordPress/Tests/Helpers/ContextHelper/IsTokenNamespacedUnitTest.inc new file mode 100644 index 0000000000..40262226de --- /dev/null +++ b/WordPress/Tests/Helpers/ContextHelper/IsTokenNamespacedUnitTest.inc @@ -0,0 +1,17 @@ +assertFalse( ContextHelper::is_token_namespaced( self::$phpcsFile, -1 ) ); + } + + /** + * Test is_token_namespaced(). + * + * @dataProvider dataIsTokenNamespaced + * + * @param string $testMarker The comment which prefaces the target token in the test file. + * @param bool $expectedResult The expected return value. + * @param string $tokenContent The token content to use for the target token. + * + * @return void + */ + public function testIsTokenNamespaced( $testMarker, $expectedResult, $tokenContent ) { + $stackPtr = $this->getTargetToken( $testMarker, \T_STRING, $tokenContent ); + $result = ContextHelper::is_token_namespaced( self::$phpcsFile, $stackPtr ); + + $this->assertSame( $expectedResult, $result ); + } + + /** + * Data provider. + * + * @see testIsTokenNamespaced() + * + * @return array> + */ + public static function dataIsTokenNamespaced() { + return array( + // Cases that should return false. + 'unqualified' => array( + 'testMarker' => '/* testUnqualified */', + 'expectedResult' => false, + 'tokenContent' => 'my_function', + ), + 'fully_qualified' => array( + 'testMarker' => '/* testFullyQualified */', + 'expectedResult' => false, + 'tokenContent' => 'MY_CONSTANT', + ), + + // Cases that should return true. + 'partially_qualified' => array( + 'testMarker' => '/* testPartiallyQualified */', + 'expectedResult' => true, + 'tokenContent' => 'my_function', + ), + 'fully_qualified_namespaced' => array( + 'testMarker' => '/* testFullyQualifiedNamespaced */', + 'expectedResult' => true, + 'tokenContent' => 'MyClass', + ), + 'namespace_relative' => array( + 'testMarker' => '/* testNamespaceRelative */', + 'expectedResult' => true, + 'tokenContent' => 'MY_CONSTANT', + ), + 'namespace_relative_sub' => array( + 'testMarker' => '/* testNamespaceRelativeSub */', + 'expectedResult' => true, + 'tokenContent' => 'my_function', + ), + ); + } +} diff --git a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php index 67031257c3..1bfa8533b7 100644 --- a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php +++ b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php @@ -19,7 +19,6 @@ * * @covers \WordPressCS\WordPress\AbstractFunctionRestrictionsSniff * @covers \WordPressCS\WordPress\Helpers\ContextHelper::has_object_operator_before - * @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_token_namespaced * @covers \WordPressCS\WordPress\Sniffs\WP\DiscouragedFunctionsSniff */ final class DiscouragedFunctionsUnitTest extends AbstractSniffUnitTest {