Skip to content

Commit acd5b10

Browse files
committed
Rework tests to use a case file as suggested during PR review
1 parent cf2a41f commit acd5b10

2 files changed

Lines changed: 53 additions & 32 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* The below should result in a false return value.
5+
*/
6+
7+
/* testNotAHookFunction */
8+
printf( 'my_action' );
9+
10+
/* testHookNameParamMissing */
11+
apply_filters_ref_array( args: $args );
12+
13+
/*
14+
* The below should result in the information on the hook name param being returned.
15+
*/
16+
17+
/* testLowercaseName */
18+
do_action( 'my_action' );
19+
20+
/* testMixedCaseName */
21+
aPpLy_FiLtErS( 'my_filter', $value );
22+
23+
/* testNamedParameter */
24+
do_action_deprecated( args: $args, hook_name: 'my_action' );

WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.php

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
namespace WordPressCS\WordPress\Tests\Helpers\WPHookHelper;
1111

12-
use PHPUnit\Framework\TestCase;
12+
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
13+
use PHPCSUtils\Utils\PassedParameters;
1314
use WordPressCS\WordPress\Helpers\WPHookHelper;
1415

1516
/**
@@ -19,67 +20,63 @@
1920
*
2021
* @covers \WordPressCS\WordPress\Helpers\WPHookHelper::get_hook_name_param
2122
*/
22-
final class GetHookNameParamUnitTest extends TestCase {
23+
final class GetHookNameParamUnitTest extends UtilityMethodTestCase {
2324

2425
/**
2526
* Test get_hook_name_param().
2627
*
2728
* @dataProvider dataGetHookNameParam
2829
*
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.
3233
*
3334
* @return void
3435
*/
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 );
4050
}
4151

4252
/**
4353
* Data provider.
4454
*
4555
* @see testGetHookNameParam()
4656
*
47-
* @return array<string, array<string, array<int, mixed>|false|string>>
57+
* @return array<string, array<string, string|false>>
4858
*/
4959
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.
5260
return array(
5361
'not_a_hook_function' => array(
54-
'functionName' => 'printf',
55-
'parameters' => array( 1 => array( 'my_action' ) ),
62+
'testMarker' => '/* testNotAHookFunction */',
5663
'expectedResult' => false,
5764
),
5865
'hook_name_param_missing' => array(
59-
'functionName' => 'apply_filters_ref_array',
60-
'parameters' => array( 2 => array( '$arg' ) ),
66+
'testMarker' => '/* testHookNameParamMissing */',
6167
'expectedResult' => false,
6268
),
6369
'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'",
6772
),
6873
'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'",
7576
),
7677
'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'",
8380
),
8481
);
8582
}

0 commit comments

Comments
 (0)