Repository navigation
Expand file tree
/
Copy pathWhenStepDefinitions.php
More file actions
176 lines (160 loc) · 5.02 KB
/
Copy pathWhenStepDefinitions.php
File metadata and controls
176 lines (160 loc) · 5.02 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
<?php
namespace WP_CLI\Tests\Context;
use WP_CLI\Process;
use Exception;
trait WhenStepDefinitions {
/**
* @param Process $proc Process instance.
* @param string $mode Mode, either 'run' or 'try'.
* @return mixed
*/
public function wpcli_tests_invoke_proc( $proc, $mode ) {
$map = array(
'run' => 'run_check_stderr',
'try' => 'run',
);
$method = $map[ $mode ];
try {
return $proc->$method();
} catch ( \RuntimeException $e ) {
if ( 'run' === $mode ) {
$message = $e->getMessage();
$status = preg_match( '/exit status: (\d+)$/', $message, $matches ) ? $matches[1] : 'unknown';
if ( '0' === $status ) {
$message .= "\n\nThe command unexpectedly produced STDERR output. If you expect a non-zero exit status or STDERR output, use `When I try [...]`. Else, this may be a bug in your code or test.";
} else {
$message .= "\n\nThe command unexpectedly exited with status {$status}. If you expect a non-zero exit status, use `When I try [...]`. Else, this may be a bug in your code or test.";
}
$message .= $this->missing_command_hint( $e->getMessage() );
throw new \RuntimeException( $message, $e->getCode(), $e );
}
throw $e;
}
}
/**
* Capture the number of sent emails by parsing STDOUT.
*
* @param string $stdout
* @return array{string, int}
*/
public function wpcli_tests_capture_email_sends( $stdout ): array {
$stdout = preg_replace( '#WP-CLI test suite: Sent email to.+\n?#', '', $stdout, -1, $email_sends );
return array( $stdout, $email_sends );
}
/**
* Launch a given command in the background.
*
* ```
* Scenario: My example scenario
* Given a WP install
* And I launch in the background `wp server --host=localhost --port=8181`
* ...
* ```
*
* @access public
*
* @When /^I launch in the background `([^`]+)`$/
*
* @param string $cmd Command to run.
*/
public function when_i_launch_in_the_background( $cmd ): void {
$this->background_proc( $cmd );
}
/**
* Run or try a given command.
*
* `run` expects an exit code 0, whereas `try` allows for non-zero exit codes.
*
* So if using `run` and the command errors, the step will fail.
*
* ```
* Scenario: My example scenario
* When I run `wp core version`
* Then STDOUT should contain:
* """
* 6.8
* """
*
* Scenario: My other scenario
* When I try `wp i18n make-pot foo bar/baz.pot`
* Then STDERR should contain:
* """
* Error: Not a valid source directory.
* """
* And the return code should be 1
* ```
*
* @access public
*
* @When /^I (run|try) `([^`]+)`$/
*
* @param string $mode Mode, either 'run' or 'try'.
* @param string $cmd Command to execute.
*/
public function when_i_run( $mode, $cmd ): void {
$cmd = $this->replace_variables( $cmd );
$this->result = $this->wpcli_tests_invoke_proc( $this->proc( $cmd ), $mode );
list( $this->result->stdout, $this->email_sends ) = $this->wpcli_tests_capture_email_sends( $this->result->stdout );
}
/**
* Run or try a given command in a subdirectory.
*
* `run` expects an exit code 0, whereas `try` allows for non-zero exit codes.
*
* ```
* Scenario: My example scenario
* When I run `wp core is-installed`
* Then STDOUT should be empty
*
* When I run `wp core is-installed` from 'foo/wp-content'
* Then STDOUT should be empty
* ```
*
* @access public
*
* @When /^I (run|try) `([^`]+)` from '([^\s]+)'$/
*
* @param string $mode Mode, either 'run' or 'try'.
* @param string $cmd Command to execute.
* @param string $subdir Directory.
*/
public function when_i_run_from_a_subfolder( $mode, $cmd, $subdir ): void {
$cmd = $this->replace_variables( $cmd );
$this->result = $this->wpcli_tests_invoke_proc( $this->proc( $cmd, array(), $subdir ), $mode );
list( $this->result->stdout, $this->email_sends ) = $this->wpcli_tests_capture_email_sends( $this->result->stdout );
}
/**
* Run or try the previous command again.
*
* `run` expects an exit code 0, whereas `try` allows for non-zero exit codes.
*
* ```
* Scenario: My example scenario
* When I run `wp site option update admin_user_id 1`
* Then STDOUT should contain:
* """
* Success: Updated 'admin_user_id' site option.
* """
*
* When I run the previous command again
* Then STDOUT should contain:
* """
* Success: Value passed for 'admin_user_id' site option is unchanged.
* """
* ```
*
* @access public
*
* @When /^I (run|try) the previous command again$/
*
* @param string $mode Mode, either 'run' or 'try'
*/
public function when_i_run_the_previous_command_again( $mode ): void {
if ( ! isset( $this->result ) ) {
throw new Exception( 'No previous command.' );
}
$proc = Process::create( $this->result->command, $this->result->cwd, $this->result->env );
$this->result = $this->wpcli_tests_invoke_proc( $proc, $mode );
list( $this->result->stdout, $this->email_sends ) = $this->wpcli_tests_capture_email_sends( $this->result->stdout );
}
}