forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.feature
More file actions
223 lines (195 loc) · 6.09 KB
/
hook.feature
File metadata and controls
223 lines (195 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
Feature: Tests `WP_CLI::add_hook()`
Scenario: Add callback to the `before_invoke:plugin list`
Given a WP installation
And a before-invoke.php file:
"""
<?php
$callback = function() {
WP_CLI::log( '`add_hook()` to the `before_invoke` is working.');
};
WP_CLI::add_hook( 'before_invoke:plugin list', $callback );
"""
And a wp-cli.yml file:
"""
require:
- before-invoke.php
"""
When I run `wp plugin list`
Then STDOUT should contain:
"""
`add_hook()` to the `before_invoke` is working.
"""
And the return code should be 0
# `wp db check` does not yet work on SQLite,
# See https://github.com/wp-cli/db-command/issues/234
@require-mysql
Scenario: Add callback to the `before_invoke:db check`
Given a WP installation
And a before-invoke.php file:
"""
<?php
$callback = function() {
WP_CLI::log( '`add_hook()` to the `before_invoke` is working.');
};
WP_CLI::add_hook( 'before_invoke:db check', $callback );
"""
And a wp-cli.yml file:
"""
require:
- before-invoke.php
"""
When I run `wp db check`
Then STDOUT should contain:
"""
`add_hook()` to the `before_invoke` is working.
"""
And the return code should be 0
Scenario: Add callback to the `before_invoke:core version`
Given a WP installation
And a before-invoke.php file:
"""
<?php
$callback = function() {
WP_CLI::log( '`add_hook()` to the `before_invoke` is working.');
};
WP_CLI::add_hook( 'before_invoke:core version', $callback );
"""
And a wp-cli.yml file:
"""
require:
- before-invoke.php
"""
When I run `wp core version`
Then STDOUT should contain:
"""
`add_hook()` to the `before_invoke` is working.
"""
And the return code should be 0
Scenario: Add callback to the `before_run_command` with args
Given a WP installation
And a before-run-command.php file:
"""
<?php
$callback = function ( $args, $assoc_args, $options ) {
WP_CLI::log( '`add_hook()` to the `before_run_command` is working.' );
if ( 'version' !== $args[1] ) {
WP_CLI::error( 'Arg context not being passed in to callback properly' );
}
if ( ! array_key_exists( 'extra', $assoc_args ) ) {
WP_CLI::error( 'Assoc arg context not being passed in to callback properly' );
}
};
WP_CLI::add_hook( 'before_run_command', $callback );
"""
And a wp-cli.yml file:
"""
require:
- before-run-command.php
"""
When I run `wp core version --extra`
Then STDOUT should contain:
"""
`add_hook()` to the `before_run_command` is working.
"""
And the return code should be 0
Scenario: Use return value of a callback hook
Given a WP installation
And a custom-hook.php file:
"""
<?php
$callback = function ( $first, $second ) {
WP_CLI::log( '`add_hook()` to the `custom_hook` is working.' );
if ( 'value1' !== $first ) {
WP_CLI::error( 'First argument is not being passed in to callback properly' );
}
if ( 'value2' !== $second ) {
WP_CLI::error( 'Second argument is not being passed in to callback properly' );
}
return 'value3';
};
WP_CLI::add_hook( 'custom_hook', $callback );
$result = WP_CLI::do_hook( 'custom_hook', 'value1', 'value2' );
if ( empty( $result ) ) {
WP_CLI::error( 'First argument is not returned via do_hook()' );
}
if ( 'value3' !== $result ) {
WP_CLI::error( 'First argument is not mutable via do_hook()' );
}
"""
And a wp-cli.yml file:
"""
require:
- custom-hook.php
"""
When I run `wp cli version`
Then STDOUT should contain:
"""
`add_hook()` to the `custom_hook` is working.
"""
Then STDOUT should not contain:
"""
First argument is not being passed in to callback properly
"""
And STDOUT should not contain:
"""
Second argument is not being passed in to callback properly
"""
And STDOUT should not contain:
"""
First argument is not returned via do_hook()
"""
And STDOUT should not contain:
"""
First argument is not mutable via do_hook()
"""
And the return code should be 0
Scenario: Callback hook with arguments does not break on bad callback
Given a WP installation
And a custom-hook.php file:
"""
<?php
$callback = function ( $first, $second ) {
WP_CLI::log( '`add_hook()` to the `custom_hook` is working.' );
if ( 'value1' !== $first ) {
WP_CLI::error( 'First argument is not being passed in to callback properly' );
}
if ( 'value2' !== $second ) {
WP_CLI::error( 'Second argument is not being passed in to callback properly' );
}
};
WP_CLI::add_hook( 'custom_hook', $callback );
$result = WP_CLI::do_hook( 'custom_hook', 'value1', 'value2' );
if ( empty( $result ) ) {
WP_CLI::error( 'First argument is not returned via do_hook()' );
}
if ( 'value1' !== $result ) {
WP_CLI::error( 'First argument is not correctly returned on bad callback missing return' );
}
"""
And a wp-cli.yml file:
"""
require:
- custom-hook.php
"""
When I run `wp cli version`
Then STDOUT should contain:
"""
`add_hook()` to the `custom_hook` is working.
"""
Then STDOUT should not contain:
"""
First argument is not being passed in to callback properly
"""
And STDOUT should not contain:
"""
Second argument is not being passed in to callback properly
"""
And STDOUT should not contain:
"""
First argument is not returned via do_hook()
"""
And STDOUT should not contain:
"""
First argument is not correctly returned on bad callback missing return
"""
And the return code should be 0