Skip to content

Commit cf2a41f

Browse files
committed
WPHookHelper::get_hook_name_param(): add basic tests
1 parent adaf62d commit cf2a41f

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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\Tests\Helpers\WPHookHelper;
11+
12+
use PHPUnit\Framework\TestCase;
13+
use WordPressCS\WordPress\Helpers\WPHookHelper;
14+
15+
/**
16+
* Tests for the `WPHookHelper::get_hook_name_param()` method.
17+
*
18+
* @since 3.4.0
19+
*
20+
* @covers \WordPressCS\WordPress\Helpers\WPHookHelper::get_hook_name_param
21+
*/
22+
final class GetHookNameParamUnitTest extends TestCase {
23+
24+
/**
25+
* Test get_hook_name_param().
26+
*
27+
* @dataProvider dataGetHookNameParam
28+
*
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.
32+
*
33+
* @return void
34+
*/
35+
public function testGetHookNameParam( $functionName, $parameters, $expectedResult ) {
36+
$this->assertSame(
37+
$expectedResult,
38+
WPHookHelper::get_hook_name_param( $functionName, $parameters )
39+
);
40+
}
41+
42+
/**
43+
* Data provider.
44+
*
45+
* @see testGetHookNameParam()
46+
*
47+
* @return array<string, array<string, array<int, mixed>|false|string>>
48+
*/
49+
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+
return array(
53+
'not_a_hook_function' => array(
54+
'functionName' => 'printf',
55+
'parameters' => array( 1 => array( 'my_action' ) ),
56+
'expectedResult' => false,
57+
),
58+
'hook_name_param_missing' => array(
59+
'functionName' => 'apply_filters_ref_array',
60+
'parameters' => array( 2 => array( '$arg' ) ),
61+
'expectedResult' => false,
62+
),
63+
'lowercase_name' => array(
64+
'functionName' => 'do_action',
65+
'parameters' => array( 1 => array( 'my_action' ) ),
66+
'expectedResult' => array( 'my_action' ),
67+
),
68+
'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' ),
75+
),
76+
'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' ),
83+
),
84+
);
85+
}
86+
}

WordPress/Tests/NamingConventions/ValidHookNameUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @since 0.10.0
1818
* @since 0.13.0 Class name changed: this class is now namespaced.
1919
*
20-
* @covers \WordPressCS\WordPress\Helpers\WPHookHelper
20+
* @covers \WordPressCS\WordPress\Helpers\WPHookHelper::get_functions
2121
* @covers \WordPressCS\WordPress\Sniffs\NamingConventions\ValidHookNameSniff
2222
*/
2323
final class ValidHookNameUnitTest extends AbstractSniffUnitTest {

0 commit comments

Comments
 (0)