Skip to content

WPHookHelper::get_hook_name_param(): add basic tests#2727

Open
rodrigoprimo wants to merge 2 commits into
developfrom
get-hook-name-param-tests
Open

WPHookHelper::get_hook_name_param(): add basic tests#2727
rodrigoprimo wants to merge 2 commits into
developfrom
get-hook-name-param-tests

Conversation

@rodrigoprimo

@rodrigoprimo rodrigoprimo commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Description

In preparation for supporting PHPCS 4.0, this PR adds unit tests for the WPHookHelper::get_hook_name_param() method.

Suggested changelog entry

N/A

Related issues/external references

Related to: #2272

@jrfnl jrfnl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rodrigoprimo Thanks for creating this PR. I've left some small suggestions inline, but, and more importantly, a principle question about how the tests for this method are set up.

* @dataProvider dataGetHookNameParam
*
* @param string $functionName The function name to test.
* @param array<int, mixed> $parameters The parameters array to pass to the method.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param array<int, mixed> $parameters The parameters array to pass to the method.
* @param array<int|string, array<string>> $parameters The parameters array to pass to the method.

*
* @param string $functionName The function name to test.
* @param array<int, mixed> $parameters The parameters array to pass to the method.
* @param array<int, mixed>|false $expectedResult The expected return value.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param array<int, mixed>|false $expectedResult The expected return value.
* @param array<string>|false $expectedResult The expected return value.

*
* @see testGetHookNameParam()
*
* @return array<string, array<string, array<int, mixed>|false|string>>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return array<string, array<string, array<int, mixed>|false|string>>
* @return array<string, array<string, array<int|string, string|array<string>>|false|string>>

Comment on lines +50 to +51
// Note: The parameter arrays use a simplified format instead of the real PassedParameters::getParameters()
// output as most of the array entries are not relevant for the method under test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of emulating the return value from PassedParameters::getParameters(), why not test the method with a case file and then let the test code call PassedParameters::getParameters() before calling WPHookHelper::get_hook_name_param() ?

Comment on lines +58 to +62
'hook_name_param_missing' => array(
'functionName' => 'apply_filters_ref_array',
'parameters' => array( 2 => array( '$arg' ) ),
'expectedResult' => false,
),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unrealistic test case as you can never have a parameters array with a second (positional) parameter, but not a first.

"Hook name param missing" is something which can only occur in function calls with named parameters.

@rodrigoprimo

Copy link
Copy Markdown
Contributor Author

Thanks for the review, Juliette!

Instead of emulating the return value from PassedParameters::getParameters(), why not test the method with a case file and then let the test code call PassedParameters::getParameters() before calling WPHookHelper::get_hook_name_param() ?

I reworked the tests in a new commit to address your feedback:

  • The tests now parse a case file and call PassedParameters::getParameters() in the test code before passing the result to WPHookHelper::get_hook_name_param(), instead of emulating that output.
  • I opted to assert only on the raw content of the selected parameter, since verifying which parameter is selected is the method's main purpose, and it keeps the tests less brittle.

I also addressed the other comments you left. The unrealistic "hook name param missing" case was replaced with a named-parameter call that omits the hook name parameter, and the docblock types were updated to reflect the new structure after refactoring the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants