OSDN Git Service

Merge pull request #970 from wp-cli/support-user-login-970
[wvm/wvm.git] / features / steps / given.php
1 <?php
2
3 use Behat\Gherkin\Node\PyStringNode,
4     Behat\Gherkin\Node\TableNode;
5
6 $steps->Given( '/^an empty directory$/',
7         function ( $world ) {
8                 $world->create_run_dir();
9         }
10 );
11
12 $steps->Given( '/^a ([^\s]+) file:$/',
13         function ( $world, $path, PyStringNode $content ) {
14                 $content = (string) $content . "\n";
15                 $full_path = $world->variables['RUN_DIR'] . "/$path";
16                 Process::create( \WP_CLI\utils\esc_cmd( 'mkdir -p %s', dirname( $full_path ) ) )->run_check();
17                 file_put_contents( $full_path, $content );
18         }
19 );
20
21 $steps->Given( '/^WP files$/',
22         function ( $world ) {
23                 $world->download_wp();
24         }
25 );
26
27 $steps->Given( '/^wp-config\.php$/',
28         function ( $world ) {
29                 $world->create_config();
30         }
31 );
32
33 $steps->Given( '/^a database$/',
34         function ( $world ) {
35                 $world->create_db();
36         }
37 );
38
39 $steps->Given( '/^a WP install$/',
40         function ( $world ) {
41                 $world->install_wp();
42         }
43 );
44
45 $steps->Given( "/^a WP install in '([^\s]+)'$/",
46         function ( $world, $subdir ) {
47                 $world->install_wp( $subdir );
48         }
49 );
50
51 $steps->Given( '/^a WP multisite install$/',
52         function ( $world ) {
53                 $world->install_wp();
54                 $world->proc( 'wp core install-network', array( 'title' => 'WP CLI Network' ) )->run_check();
55         }
56 );
57
58 $steps->Given( '/^a custom wp-content directory$/',
59         function ( $world ) {
60                 $wp_config_path = $world->variables['RUN_DIR'] . "/wp-config.php";
61
62                 $wp_config_code = file_get_contents( $wp_config_path );
63
64                 $world->move_files( 'wp-content', 'my-content' );
65                 $world->add_line_to_wp_config( $wp_config_code,
66                         "define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/my-content' );" );
67
68                 $world->move_files( 'my-content/plugins', 'my-plugins' );
69                 $world->add_line_to_wp_config( $wp_config_code,
70                         "define( 'WP_PLUGIN_DIR', __DIR__ . '/my-plugins' );" );
71
72                 file_put_contents( $wp_config_path, $wp_config_code );
73         }
74 );
75
76 $steps->Given( '/^download:$/',
77         function ( $world, TableNode $table ) {
78                 foreach ( $table->getHash() as $row ) {
79                         $path = $world->replace_variables( $row['path'] );
80                         if ( file_exists( $path ) ) {
81                                 // assume it's the same file and skip re-download
82                                 continue;
83                         }
84
85                         \Process::create( \WP_CLI\Utils\esc_cmd( 'curl -sSL %s > %s', $row['url'], $path ) )->run_check();
86                 }
87         }
88 );
89
90 $steps->Given( '/^save (STDOUT|STDERR) ([\'].+[^\'])?as \{(\w+)\}$/',
91         function ( $world, $stream, $output_filter, $key ) {
92
93                 if ( $output_filter ) {
94                         $output_filter = '/' . trim( str_replace( '%s', '(.+[^\b])', $output_filter ), "' " ) . '/';
95                         if ( false !== preg_match( $output_filter, $world->result->$stream, $matches ) )
96                                 $output = array_pop( $matches );
97                         else
98                                 $output = '';
99                 } else {
100                         $output = $world->result->$stream;
101                 }
102                 $world->variables[ $key ] = trim( $output, "\n" );
103         }
104 );
105