|
| 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 | +} |
0 commit comments