Skip to content

Commit 73fc40f

Browse files
committed
WIP ContextHelper::is_token_namespaced(): add basic tests
Check the way the tests are structured. There is for sure room to improve their organization.
1 parent 7d07928 commit 73fc40f

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* The below should NOT be recognized as namespaced tokens by ContextHelper::is_token_namespaced().
5+
*/
6+
7+
/* test return false 1 */ my_function();
8+
/* test return false 2 */ \my_function();
9+
/* test return false 3 */ MyClass::my_static_method();
10+
/* test return false 4 */ $var = new \MyClass();
11+
/* test return false 5 */ $const = MY_CONSTANT;
12+
/* test return false 6 */ $const = \MY_CONSTANT;
13+
14+
/*
15+
* The below should be recognized as namespaced tokens by ContextHelper::is_token_namespaced().
16+
*/
17+
18+
/* test return true 1 */ MyNamespace\my_function();
19+
/* test return true 2 */ \MyNamespace\my_function();
20+
/* test return true 3 */ namespace\my_function(); // False positive if the file is not namespaced.
21+
/* test return true 4 */ $var = new MyNamespace\MyClass();
22+
/* test return true 5 */ $const = MyNamespace\MY_CONSTANT;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\Util\Tests\Helpers\ContextHelper;
11+
12+
use PHPCSUtils\Tokens\Collections;
13+
use WordPressCS\WordPress\Helpers\ContextHelper;
14+
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
15+
16+
/**
17+
* Tests for the `ContextHelper::is_token_namespaced()` utility method.
18+
*
19+
* @since 3.3.0
20+
*
21+
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_token_namespaced()
22+
*/
23+
final class IsTokenNamespacedUnitTest extends UtilityMethodTestCase {
24+
25+
/**
26+
* Test is_token_namespaced() returns false if the token does not exist.
27+
*
28+
* @return void
29+
*/
30+
public function testIsTokenNamespacedReturnFalseIfTokenDoesntExist() {
31+
$this->assertFalse( ContextHelper::is_token_namespaced( self::$phpcsFile, -1 ) );
32+
}
33+
34+
/**
35+
* Test is_token_namespaced() returns false for non-namespaced contexts.
36+
*
37+
* @dataProvider dataIsTokenNamespacedShouldReturnFalse
38+
*
39+
* @param string $commentString The comment which prefaces the target token in the test file.
40+
* @param string $tokenContent The token content to use for the target token.
41+
*
42+
* @return void
43+
*/
44+
public function testIsTokenNamespacedShouldReturnFalse( $commentString, $tokenContent ) {
45+
$stackPtr = $this->getTargetToken( $commentString, Collections::nameTokens(), $tokenContent );
46+
$this->assertFalse( ContextHelper::is_token_namespaced( self::$phpcsFile, $stackPtr ) );
47+
}
48+
49+
/**
50+
* Data provider.
51+
*
52+
* @return array
53+
* @see testIsTokenNamespacedShouldReturnFalse()
54+
*/
55+
public static function dataIsTokenNamespacedShouldReturnFalse() {
56+
return array(
57+
array( '/* test return false 1 */', 'my_function' ),
58+
array( '/* test return false 2 */', 'my_function' ),
59+
array( '/* test return false 3 */', 'MyClass' ),
60+
array( '/* test return false 4 */', 'MyClass' ),
61+
array( '/* test return false 5 */', 'MY_CONSTANT' ),
62+
array( '/* test return false 6 */', 'MY_CONSTANT' ),
63+
);
64+
}
65+
66+
/**
67+
* Test is_token_namespaced() returns true for namespaced contexts.
68+
*
69+
* @dataProvider dataIsTokenNamespacedShouldReturnTrue
70+
*
71+
* @param string $commentString The comment which prefaces the target token in the test file.
72+
* @param string $tokenContent The token content to use for the target token.
73+
*
74+
* @return void
75+
*/
76+
public function testIsTokenNamespacedShouldReturnTrue( $commentString, $tokenContent ) {
77+
$stackPtr = $this->getTargetToken( $commentString, Collections::nameTokens(), $tokenContent );
78+
$this->assertTrue( ContextHelper::is_token_namespaced( self::$phpcsFile, $stackPtr ) );
79+
}
80+
81+
/**
82+
* Data provider.
83+
*
84+
* @return array
85+
* @see testIsTokenNamespacedShouldReturnTrue()
86+
*/
87+
public static function dataIsTokenNamespacedShouldReturnTrue() {
88+
return array(
89+
array( '/* test return true 1 */', 'my_function' ),
90+
array( '/* test return true 2 */', 'my_function' ),
91+
array( '/* test return true 3 */', 'my_function' ),
92+
array( '/* test return true 4 */', 'MyClass' ),
93+
array( '/* test return true 5 */', 'MY_CONSTANT' ),
94+
);
95+
}
96+
}

0 commit comments

Comments
 (0)