• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

10up / wp_mock / 5878415280

16 Aug 2023 11:55AM UTC coverage: 66.312% (+0.7%) from 65.646%
5878415280

push

github

web-flow
Update `WP_Mock::expectHookNotAdded()` with new params (#234)

<!--
Filling out the required portions of this template is mandatory. 
Any PR that does not include enough information to be reviewed may be
closed at a maintainers' discretion.
All new code requires associated documentation and unit tests.
-->

# Summary <!-- Required -->

Adds method params to `expectHookNotAdded` for priority and allowed
arguments that are symmetrical to `expectHookAdded` - and carries them
over the aliased functions for filter and action hooks.

This way, we can set more specific expectations than assuming hooks will
always have `10, 1` as priority/args allowed.

The PR also improves the WP_Mock hook methods phpdocs and types to match
current standards.

### Closes: #233 

## Contributor checklist <!-- Required -->

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you are unsure about any of these, please ask for
clarification. We are here to help! -->

- [x] I agree to follow this project's [**Code of
Conduct**](https://github.com/10up/.github/blob/trunk/CODE_OF_CONDUCT.md).
- [x] I have updated the documentation accordingly 
- [x] I have added tests to cover changes introduced by this pull
request
- [x] All new and existing tests pass

### Reviewer checklist <!-- Required -->

<!-- The following checklist is for the reviewer: add any steps that may
be relevant while reviewing this pull request -->

- [x] Code changes review
- [ ] Documentation changes review
- [x] Unit tests pass
- [x] Static analysis passes

17 of 17 new or added lines in 2 files covered. (100.0%)

498 of 751 relevant lines covered (66.31%)

2.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.0
/php/WP_Mock/API/function-mocks.php
1
<?php
2

3
use PHPUnit\Framework\ExpectationFailedException;
4
use WP_Mock\Functions\Handler;
5

6
if (! function_exists('add_action')) {
×
7
    /**
8
     * Hooks a function on to a specific action.
9
     *
10
     * Actions are the hooks that WordPress launches at specific points during execution, or when specific events occur.
11
     * Plugins can specify that one or more of its PHP functions are executed at these points, using the Action API.
12
     *
13
     * @link https://developer.wordpress.org/plugins/hooks/actions/
14
     *
15
     * @param string $tag the name of the action to which the $function_to_add is hooked
16
     * @param string|callable-string|callable $functionToAdd the name of the function you wish to be called
17
     * @param int $priority optional, used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action
18
     * @param int $acceptedArgs the number of arguments the function accept (default 1)
19
     */
20
    function add_action(string $tag, $functionToAdd, int $priority = 10, int $acceptedArgs = 1)
21
    {
22
        \WP_Mock::onActionAdded($tag)->react($functionToAdd, $priority, $acceptedArgs);
×
23
    }
24
}
25

26
if (! function_exists('do_action')) {
×
27
    /**
28
     * Execute functions hooked on a specific action hook.
29
     *
30
     * @param string $tag     The name of the action to be executed.
31
     * @param mixed  $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
32
     *
33
     * @return null Will return null if $tag does not exist in $wp_filter array
34
     */
35
    function do_action($tag, $arg = '')
36
    {
37
        $args = func_get_args();
×
38
        $args = array_slice($args, 1);
×
39

40
        return \WP_Mock::onAction($tag)->react($args);
×
41
    }
42
}
43

44
if (! function_exists('add_filter')) {
×
45
    /**
46
     * Hooks a function on to a specific filter.
47
     *
48
     * Filters are the hooks that WordPress uses to alter the value of a variable at specific points during execution.
49
     * Plugins can specify that one or more of its PHP functions are executed at these points, using the Filter API, to change the value of that variable.
50
     *
51
     * @link https://developer.wordpress.org/plugins/hooks/filters/
52
     *
53
     * @param string $tag the name of the action to which the $function_to_add is hooked
54
     * @param string|callable-string|callable $functionToAdd the name of the function you wish to be called
55
     * @param int $priority optional, used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action
56
     * @param int $acceptedArgs the number of arguments the function accept (default 1)
57
     */
58
    function add_filter(string $tag, $functionToAdd, int $priority = 10, int $acceptedArgs = 1)
59
    {
60
        \WP_Mock::onFilterAdded($tag)->react($functionToAdd, $priority, $acceptedArgs);
×
61
    }
62
}
63

64
if (! function_exists('apply_filters')) {
×
65
    /**
66
     * Call the functions added to a filter hook.
67
     *
68
     * @param string $tag     The name of the filter hook.
69
     * @param mixed  $value   The value on which the filters hooked to <tt>$tag</tt> are applied on.
70
     * @param mixed  $var,... Additional variables passed to the functions hooked to <tt>$tag</tt>.
71
     *
72
     * @return mixed The filtered value after all hooked functions are applied to it.
73
     */
74
    function apply_filters($tag, $value)
75
    {
76
        $args    = func_get_args();
×
77
        $args    = array_slice($args, 1);
×
78
        $args[0] = $value;
×
79

80
        return \WP_Mock::onFilter($tag)->apply($args);
×
81
    }
82
}
83

84
if (! function_exists('esc_html')) {
×
85
    /**
86
     * @return string|mixed
87
     * @throws ExpectationFailedException|Exception
88
     */
89
    function esc_html()
90
    {
91
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
1✔
92
    }
93
}
94

95
if (! function_exists('esc_attr')) {
×
96
    /**
97
     * @return string|mixed
98
     * @throws ExpectationFailedException|Exception
99
     */
100
    function esc_attr()
101
    {
102
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
1✔
103
    }
104
}
105

106
if (! function_exists('esc_url')) {
×
107
    /**
108
     * @return string|mixed
109
     * @throws ExpectationFailedException|Exception
110
     */
111
    function esc_url()
112
    {
113
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
1✔
114
    }
115
}
116

117
if (! function_exists('esc_url_raw')) {
×
118
    /**
119
     * @return string|mixed
120
     * @throws ExpectationFailedException|Exception
121
     */
122
    function esc_url_raw()
123
    {
124
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
1✔
125
    }
126
}
127

128
if (! function_exists('esc_js')) {
×
129
    /**
130
     * @return string|mixed
131
     * @throws ExpectationFailedException|Exception
132
     */
133
    function esc_js()
134
    {
135
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
1✔
136
    }
137
}
138

139
if (! function_exists('esc_textarea')) {
×
140
    /**
141
     * @return string|mixed
142
     * @throws ExpectationFailedException|Exception
143
     */
144
    function esc_textarea()
145
    {
146
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
1✔
147
    }
148
}
149

150
if (! function_exists('__')) {
×
151
    /**
152
     * @return string|mixed
153
     * @throws ExpectationFailedException|Exception
154
     */
155
    function __()
156
    {
157
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
1✔
158
    }
159
}
160

161
if (! function_exists('_e')) {
×
162
    /**
163
     * @return void
164
     * @throws ExpectationFailedException|Exception
165
     */
166
    function _e(): void
167
    {
168
        Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args());
1✔
169
    }
170
}
171

172
if (! function_exists('_x')) {
×
173
    /**
174
     * @return string|mixed
175
     * @throws ExpectationFailedException|Exception
176
     */
177
    function _x()
178
    {
179
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
1✔
180
    }
181
}
182

183
if (! function_exists('esc_html__')) {
×
184
    /**
185
     * @return string|mixed
186
     * @throws ExpectationFailedException|Exception
187
     */
188
    function esc_html__()
189
    {
190
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
1✔
191
    }
192
}
193

194
if (! function_exists('esc_html_e')) {
×
195
    /**
196
     * @return void
197
     * @throws ExpectationFailedException|Exception
198
     */
199
    function esc_html_e(): void
200
    {
201
        Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args());
1✔
202
    }
203
}
204

205
if (! function_exists('esc_html_x')) {
×
206
    /**
207
     * @return string|mixed
208
     * @throws ExpectationFailedException|Exception
209
     */
210
    function esc_html_x()
211
    {
212
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
×
213
    }
214
}
215

216
if (! function_exists('esc_attr__')) {
×
217
    /**
218
     * @return string|mixed
219
     * @throws ExpectationFailedException|Exception
220
     */
221
    function esc_attr__()
222
    {
223
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
1✔
224
    }
225
}
226

227
if (! function_exists('esc_attr_e')) {
×
228
    /**
229
     * @return void
230
     * @throws ExpectationFailedException|Exception
231
     */
232
    function esc_attr_e(): void
233
    {
234
        Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args());
1✔
235
    }
236
}
237

238
if (! function_exists('esc_attr_x')) {
×
239
    /**
240
     * @return string|mixed
241
     * @throws ExpectationFailedException
242
     */
243
    function esc_attr_x()
244
    {
245
        return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
1✔
246
    }
247
}
248

249
if (! function_exists('_n')) {
×
250
    /**
251
     * @return string|mixed singular or plural string based on number
252
     * @throws ExpectationFailedException if too few arguments passed
253
     */
254
    function _n()
255
    {
256
        $args = func_get_args();
1✔
257

258
        if (count($args) >= 3) {
1✔
259
            /** @phpstan-ignore-next-line */
260
            if (isset($args[0]) && 1 >= intval($args[2])) {
1✔
261
                return $args[0];
1✔
262
            } else {
263
                return $args[1];
1✔
264
            }
265
        } else {
266
            throw new ExpectationFailedException(sprintf('Too few arguments to function %s', __FUNCTION__));
×
267
        }
268
    }
269
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc