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