-
-
Notifications
You must be signed in to change notification settings - Fork 519
ContextHelper::is_in_array_comparison(): add basic tests #2726
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
Merged
+161
−1
Merged
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions
30
WordPress/Tests/Helpers/ContextHelper/IsInArrayComparisonUnitTest.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,30 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * The below should NOT be recognized as being inside an array comparison function call. | ||
| */ | ||
|
|
||
| /* testBareVariable */ $value; | ||
| some_function( /* testOtherFunctionCall */ $value ); | ||
| MyNamespace\in_array( /* testPartiallyQualifiedFunction */ $value, $haystack ); | ||
| \MyNamespace\array_search( /* testFullyQualifiedNamespacedFunction */ $value, $haystack ); | ||
| namespace\array_keys( $array, /* testNamespaceRelativeFunction */ $value ); // The method should start recognizing this as an array comparison function once it can resolve relative namespaces. | ||
| namespace\Sub\in_array( /* testNamespaceRelativeSubFunction */ $value, $haystack ); | ||
| $obj->array_search( /* testObjectMethod */ $value, $haystack ); | ||
| $obj?->array_keys( $array, /* testNullsafeObjectMethod */ $value ); | ||
| ArrayHelper::in_array( /* testStaticMethod */ $value, $haystack ); | ||
| array_keys( /* testArrayKeysFirstParamOnly */ $array ); | ||
| array_keys( array: /* testArrayKeysWrongNamedParam */ $array, search_value: 'value', strict: true ); | ||
|
|
||
| /* | ||
| * The below should be recognized as being inside an array comparison function call. | ||
| */ | ||
|
|
||
| in_array( /* testInArray */ $value, $haystack ); | ||
| array_search( /* testArraySearch */ $value, $haystack ); | ||
| array_keys( $array, /* testArrayKeysWithFilterValue */ $value ); | ||
| array_keys( filter_value: /* testArrayKeysNamedParam */ $value, array: $array ); | ||
| IN_array( /* testMixedCaseFunction */ $value, $haystack ); | ||
| \array_search( /* testFullyQualifiedFunction */ $value, $haystack ); | ||
| \ARRAY_KEYS( $array, /* testFullyQualifiedUpperCaseFunction */ $value ); | ||
| in_array( my_function( /* testNestedFunctionCall */ $value ), $haystack ); | ||
131 changes: 131 additions & 0 deletions
131
WordPress/Tests/Helpers/ContextHelper/IsInArrayComparisonUnitTest.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,131 @@ | ||
| <?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\Tests\Helpers\ContextHelper; | ||
|
|
||
| use PHPCSUtils\TestUtils\UtilityMethodTestCase; | ||
| use WordPressCS\WordPress\Helpers\ContextHelper; | ||
|
|
||
| /** | ||
| * Tests for the `ContextHelper::is_in_array_comparison()` utility method. | ||
| * | ||
| * @since 3.4.0 | ||
| * | ||
| * @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_array_comparison | ||
| */ | ||
| final class IsInArrayComparisonUnitTest extends UtilityMethodTestCase { | ||
|
|
||
| /** | ||
| * Test is_in_array_comparison(). | ||
| * | ||
| * @dataProvider dataIsInArrayComparison | ||
| * | ||
| * @param string $testMarker The comment which prefaces the target token in the test file. | ||
| * @param bool $expectedResult The expected return value. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testIsInArrayComparison( $testMarker, $expectedResult ) { | ||
| $stackPtr = $this->getTargetToken( $testMarker, \T_VARIABLE ); | ||
| $result = ContextHelper::is_in_array_comparison( self::$phpcsFile, $stackPtr ); | ||
|
|
||
| $this->assertSame( $expectedResult, $result ); | ||
| } | ||
|
|
||
| /** | ||
| * Data provider. | ||
| * | ||
| * @see testIsInArrayComparison() | ||
| * | ||
| * @return array<string, array<string, bool|string>> | ||
| */ | ||
| public static function dataIsInArrayComparison() { | ||
| return array( | ||
| // Cases that should return false. | ||
| 'bare_variable' => array( | ||
| 'testMarker' => '/* testBareVariable */', | ||
| 'expectedResult' => false, | ||
| ), | ||
| 'other_function_call' => array( | ||
| 'testMarker' => '/* testOtherFunctionCall */', | ||
| 'expectedResult' => false, | ||
| ), | ||
| 'partially_qualified_function' => array( | ||
| 'testMarker' => '/* testPartiallyQualifiedFunction */', | ||
| 'expectedResult' => false, | ||
| ), | ||
| 'fully_qualified_namespaced_function' => array( | ||
| 'testMarker' => '/* testFullyQualifiedNamespacedFunction */', | ||
| 'expectedResult' => false, | ||
| ), | ||
| 'namespace_relative_function' => array( | ||
| 'testMarker' => '/* testNamespaceRelativeFunction */', | ||
| 'expectedResult' => false, | ||
| ), | ||
| 'namespace_relative_sub_function' => array( | ||
| 'testMarker' => '/* testNamespaceRelativeSubFunction */', | ||
| 'expectedResult' => false, | ||
| ), | ||
| 'object_method' => array( | ||
| 'testMarker' => '/* testObjectMethod */', | ||
| 'expectedResult' => false, | ||
| ), | ||
| 'nullsafe_object_method' => array( | ||
| 'testMarker' => '/* testNullsafeObjectMethod */', | ||
| 'expectedResult' => false, | ||
| ), | ||
| 'static_method' => array( | ||
| 'testMarker' => '/* testStaticMethod */', | ||
| 'expectedResult' => false, | ||
| ), | ||
| 'array_keys_first_param_only' => array( | ||
| 'testMarker' => '/* testArrayKeysFirstParamOnly */', | ||
| 'expectedResult' => false, | ||
| ), | ||
| 'array_keys_wrong_named_param' => array( | ||
| 'testMarker' => '/* testArrayKeysWrongNamedParam */', | ||
| 'expectedResult' => false, | ||
| ), | ||
|
|
||
| // Cases that should return true. | ||
| 'in_array' => array( | ||
| 'testMarker' => '/* testInArray */', | ||
| 'expectedResult' => true, | ||
| ), | ||
| 'array_search' => array( | ||
| 'testMarker' => '/* testArraySearch */', | ||
| 'expectedResult' => true, | ||
| ), | ||
| 'array_keys_with_filter_value' => array( | ||
| 'testMarker' => '/* testArrayKeysWithFilterValue */', | ||
| 'expectedResult' => true, | ||
| ), | ||
| 'array_keys_named_param' => array( | ||
| 'testMarker' => '/* testArrayKeysNamedParam */', | ||
| 'expectedResult' => true, | ||
| ), | ||
| 'mixed_case_function' => array( | ||
| 'testMarker' => '/* testMixedCaseFunction */', | ||
| 'expectedResult' => true, | ||
| ), | ||
| 'fully_qualified_function' => array( | ||
| 'testMarker' => '/* testFullyQualifiedFunction */', | ||
| 'expectedResult' => true, | ||
| ), | ||
| 'fully_qualified_upper_case_function' => array( | ||
| 'testMarker' => '/* testFullyQualifiedUpperCaseFunction */', | ||
| 'expectedResult' => true, | ||
| ), | ||
| 'nested_function_call' => array( | ||
| 'testMarker' => '/* testNestedFunctionCall */', | ||
| 'expectedResult' => true, | ||
| ), | ||
| ); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.