-
-
Notifications
You must be signed in to change notification settings - Fork 521
ContextHelper::is_in_function_call(): add basic tests #2626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rodrigoprimo
wants to merge
3
commits into
WordPress:develop
Choose a base branch
from
rodrigoprimo:is-in-function-call-tests
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
WordPress/Util/Tests/Helpers/ContextHelper/IsInFunctionCallUnitTest.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* Make sure that tokens inside a function call are correctly identified using the default values | ||
* for the optional parameters (`$global_function = true` and `$allow_nested = false`). | ||
* | ||
* The below should *NOT* be recognized as inside a function call to one of the valid functions. | ||
*/ | ||
|
||
$a = /* test return false 1 */ 'foo'; | ||
another_function( /* test return false 2 */ $a ); | ||
MyNamespace\my_function( /* test return false 3 */ $a ); | ||
\MyNamespace\my_function( /* test return false 4 */ $a ); | ||
namespace\MyNamespace\my_function( /* test return false 5 */ $a ); | ||
my_function( another_function( /* test return false 6 */ $a ) ); | ||
$anon_func = function() { | ||
/* test return false 7 */ $a = 'foo'; | ||
}; | ||
$my_function( /* test return false 8 */ $a ); | ||
|
||
/* | ||
* Make sure that tokens inside a function call are correctly identified using the default values | ||
* for the optional parameters (`$global_function = true` and `$allow_nested = false`). | ||
* | ||
* The below should be recognized as inside a function call to one of the valid functions. | ||
*/ | ||
|
||
/* test function call 1 */ my_function( /* test inside function pointer 1 */ $a ); | ||
/* test function call 2 */ MY_FUNCTION( /* test inside function pointer 2 */ $a ); | ||
/* test function call 3 */ \my_function( /* test inside function pointer 3 */ $a ); | ||
|
||
/* | ||
* Make sure that tokens inside a function call are correctly identified when `$global_function` is | ||
* set to false. | ||
* | ||
* The below should be recognized as inside a function call to one of the valid functions. | ||
*/ | ||
|
||
/* test function call 4 */ my_function( /* test inside function pointer 4 */ $a ); | ||
/* test function call 5 */ MY_FUNCTION( /* test inside function pointer 5 */ $a ); | ||
/* test function call 6 */ \my_function( /* test inside function pointer 6 */ $a ); | ||
MyNamespace\/* test function call 7 */my_function( /* test inside function pointer 7 */ $a ); | ||
\MyNamespace\/* test function call 8 */my_function( /* test inside function pointer 8 */ $a ); | ||
namespace\MyNamespace\/* test function call 9 */my_function( /* test inside function pointer 9 */ $a ); | ||
MyClass::/* test function call 10 */my_function( /* test inside function pointer 10 */ $a ); | ||
/* test function call 11 */ $obj->my_function( /* test inside function pointer 11 */ $a ); | ||
/* test function call 12 */ $obj?->my_function( /* test inside function pointer 12 */ $a ); | ||
|
||
/* | ||
* Make sure that tokens inside a function call are correctly identified when `$allow_nested` is | ||
* set to true. | ||
* | ||
* The below should be recognized as inside a function call to one of the valid functions. | ||
*/ | ||
/* test function call 13 */ my_function( another_function( /* test inside function pointer 13 */ $a ) ); | ||
another_function( /* test function call 14 */ my_function( /* test inside function pointer 14 */ $a ) ); | ||
/* test function call 15 */ my_function( middle_function( inner_function( /* test inside function pointer 15 */ $a ) ) ); |
165 changes: 165 additions & 0 deletions
165
WordPress/Util/Tests/Helpers/ContextHelper/IsInFunctionCallUnitTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
/** | ||
* WordPress Coding Standard. | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* @link https://github.com/WordPress/WordPress-Coding-Standards | ||
* @license https://opensource.org/licenses/MIT MIT | ||
*/ | ||
|
||
namespace WordPressCS\WordPress\Util\Tests\Helpers\ContextHelper; | ||
|
||
use PHPCSUtils\Tokens\Collections; | ||
use WordPressCS\WordPress\Helpers\ContextHelper; | ||
use PHPCSUtils\TestUtils\UtilityMethodTestCase; | ||
|
||
/** | ||
* Tests for the `ContextHelper::is_in_function_call()` utility method. | ||
* | ||
* @since 3.3.0 | ||
* | ||
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_function_call() | ||
*/ | ||
final class IsInFunctionCallUnitTest extends UtilityMethodTestCase { | ||
|
||
|
||
/** | ||
* Test is_in_function_call() returns false if given token is not inside a function call | ||
* or not inside one of the expected function calls. | ||
* | ||
* @dataProvider dataIsInFunctionCallShouldReturnFalse | ||
* | ||
* @param string $commentString The comment which prefaces the target token in the test file. | ||
* @param int|string $tokenType The token type to search for. | ||
* | ||
* @return void | ||
*/ | ||
public function testIsInFunctionCallShouldReturnFalse( $commentString, $tokenType ) { | ||
$stackPtr = $this->getTargetToken( $commentString, $tokenType ); | ||
$result = ContextHelper::is_in_function_call( self::$phpcsFile, $stackPtr, array( 'my_function' => true ) ); | ||
$this->assertFalse( $result ); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array | ||
* @see testIsInFunctionCallShouldReturnFalse() | ||
*/ | ||
public static function dataIsInFunctionCallShouldReturnFalse() { | ||
return array( | ||
array( '/* test return false 1 */', \T_CONSTANT_ENCAPSED_STRING ), | ||
array( '/* test return false 2 */', \T_VARIABLE ), | ||
array( '/* test return false 3 */', \T_VARIABLE ), | ||
array( '/* test return false 4 */', \T_VARIABLE ), | ||
array( '/* test return false 5 */', \T_VARIABLE ), | ||
array( '/* test return false 6 */', \T_VARIABLE ), | ||
array( '/* test return false 7 */', \T_VARIABLE ), | ||
array( '/* test return false 8 */', \T_VARIABLE ), | ||
); | ||
} | ||
|
||
/** | ||
* Test is_in_function_call() returns pointer to function name if given token is inside a function call. | ||
* | ||
* @dataProvider dataIsInFunctionCallShouldReturnFunctionPointer | ||
* | ||
* @param string $insideFunctionCommentString The comment which prefaces the target token inside a function in the test file. | ||
* @param int|string $insideFunctionTokenType The token type to search for. | ||
* @param string $functionCallCommentString The comment which prefaces the function call token in the test file. | ||
* | ||
* @return void | ||
*/ | ||
public function testIsInFunctionCallShouldReturnFunctionPointer( $insideFunctionCommentString, $insideFunctionTokenType, $functionCallCommentString ) { | ||
$insideFunctionPtr = $this->getTargetToken( $insideFunctionCommentString, $insideFunctionTokenType ); | ||
$functionNamePtr = $this->getTargetToken( $functionCallCommentString, Collections::nameTokens() ); | ||
$result = ContextHelper::is_in_function_call( self::$phpcsFile, $insideFunctionPtr, array( 'my_function' => true ) ); | ||
$this->assertSame( $result, $functionNamePtr ); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array | ||
* @see testIsInFunctionCallShouldReturnFunctionPointer() | ||
*/ | ||
public static function dataIsInFunctionCallShouldReturnFunctionPointer() { | ||
return array( | ||
array( '/* test inside function pointer 1 */', \T_VARIABLE, '/* test function call 1 */' ), | ||
array( '/* test inside function pointer 2 */', \T_VARIABLE, '/* test function call 2 */' ), | ||
array( '/* test inside function pointer 3 */', \T_VARIABLE, '/* test function call 3 */' ), | ||
); | ||
} | ||
|
||
/** | ||
* Test is_in_function_call() returns pointer to function name if given token is inside a | ||
* function call when `$global_functions` is set to false. | ||
* | ||
* @dataProvider dataIsInFunctionCallShouldReturnFunctionPointerWhenGlobalIsFalse | ||
* | ||
* @param string $insideFunctionCommentString The comment which prefaces the target token inside a function in the test file. | ||
* @param int|string $insideFunctionTokenType The token type to search for. | ||
* @param string $functionCallCommentString The comment which prefaces the function call token in the test file. | ||
* | ||
* @return void | ||
*/ | ||
public function testIsInFunctionCallShouldReturnFunctionPointerWhenGlobalIsFalse( $insideFunctionCommentString, $insideFunctionTokenType, $functionCallCommentString ) { | ||
$insideFunctionPtr = $this->getTargetToken( $insideFunctionCommentString, $insideFunctionTokenType ); | ||
$functionNamePtr = $this->getTargetToken( $functionCallCommentString, Collections::nameTokens() ); | ||
$result = ContextHelper::is_in_function_call( self::$phpcsFile, $insideFunctionPtr, array( 'my_function' => true ), false ); | ||
$this->assertSame( $result, $functionNamePtr ); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array | ||
* @see testIsInFunctionCallShouldReturnFunctionPointerWhenGlobalIsFalse() | ||
*/ | ||
public static function dataIsInFunctionCallShouldReturnFunctionPointerWhenGlobalIsFalse() { | ||
return array( | ||
array( '/* test inside function pointer 4 */', \T_VARIABLE, '/* test function call 4 */' ), | ||
array( '/* test inside function pointer 5 */', \T_VARIABLE, '/* test function call 5 */' ), | ||
array( '/* test inside function pointer 6 */', \T_VARIABLE, '/* test function call 6 */' ), | ||
array( '/* test inside function pointer 7 */', \T_VARIABLE, '/* test function call 7 */' ), | ||
array( '/* test inside function pointer 8 */', \T_VARIABLE, '/* test function call 8 */' ), | ||
array( '/* test inside function pointer 9 */', \T_VARIABLE, '/* test function call 9 */' ), | ||
array( '/* test inside function pointer 10 */', \T_VARIABLE, '/* test function call 10 */' ), | ||
array( '/* test inside function pointer 11 */', \T_VARIABLE, '/* test function call 11 */' ), | ||
array( '/* test inside function pointer 12 */', \T_VARIABLE, '/* test function call 12 */' ), | ||
); | ||
} | ||
|
||
/** | ||
* Test is_in_function_call() returns pointer to function name if given token is inside a | ||
* function call when `$allow_nested` is set to true. | ||
* | ||
* @dataProvider dataIsInFunctionCallShouldReturnFunctionPointerWhenAllowNestedIsTrue | ||
* | ||
* @param string $insideFunctionCommentString The comment which prefaces the target token inside a function in the test file. | ||
* @param int|string $insideFunctionTokenType The token type to search for. | ||
* @param string $functionCallCommentString The comment which prefaces the function call token in the test file. | ||
* | ||
* @return void | ||
*/ | ||
public function testIsInFunctionCallShouldReturnFunctionPointerWhenAllowNestedIsTrue( $insideFunctionCommentString, $insideFunctionTokenType, $functionCallCommentString ) { | ||
$insideFunctionPtr = $this->getTargetToken( $insideFunctionCommentString, $insideFunctionTokenType ); | ||
$functionNamePtr = $this->getTargetToken( $functionCallCommentString, Collections::nameTokens() ); | ||
$result = ContextHelper::is_in_function_call( self::$phpcsFile, $insideFunctionPtr, array( 'my_function' => true ), true, true ); | ||
$this->assertSame( $result, $functionNamePtr ); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array | ||
* @see testIsInFunctionCallShouldReturnFunctionPointerWhenAllowNestedIsTrue() | ||
*/ | ||
public static function dataIsInFunctionCallShouldReturnFunctionPointerWhenAllowNestedIsTrue() { | ||
return array( | ||
array( '/* test inside function pointer 13 */', \T_VARIABLE, '/* test function call 13 */' ), | ||
array( '/* test inside function pointer 14 */', \T_VARIABLE, '/* test function call 14 */' ), | ||
array( '/* test inside function pointer 15 */', \T_VARIABLE, '/* test function call 15 */' ), | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and below:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Do you happen to remember more details, or what you used instead? This seems to be working as I expect. Composer is running both PHPUnit commands. It will not run the second command if the first one fails, but I guess that is not a problem, or is this what you are referring to for some reason that I'm missing?
Running the tests in two separate commands is creating problems for the code coverage report. I'm looking into this now, and I will continue this part of the conversation when I reply to your other remark about creating two separate test suites.