Skip to content

Commit ab1167e

Browse files
committed
ContextHelper::is_in_function_call(): add basic tests
Since there were no previous tests for utility methods, it was also necessary to modify the test structure to enable running utility tests. They are different from the sniff tests as they extend `UtilityMethodTestCase` and don't need to run via `./vendor/squizlabs/php_codesniffer/tests/AllTests.php`.
1 parent 8e6628f commit ab1167e

File tree

5 files changed

+245
-3
lines changed

5 files changed

+245
-3
lines changed

Tests/bootstrap.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@
4545
) {
4646
require_once $phpcsDir . $ds . 'autoload.php';
4747
require_once $phpcsDir . $ds . 'tests' . $ds . 'bootstrap.php'; // PHPUnit 6.x+ support.
48+
49+
spl_autoload_register(
50+
function ( $className ) {
51+
// Only try & load our own classes.
52+
if ( stripos( $className, 'WordPressCS' ) !== 0 ) {
53+
return;
54+
}
55+
56+
$file = realpath( dirname( __DIR__ ) ) . DIRECTORY_SEPARATOR . strtr( str_replace( 'WordPressCS\\', '', $className ), '\\', DIRECTORY_SEPARATOR ) . '.php';
57+
58+
if ( file_exists( $file ) ) {
59+
include_once $file;
60+
}
61+
}
62+
);
4863
} else {
4964
echo 'Uh oh... can\'t find PHPCS.
5065
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* Make sure that tokens inside a function call are correctly identified using the default values
5+
* for the optional parameters (`$global_function = true` and `$allow_nested = false`).
6+
*
7+
* The below should *NOT* be recognized as inside a function call to one of the valid functions.
8+
*/
9+
10+
$a = /* test return false 1 */ 'foo';
11+
another_function( /* test return false 2 */ $a );
12+
MyNamespace\my_function( /* test return false 3 */ $a );
13+
\MyNamespace\my_function( /* test return false 4 */ $a );
14+
namespace\MyNamespace\my_function( /* test return false 5 */ $a );
15+
my_function( another_function( /* test return false 6 */ $a ) );
16+
$anon_func = function() {
17+
/* test return false 7 */ $a = 'foo';
18+
};
19+
$my_function( /* test return false 8 */ $a );
20+
21+
/*
22+
* Make sure that tokens inside a function call are correctly identified using the default values
23+
* for the optional parameters (`$global_function = true` and `$allow_nested = false`).
24+
*
25+
* The below should be recognized as inside a function call to one of the valid functions.
26+
*/
27+
28+
/* test function call 1 */ my_function( /* test inside function pointer 1 */ $a );
29+
/* test function call 2 */ MY_FUNCTION( /* test inside function pointer 2 */ $a );
30+
/* test function call 3 */ \my_function( /* test inside function pointer 3 */ $a );
31+
32+
/*
33+
* Make sure that tokens inside a function call are correctly identified when `$global_function` is
34+
* set to false.
35+
*
36+
* The below should be recognized as inside a function call to one of the valid functions.
37+
*/
38+
39+
/* test function call 4 */ my_function( /* test inside function pointer 4 */ $a );
40+
/* test function call 5 */ MY_FUNCTION( /* test inside function pointer 5 */ $a );
41+
/* test function call 6 */ \my_function( /* test inside function pointer 6 */ $a );
42+
MyNamespace\/* test function call 7 */my_function( /* test inside function pointer 7 */ $a );
43+
\MyNamespace\/* test function call 8 */my_function( /* test inside function pointer 8 */ $a );
44+
namespace\MyNamespace\/* test function call 9 */my_function( /* test inside function pointer 9 */ $a );
45+
MyClass::/* test function call 10 */my_function( /* test inside function pointer 10 */ $a );
46+
/* test function call 11 */ $obj->my_function( /* test inside function pointer 11 */ $a );
47+
/* test function call 12 */ $obj?->my_function( /* test inside function pointer 12 */ $a );
48+
49+
/*
50+
* Make sure that tokens inside a function call are correctly identified when `$allow_nested` is
51+
* set to true.
52+
*
53+
* The below should be recognized as inside a function call to one of the valid functions.
54+
*/
55+
/* test function call 13 */ my_function( another_function( /* test inside function pointer 13 */ $a ) );
56+
another_function( /* test function call 14 */ my_function( /* test inside function pointer 14 */ $a ) );
57+
/* test function call 15 */ my_function( middle_function( inner_function( /* test inside function pointer 15 */ $a ) ) );
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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_in_function_call()` utility method.
18+
*
19+
* @since 3.3.0
20+
*
21+
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_function_call()
22+
*/
23+
final class IsInFunctionCallUnitTest extends UtilityMethodTestCase {
24+
25+
26+
/**
27+
* Test is_in_function_call() returns false if given token is not inside a function call
28+
* or not inside one of the expected function calls.
29+
*
30+
* @dataProvider dataIsInFunctionCallShouldReturnFalse
31+
*
32+
* @param string $commentString The comment which prefaces the target token in the test file.
33+
* @param int|string $tokenType The token type to search for.
34+
*
35+
* @return void
36+
*/
37+
public function testIsInFunctionCallShouldReturnFalse( $commentString, $tokenType ) {
38+
$stackPtr = $this->getTargetToken( $commentString, $tokenType );
39+
$result = ContextHelper::is_in_function_call( self::$phpcsFile, $stackPtr, array( 'my_function' => true ) );
40+
$this->assertFalse( $result );
41+
}
42+
43+
/**
44+
* Data provider.
45+
*
46+
* @return array
47+
* @see testIsInFunctionCallShouldReturnFalse()
48+
*/
49+
public static function dataIsInFunctionCallShouldReturnFalse() {
50+
return array(
51+
array( '/* test return false 1 */', \T_CONSTANT_ENCAPSED_STRING ),
52+
array( '/* test return false 2 */', \T_VARIABLE ),
53+
array( '/* test return false 3 */', \T_VARIABLE ),
54+
array( '/* test return false 4 */', \T_VARIABLE ),
55+
array( '/* test return false 5 */', \T_VARIABLE ),
56+
array( '/* test return false 6 */', \T_VARIABLE ),
57+
array( '/* test return false 7 */', \T_VARIABLE ),
58+
array( '/* test return false 8 */', \T_VARIABLE ),
59+
);
60+
}
61+
62+
/**
63+
* Test is_in_function_call() returns pointer to function name if given token is inside a function call.
64+
*
65+
* @dataProvider dataIsInFunctionCallShouldReturnFunctionPointer
66+
*
67+
* @param string $insideFunctionCommentString The comment which prefaces the target token inside a function in the test file.
68+
* @param int|string $insideFunctionTokenType The token type to search for.
69+
* @param string $functionCallCommentString The comment which prefaces the function call token in the test file.
70+
*
71+
* @return void
72+
*/
73+
public function testIsInFunctionCallShouldReturnFunctionPointer( $insideFunctionCommentString, $insideFunctionTokenType, $functionCallCommentString ) {
74+
$insideFunctionPtr = $this->getTargetToken( $insideFunctionCommentString, $insideFunctionTokenType );
75+
$functionNamePtr = $this->getTargetToken( $functionCallCommentString, Collections::nameTokens() );
76+
$result = ContextHelper::is_in_function_call( self::$phpcsFile, $insideFunctionPtr, array( 'my_function' => true ) );
77+
$this->assertSame( $result, $functionNamePtr );
78+
}
79+
80+
/**
81+
* Data provider.
82+
*
83+
* @return array
84+
* @see testIsInFunctionCallShouldReturnFunctionPointer()
85+
*/
86+
public static function dataIsInFunctionCallShouldReturnFunctionPointer() {
87+
return array(
88+
array( '/* test inside function pointer 1 */', \T_VARIABLE, '/* test function call 1 */' ),
89+
array( '/* test inside function pointer 2 */', \T_VARIABLE, '/* test function call 2 */' ),
90+
array( '/* test inside function pointer 3 */', \T_VARIABLE, '/* test function call 3 */' ),
91+
);
92+
}
93+
94+
/**
95+
* Test is_in_function_call() returns pointer to function name if given token is inside a
96+
* function call when `$global_functions` is set to false.
97+
*
98+
* @dataProvider dataIsInFunctionCallShouldReturnFunctionPointerWhenGlobalIsFalse
99+
*
100+
* @param string $insideFunctionCommentString The comment which prefaces the target token inside a function in the test file.
101+
* @param int|string $insideFunctionTokenType The token type to search for.
102+
* @param string $functionCallCommentString The comment which prefaces the function call token in the test file.
103+
*
104+
* @return void
105+
*/
106+
public function testIsInFunctionCallShouldReturnFunctionPointerWhenGlobalIsFalse( $insideFunctionCommentString, $insideFunctionTokenType, $functionCallCommentString ) {
107+
$insideFunctionPtr = $this->getTargetToken( $insideFunctionCommentString, $insideFunctionTokenType );
108+
$functionNamePtr = $this->getTargetToken( $functionCallCommentString, Collections::nameTokens() );
109+
$result = ContextHelper::is_in_function_call( self::$phpcsFile, $insideFunctionPtr, array( 'my_function' => true ), false );
110+
$this->assertSame( $result, $functionNamePtr );
111+
}
112+
113+
/**
114+
* Data provider.
115+
*
116+
* @return array
117+
* @see testIsInFunctionCallShouldReturnFunctionPointerWhenGlobalIsFalse()
118+
*/
119+
public static function dataIsInFunctionCallShouldReturnFunctionPointerWhenGlobalIsFalse() {
120+
return array(
121+
array( '/* test inside function pointer 4 */', \T_VARIABLE, '/* test function call 4 */' ),
122+
array( '/* test inside function pointer 5 */', \T_VARIABLE, '/* test function call 5 */' ),
123+
array( '/* test inside function pointer 6 */', \T_VARIABLE, '/* test function call 6 */' ),
124+
array( '/* test inside function pointer 7 */', \T_VARIABLE, '/* test function call 7 */' ),
125+
array( '/* test inside function pointer 8 */', \T_VARIABLE, '/* test function call 8 */' ),
126+
array( '/* test inside function pointer 9 */', \T_VARIABLE, '/* test function call 9 */' ),
127+
array( '/* test inside function pointer 10 */', \T_VARIABLE, '/* test function call 10 */' ),
128+
array( '/* test inside function pointer 11 */', \T_VARIABLE, '/* test function call 11 */' ),
129+
array( '/* test inside function pointer 12 */', \T_VARIABLE, '/* test function call 12 */' ),
130+
);
131+
}
132+
133+
/**
134+
* Test is_in_function_call() returns pointer to function name if given token is inside a
135+
* function call when `$allow_nested` is set to true.
136+
*
137+
* @dataProvider dataIsInFunctionCallShouldReturnFunctionPointerWhenAllowNestedIsTrue
138+
*
139+
* @param string $insideFunctionCommentString The comment which prefaces the target token inside a function in the test file.
140+
* @param int|string $insideFunctionTokenType The token type to search for.
141+
* @param string $functionCallCommentString The comment which prefaces the function call token in the test file.
142+
*
143+
* @return void
144+
*/
145+
public function testIsInFunctionCallShouldReturnFunctionPointerWhenAllowNestedIsTrue( $insideFunctionCommentString, $insideFunctionTokenType, $functionCallCommentString ) {
146+
$insideFunctionPtr = $this->getTargetToken( $insideFunctionCommentString, $insideFunctionTokenType );
147+
$functionNamePtr = $this->getTargetToken( $functionCallCommentString, Collections::nameTokens() );
148+
$result = ContextHelper::is_in_function_call( self::$phpcsFile, $insideFunctionPtr, array( 'my_function' => true ), true, true );
149+
$this->assertSame( $result, $functionNamePtr );
150+
}
151+
152+
/**
153+
* Data provider.
154+
*
155+
* @return array
156+
* @see testIsInFunctionCallShouldReturnFunctionPointerWhenAllowNestedIsTrue()
157+
*/
158+
public static function dataIsInFunctionCallShouldReturnFunctionPointerWhenAllowNestedIsTrue() {
159+
return array(
160+
array( '/* test inside function pointer 13 */', \T_VARIABLE, '/* test function call 13 */' ),
161+
array( '/* test inside function pointer 14 */', \T_VARIABLE, '/* test function call 14 */' ),
162+
array( '/* test inside function pointer 15 */', \T_VARIABLE, '/* test function call 15 */' ),
163+
);
164+
}
165+
}

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@
5353
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf"
5454
],
5555
"run-tests": [
56-
"@php ./vendor/phpunit/phpunit/phpunit --filter WordPress ./vendor/squizlabs/php_codesniffer/tests/AllTests.php --no-coverage"
56+
"@php ./vendor/phpunit/phpunit/phpunit --filter WordPress ./vendor/squizlabs/php_codesniffer/tests/AllTests.php --no-coverage",
57+
"@php ./vendor/phpunit/phpunit/phpunit WordPress/Util/Tests/ --no-coverage"
5758
],
5859
"coverage": [
59-
"@php ./vendor/phpunit/phpunit/phpunit --filter WordPress ./vendor/squizlabs/php_codesniffer/tests/AllTests.php"
60+
"@php ./vendor/phpunit/phpunit/phpunit --filter WordPress ./vendor/squizlabs/php_codesniffer/tests/AllTests.php",
61+
"@php ./vendor/phpunit/phpunit/phpunit WordPress/Util/Tests/"
6062
],
6163
"check-complete": [
6264
"@php ./vendor/phpcsstandards/phpcsdevtools/bin/phpcs-check-feature-completeness -q ./WordPress"

phpunit.xml.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
forceCoversAnnotation="true">
1414

1515
<testsuites>
16-
<testsuite name="WordPress">
16+
<testsuite name="WordPress Utilities Tests">
17+
<directory suffix="UnitTest.php">./WordPress/Util/Tests/</directory>
18+
</testsuite>
19+
<testsuite name="WordPress Sniffs Tests">
1720
<directory suffix="UnitTest.php">./WordPress/Tests/</directory>
1821
</testsuite>
1922
</testsuites>

0 commit comments

Comments
 (0)