@@ -13,6 +13,17 @@ trait ThenStepDefinitions {
1313 use Support;
1414
1515 /**
16+ * Expect a specific exit code of the previous command.
17+ *
18+ * ```
19+ * Scenario: My example scenario
20+ * Given a WP installation
21+ * When I try `wp plugin install`
22+ * Then the return code should be 1
23+ * ```
24+ *
25+ * @access public
26+ *
1627 * @Then /^the return code should( not)? be (\d+)$/
1728 */
1829 public function then_the_return_code_should_be ( $ not , $ return_code ) {
@@ -25,6 +36,26 @@ public function then_the_return_code_should_be( $not, $return_code ) {
2536 }
2637
2738 /**
39+ * Check the contents of STDOUT or STDERR.
40+ *
41+ * ```
42+ * Scenario: My example scenario
43+ * Given an empty directory
44+ * When I run `wp core is-installed`
45+ * Then STDOUT should be empty
46+ *
47+ * Scenario: My other scenario
48+ * Given a WP install
49+ * When I run `wp plugin install akismet`
50+ * Then STDOUT should contain:
51+ * """
52+ * Plugin installed successfully.
53+ * """
54+ * AND STDERR should be empty
55+ * ```
56+ *
57+ * @access public
58+ *
2859 * @Then /^(STDOUT|STDERR) should( strictly)? (be|contain|not contain):$/
2960 */
3061 public function then_stdout_stderr_should_contain ( $ stream , $ strictly , $ action , PyStringNode $ expected ) {
@@ -37,6 +68,17 @@ public function then_stdout_stderr_should_contain( $stream, $strictly, $action,
3768 }
3869
3970 /**
71+ * Expect STDOUT or STDERR to be a numeric value.
72+ *
73+ * ```
74+ * Scenario: My example scenario
75+ * Given a WP installation
76+ * When I run `wp db size --size_format=b`
77+ * Then STDOUT should be a number
78+ * ```
79+ *
80+ * @access public
81+ *
4082 * @Then /^(STDOUT|STDERR) should be a number$/
4183 */
4284 public function then_stdout_stderr_should_be_a_number ( $ stream ) {
@@ -47,6 +89,17 @@ public function then_stdout_stderr_should_be_a_number( $stream ) {
4789 }
4890
4991 /**
92+ * Expect STDOUT or STDERR to not be a numeric value.
93+ *
94+ * ```
95+ * Scenario: My example scenario
96+ * Given a WP installation
97+ * When I run `wp post list --format=json`
98+ * Then STDOUT should not be a number
99+ * ```
100+ *
101+ * @access public
102+ *
50103 * @Then /^(STDOUT|STDERR) should not be a number$/
51104 */
52105 public function then_stdout_stderr_should_not_be_a_number ( $ stream ) {
@@ -57,6 +110,20 @@ public function then_stdout_stderr_should_not_be_a_number( $stream ) {
57110 }
58111
59112 /**
113+ * Expect STDOUT to be a table containing the given rows.
114+ *
115+ * ```
116+ * Scenario: My example scenario
117+ * Given a WP installation
118+ * When I run `wp config list --fields=name,type`
119+ * Then STDOUT should be a table containing rows:
120+ * | name | type |
121+ * | DB_NAME | constant |
122+ * | DB_USER | constant |
123+ * ```
124+ *
125+ * @access public
126+ *
60127 * @Then /^STDOUT should be a table containing rows:$/
61128 */
62129 public function then_stdout_should_be_a_table_containing_rows ( TableNode $ expected ) {
@@ -72,6 +139,27 @@ public function then_stdout_should_be_a_table_containing_rows( TableNode $expect
72139 }
73140
74141 /**
142+ * Expect STDOUT to end with a table containing the given rows.
143+ *
144+ * Useful when the table is preceded by some other output.
145+ *
146+ * ```
147+ * Scenario: My example scenario
148+ * Given a WP installation
149+ * When I run `wp search-replace foo bar --report-changed-only`
150+ * Then STDOUT should contain:
151+ * """
152+ * Success: Made 3 replacements.
153+ * """
154+ * And STDOUT should end with a table containing rows:
155+ * | Table | Column | Replacements | Type |
156+ * | wp_options | option_value | 1 | PHP |
157+ * | wp_postmeta | meta_value | 1 | SQL |
158+ * | wp_posts | post_title | 1 | SQL |
159+ * ```
160+ *
161+ * @access public
162+ *
75163 * @Then /^STDOUT should end with a table containing rows:$/
76164 */
77165 public function then_stdout_should_end_with_a_table_containing_rows ( TableNode $ expected ) {
@@ -93,6 +181,21 @@ public function then_stdout_should_end_with_a_table_containing_rows( TableNode $
93181 }
94182
95183 /**
184+ * Expect valid JSON output in STDOUT.
185+ *
186+ * ```
187+ * Scenario: My example scenario
188+ * When I run `wp post meta get 1 meta-key --format=json`
189+ * Then STDOUT should be JSON containing:
190+ * """
191+ * {
192+ * "foo": "baz"
193+ * }
194+ * """
195+ * ```
196+ *
197+ * @access public
198+ *
96199 * @Then /^STDOUT should be JSON containing:$/
97200 */
98201 public function then_stdout_should_be_json_containing ( PyStringNode $ expected ) {
@@ -105,6 +208,21 @@ public function then_stdout_should_be_json_containing( PyStringNode $expected )
105208 }
106209
107210 /**
211+ * Expect valid JSON array output in STDOUT.
212+ *
213+ * Errors when some items are missing from the expected array.
214+ *
215+ * ```
216+ * Scenario: My example scenario
217+ * When I run `wp plugin list --field=name --format=json`
218+ * Then STDOUT should be a JSON array containing:
219+ * """
220+ * ["akismet", "hello-dolly"]
221+ * """
222+ * ```
223+ *
224+ * @access public
225+ *
108226 * @Then /^STDOUT should be a JSON array containing:$/
109227 */
110228 public function then_stdout_should_be_a_json_array_containing ( PyStringNode $ expected ) {
@@ -121,6 +239,18 @@ public function then_stdout_should_be_a_json_array_containing( PyStringNode $exp
121239 }
122240
123241 /**
242+ * Expect STDOUT to be CSV containing certain values.
243+ *
244+ * ```
245+ * Scenario: My example scenario
246+ * When I run `wp term list post_tag --fields=name,slug --format=csv`
247+ * Then STDOUT should be CSV containing:
248+ * | name | slug |
249+ * | Test term | test |
250+ * ```
251+ *
252+ * @access public
253+ *
124254 * @Then /^STDOUT should be CSV containing:$/
125255 */
126256 public function then_stdout_should_be_csv_containing ( TableNode $ expected ) {
@@ -139,6 +269,21 @@ public function then_stdout_should_be_csv_containing( TableNode $expected ) {
139269 }
140270
141271 /**
272+ * Expect STDOUT to be YAML containig certain content.
273+ *
274+ * ```
275+ * Scenario: My example scenario
276+ * When I run `wp cli alias list`
277+ * Then STDOUT should be YAML containing:
278+ * """
279+ * @all: Run command against every registered alias.
280+ * @foo:
281+ * path: {TEST_DIR}/foo
282+ * """
283+ * ```
284+ *
285+ * @access public
286+ *
142287 * @Then /^STDOUT should be YAML containing:$/
143288 */
144289 public function then_stdout_should_be_yaml_containing ( PyStringNode $ expected ) {
@@ -151,6 +296,17 @@ public function then_stdout_should_be_yaml_containing( PyStringNode $expected )
151296 }
152297
153298 /**
299+ * Expect STDOUT or STDERR to be empty.
300+ *
301+ * ```
302+ * Scenario: My other scenario
303+ * Given a WP install
304+ * When I run `wp plugin install akismet`
305+ * Then STDERR should be empty
306+ * ```
307+ *
308+ * @access public
309+ *
154310 * @Then /^(STDOUT|STDERR) should be empty$/
155311 */
156312 public function then_stdout_stderr_should_be_empty ( $ stream ) {
@@ -163,6 +319,16 @@ public function then_stdout_stderr_should_be_empty( $stream ) {
163319 }
164320
165321 /**
322+ * Expect STDOUT or STDERR not to be empty.
323+ *
324+ * ```
325+ * Scenario: My example scenario
326+ * When I run `wp user create examplejane [email protected] ` 327+ * Then STDOUT should not be empty
328+ * ```
329+ *
330+ * @access public
331+ *
166332 * @Then /^(STDOUT|STDERR) should not be empty$/
167333 */
168334 public function then_stdout_stderr_should_not_be_empty ( $ stream ) {
@@ -175,6 +341,17 @@ public function then_stdout_stderr_should_not_be_empty( $stream ) {
175341 }
176342
177343 /**
344+ * Expect STDOUT or STDERR to be a version string comparing to the given version.
345+ *
346+ * ```
347+ * Scenario: My example scenario
348+ * Given a WP install
349+ * When I run `wp core version
350+ * Then STDOUT should be a version string >= 6.8
351+ * ```
352+ *
353+ * @access public
354+ *
178355 * @Then /^(STDOUT|STDERR) should be a version string (<|<=|>|>=|==|=|!=|<>) ([+\w.{}-]+)$/
179356 */
180357 public function then_stdout_stderr_should_be_a_specific_version_string ( $ stream , $ operator , $ goal_ver ) {
@@ -186,6 +363,26 @@ public function then_stdout_stderr_should_be_a_specific_version_string( $stream,
186363 }
187364
188365 /**
366+ * Expect a certain file or directory to (not) exist or (not) contain certain contents.
367+ *
368+ * ```
369+ * Scenario: My example scenario
370+ * When I run `wp core download`
371+ * Then the wp-settings.php file should exist
372+ * And the wp-content directory should exist
373+ * And the {RUN_DIR} directory should contain:
374+ * """
375+ * index.php
376+ * license.txt
377+ * """
378+ * And the wp-config.php file should contain:
379+ * """
380+ * That's all, stop editing! Happy publishing.
381+ * """
382+ * ```
383+ *
384+ * @access public
385+ *
189386 * @Then /^the (.+) (file|directory) should( strictly)? (exist|not exist|be:|contain:|not contain:)$/
190387 */
191388 public function then_a_specific_file_folder_should_exist ( $ path , $ type , $ strictly , $ action , $ expected = null ) {
@@ -196,7 +393,7 @@ public function then_a_specific_file_folder_should_exist( $path, $type, $strictl
196393 $ path = $ this ->variables ['RUN_DIR ' ] . "/ $ path " ;
197394 }
198395
199- $ exists = function ( $ path ) use ( $ type ) {
396+ $ exists = static function ($ path ) use ( $ type ) {
200397 // Clear the stat cache for the path first to avoid
201398 // potentially inaccurate results when files change outside of PHP.
202399 // See https://www.php.net/manual/en/function.clearstatcache.php
@@ -240,6 +437,16 @@ public function then_a_specific_file_folder_should_exist( $path, $type, $strictl
240437 }
241438
242439 /**
440+ * Match file contents against a regex.
441+ *
442+ * ```
443+ * Scenario: My example scenario
444+ * When I run `wp scaffold plugin hello-world`
445+ * Then the contents of the wp-content/plugins/hello-world/languages/hello-world.pot file should match /X-Generator:\s/
446+ * ```
447+ *
448+ * @access public
449+ *
243450 * @Then /^the contents of the (.+) file should( not)? match (((\/.+\/)|(#.+#))([a-z]+)?)$/
244451 */
245452 public function then_the_contents_of_a_specific_file_should_match ( $ path , $ not , $ expected ) {
@@ -259,6 +466,16 @@ public function then_the_contents_of_a_specific_file_should_match( $path, $not,
259466 }
260467
261468 /**
469+ * Match STDOUT or STDERR against a regex.
470+ *
471+ * ```
472+ * Scenario: My example scenario
473+ * When I run `wp dist-archive wp-content/plugins/hello-world`
474+ * Then STDOUT should match /^Success: Created hello-world.0.1.0.zip \(Size: \d+(?:\.\d*)? [a-zA-Z]{1,3}\)$/
475+ * ```
476+ *
477+ * @access public
478+ *
262479 * @Then /^(STDOUT|STDERR) should( not)? match (((\/.+\/)|(#.+#))([a-z]+)?)$/
263480 */
264481 public function then_stdout_stderr_should_match_a_string ( $ stream , $ not , $ expected ) {
@@ -273,6 +490,16 @@ public function then_stdout_stderr_should_match_a_string( $stream, $not, $expect
273490 }
274491
275492 /**
493+ * Expect an email to be sent (or not).
494+ *
495+ * ```
496+ * Scenario: My example scenario
497+ * When I run `wp user reset-password 1`
498+ * Then an email should be sent
499+ * ```
500+ *
501+ * @access public
502+ *
276503 * @Then /^an email should (be sent|not be sent)$/
277504 */
278505 public function then_an_email_should_be_sent ( $ expected ) {
@@ -286,6 +513,17 @@ public function then_an_email_should_be_sent( $expected ) {
286513 }
287514
288515 /**
516+ * Expect the HTTP status code for visiting `http://localhost:8080`.
517+ *
518+ * ```
519+ * Scenario: My example scenario
520+ * Given a WP installation with Composer
521+ * And a PHP built-in web server to serve 'WordPress'
522+ * Then the HTTP status code should be 200
523+ * ```
524+ *
525+ * @access public
526+ *
289527 * @Then the HTTP status code should be :code
290528 */
291529 public function then_the_http_status_code_should_be ( $ return_code ) {
0 commit comments