Skip to content

Commit 83129fc

Browse files
Merge pull request #148 from wp-cli/add-context-for-regex
Add context to behat which can check the contents with regexp
2 parents 961cba0 + 33c371f commit 83129fc

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

features/bootstrap/support.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
// Utility functions used by Behat steps
44

5+
function assertRegExp( $regex, $actual ) {
6+
if ( ! preg_match( $regex, $actual ) ) {
7+
throw new Exception( "Actual value: " . var_export( $actual, true ) );
8+
}
9+
}
10+
511
function assertEquals( $expected, $actual ) {
612
if ( $expected != $actual ) {
713
throw new Exception( "Actual value: " . var_export( $actual, true ) );

features/steps/then.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function ( $world, $stream, $operator, $goal_ver ) {
152152
throw new Exception( $world->result );
153153
}
154154
}
155-
);
155+
);
156156

157157
$steps->Then( '/^the (.+) (file|directory) should (exist|not exist|be:|contain:|not contain:)$/',
158158
function ( $world, $path, $type, $action, $expected = null ) {
@@ -199,6 +199,28 @@ function ( $world, $path, $type, $action, $expected = null ) {
199199
}
200200
);
201201

202+
$steps->Then( '/^the contents of the (.+) file should match (((\/.+\/)|(#.+#))([a-z]+)?)$/',
203+
function ( $world, $path, $expected ) {
204+
$path = $world->replace_variables( $path );
205+
206+
// If it's a relative path, make it relative to the current test dir
207+
if ( '/' !== $path[0] ) {
208+
$path = $world->variables['RUN_DIR'] . "/$path";
209+
}
210+
211+
$contents = file_get_contents( $path );
212+
assertRegExp( $expected, $contents );
213+
}
214+
);
215+
216+
$steps->Then( '/^(STDOUT|STDERR) should match (((\/.+\/)|(#.+#))([a-z]+)?)$/',
217+
function ( $world, $stream, $expected ) {
218+
219+
$stream = strtolower( $stream );
220+
assertRegExp( $expected, $world->result->$stream );
221+
}
222+
);
223+
202224
$steps->Then( '/^an email should (be sent|not be sent)$/', function( $world, $expected ) {
203225
if ( 'be sent' === $expected ) {
204226
assertNotEquals( 0, $world->email_sends );

0 commit comments

Comments
 (0)