|
9 | 9 |
|
10 | 10 | namespace WordPressCS\WordPress\Tests\Helpers\WPHookHelper; |
11 | 11 |
|
12 | | -use PHPUnit\Framework\TestCase; |
| 12 | +use PHPCSUtils\TestUtils\UtilityMethodTestCase; |
| 13 | +use PHPCSUtils\Utils\PassedParameters; |
13 | 14 | use WordPressCS\WordPress\Helpers\WPHookHelper; |
14 | 15 |
|
15 | 16 | /** |
|
19 | 20 | * |
20 | 21 | * @covers \WordPressCS\WordPress\Helpers\WPHookHelper::get_hook_name_param |
21 | 22 | */ |
22 | | -final class GetHookNameParamUnitTest extends TestCase { |
| 23 | +final class GetHookNameParamUnitTest extends UtilityMethodTestCase { |
23 | 24 |
|
24 | 25 | /** |
25 | 26 | * Test get_hook_name_param(). |
26 | 27 | * |
27 | 28 | * @dataProvider dataGetHookNameParam |
28 | 29 | * |
29 | | - * @param string $functionName The function name to test. |
30 | | - * @param array<int, mixed> $parameters The parameters array to pass to the method. |
31 | | - * @param array<int, mixed>|false $expectedResult The expected return value. |
| 30 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 31 | + * @param string|false $expectedResult The raw content of the expected hook name parameter, |
| 32 | + * or `false` when no hook name parameter is expected. |
32 | 33 | * |
33 | 34 | * @return void |
34 | 35 | */ |
35 | | - public function testGetHookNameParam( $functionName, $parameters, $expectedResult ) { |
36 | | - $this->assertSame( |
37 | | - $expectedResult, |
38 | | - WPHookHelper::get_hook_name_param( $functionName, $parameters ) |
39 | | - ); |
| 36 | + public function testGetHookNameParam( $testMarker, $expectedResult ) { |
| 37 | + $stackPtr = $this->getTargetToken( $testMarker, \T_STRING ); |
| 38 | + $functionName = self::$phpcsFile->getTokens()[ $stackPtr ]['content']; |
| 39 | + $parameters = PassedParameters::getParameters( self::$phpcsFile, $stackPtr ); |
| 40 | + |
| 41 | + $result = WPHookHelper::get_hook_name_param( $functionName, $parameters ); |
| 42 | + |
| 43 | + if ( is_array( $result ) ) { |
| 44 | + // The details of the parameter are populated by PassedParameters::getParameters(). |
| 45 | + // Here we only verify which parameter was selected. |
| 46 | + $result = $result['raw']; |
| 47 | + } |
| 48 | + |
| 49 | + $this->assertSame( $expectedResult, $result ); |
40 | 50 | } |
41 | 51 |
|
42 | 52 | /** |
43 | 53 | * Data provider. |
44 | 54 | * |
45 | 55 | * @see testGetHookNameParam() |
46 | 56 | * |
47 | | - * @return array<string, array<string, array<int, mixed>|false|string>> |
| 57 | + * @return array<string, array<string, string|false>> |
48 | 58 | */ |
49 | 59 | public static function dataGetHookNameParam() { |
50 | | - // Note: The parameter arrays use a simplified format instead of the real PassedParameters::getParameters() |
51 | | - // output as most of the array entries are not relevant for the method under test. |
52 | 60 | return array( |
53 | 61 | 'not_a_hook_function' => array( |
54 | | - 'functionName' => 'printf', |
55 | | - 'parameters' => array( 1 => array( 'my_action' ) ), |
| 62 | + 'testMarker' => '/* testNotAHookFunction */', |
56 | 63 | 'expectedResult' => false, |
57 | 64 | ), |
58 | 65 | 'hook_name_param_missing' => array( |
59 | | - 'functionName' => 'apply_filters_ref_array', |
60 | | - 'parameters' => array( 2 => array( '$arg' ) ), |
| 66 | + 'testMarker' => '/* testHookNameParamMissing */', |
61 | 67 | 'expectedResult' => false, |
62 | 68 | ), |
63 | 69 | 'lowercase_name' => array( |
64 | | - 'functionName' => 'do_action', |
65 | | - 'parameters' => array( 1 => array( 'my_action' ) ), |
66 | | - 'expectedResult' => array( 'my_action' ), |
| 70 | + 'testMarker' => '/* testLowercaseName */', |
| 71 | + 'expectedResult' => "'my_action'", |
67 | 72 | ), |
68 | 73 | 'mixedcase_name' => array( |
69 | | - 'functionName' => 'aPpLy_FiLtErS', |
70 | | - 'parameters' => array( |
71 | | - 1 => array( 'my_filter' ), |
72 | | - 2 => array( '$value' ), |
73 | | - ), |
74 | | - 'expectedResult' => array( 'my_filter' ), |
| 74 | + 'testMarker' => '/* testMixedCaseName */', |
| 75 | + 'expectedResult' => "'my_filter'", |
75 | 76 | ), |
76 | 77 | 'named_parameter' => array( |
77 | | - 'functionName' => 'do_action_deprecated', |
78 | | - 'parameters' => array( |
79 | | - 'args' => array( '$args' ), |
80 | | - 'hook_name' => array( 'my_action' ), |
81 | | - ), |
82 | | - 'expectedResult' => array( 'my_action' ), |
| 78 | + 'testMarker' => '/* testNamedParameter */', |
| 79 | + 'expectedResult' => "'my_action'", |
83 | 80 | ), |
84 | 81 | ); |
85 | 82 | } |
|
0 commit comments