OSDN Git Service

behat: split step definition files
authorscribu <mail@scribu.net>
Sun, 27 Oct 2013 21:29:53 +0000 (23:29 +0200)
committerscribu <mail@scribu.net>
Sun, 27 Oct 2013 21:29:53 +0000 (23:29 +0200)
features/bootstrap/FeatureContext.php
features/steps/given.php [new file with mode: 0644]
features/steps/then.php [moved from features/steps/basic_steps.php with 54% similarity]
features/steps/when.php [new file with mode: 0644]

index 35d9217..d1650f5 100644 (file)
@@ -79,7 +79,7 @@ class FeatureContext extends BehatContext implements ClosuredContextInterface {
        }
 
        public function getStepDefinitionResources() {
-               return array( __DIR__ . '/../steps/basic_steps.php' );
+               return glob( __DIR__ . '/../steps/*.php' );
        }
 
        public function getHookDefinitionResources() {
diff --git a/features/steps/given.php b/features/steps/given.php
new file mode 100644 (file)
index 0000000..998a013
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+
+use Behat\Gherkin\Node\PyStringNode,
+    Behat\Gherkin\Node\TableNode;
+
+$steps->Given( '/^an empty directory$/',
+       function ( $world ) {
+               $world->create_run_dir();
+       }
+);
+
+$steps->Given( '/^a ([^\s]+) file:$/',
+       function ( $world, $path, PyStringNode $content ) {
+               $content = (string) $content . "\n";
+               $full_path = $world->variables['RUN_DIR'] . "/$path";
+               Process::create( \WP_CLI\utils\esc_cmd( 'mkdir -p %s', dirname( $full_path ) ) )->run_check();
+               file_put_contents( $full_path, $content );
+       }
+);
+
+$steps->Given( '/^WP files$/',
+       function ( $world ) {
+               $world->download_wp();
+       }
+);
+
+$steps->Given( '/^wp-config\.php$/',
+       function ( $world ) {
+               $world->create_config();
+       }
+);
+
+$steps->Given( '/^a database$/',
+       function ( $world ) {
+               $world->create_db();
+       }
+);
+
+$steps->Given( '/^a WP install$/',
+       function ( $world ) {
+               $world->install_wp();
+       }
+);
+
+$steps->Given( "/^a WP install in '([^\s]+)'$/",
+       function ( $world, $subdir ) {
+               $world->install_wp( $subdir );
+       }
+);
+
+$steps->Given( '/^a WP multisite install$/',
+       function ( $world ) {
+               $world->install_wp();
+               $world->proc( 'wp core install-network', array( 'title' => 'WP CLI Network' ) )->run_check();
+       }
+);
+
+$steps->Given( '/^a custom wp-content directory$/',
+       function ( $world ) {
+               $wp_config_path = $world->variables['RUN_DIR'] . "/wp-config.php";
+
+               $wp_config_code = file_get_contents( $wp_config_path );
+
+               $world->move_files( 'wp-content', 'my-content' );
+               $world->add_line_to_wp_config( $wp_config_code,
+                       "define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/my-content' );" );
+
+               $world->move_files( 'my-content/plugins', 'my-plugins' );
+               $world->add_line_to_wp_config( $wp_config_code,
+                       "define( 'WP_PLUGIN_DIR', __DIR__ . '/my-plugins' );" );
+
+               file_put_contents( $wp_config_path, $wp_config_code );
+       }
+);
+
+$steps->Given( '/^download:$/',
+       function ( $world, TableNode $table ) {
+               foreach ( $table->getHash() as $row ) {
+                       $path = $world->replace_variables( $row['path'] );
+                       if ( file_exists( $path ) ) {
+                               // assume it's the same file and skip re-download
+                               continue;
+                       }
+
+                       \Process::create( \WP_CLI\Utils\esc_cmd( 'curl -sSL %s > %s', $row['url'], $path ) )->run_check();
+               }
+       }
+);
+
+$steps->Given( '/^save (STDOUT|STDERR) ([\'].+[^\'])?as \{(\w+)\}$/',
+       function ( $world, $stream, $output_filter, $key ) {
+
+               if ( $output_filter ) {
+                       $output_filter = '/' . trim( str_replace( '%s', '(.+[^\b])', $output_filter ), "' " ) . '/';
+                       if ( false !== preg_match( $output_filter, $world->result->$stream, $matches ) )
+                               $output = array_pop( $matches );
+                       else
+                               $output = '';
+               } else {
+                       $output = $world->result->$stream;
+               }
+               $world->variables[ $key ] = trim( $output, "\n" );
+       }
+);
+
similarity index 54%
rename from features/steps/basic_steps.php
rename to features/steps/then.php
index 44342b5..75845c0 100644 (file)
@@ -3,140 +3,6 @@
 use Behat\Gherkin\Node\PyStringNode,
     Behat\Gherkin\Node\TableNode;
 
-function invoke_proc( $proc, $mode, $subdir = null ) {
-       $map = array(
-               'run' => 'run_check',
-               'try' => 'run'
-       );
-       $method = $map[ $mode ];
-
-       return $proc->$method( $subdir );
-}
-
-$steps->Given( '/^an empty directory$/',
-       function ( $world ) {
-               $world->create_run_dir();
-       }
-);
-
-$steps->Given( '/^a ([^\s]+) file:$/',
-       function ( $world, $path, PyStringNode $content ) {
-               $content = (string) $content . "\n";
-               $full_path = $world->variables['RUN_DIR'] . "/$path";
-               Process::create( \WP_CLI\utils\esc_cmd( 'mkdir -p %s', dirname( $full_path ) ) )->run_check();
-               file_put_contents( $full_path, $content );
-       }
-);
-
-$steps->Given( '/^WP files$/',
-       function ( $world ) {
-               $world->download_wp();
-       }
-);
-
-$steps->Given( '/^wp-config\.php$/',
-       function ( $world ) {
-               $world->create_config();
-       }
-);
-
-$steps->Given( '/^a database$/',
-       function ( $world ) {
-               $world->create_db();
-       }
-);
-
-$steps->Given( '/^a WP install$/',
-       function ( $world ) {
-               $world->install_wp();
-       }
-);
-
-$steps->Given( "/^a WP install in '([^\s]+)'$/",
-       function ( $world, $subdir ) {
-               $world->install_wp( $subdir );
-       }
-);
-
-$steps->Given( '/^a WP multisite install$/',
-       function ( $world ) {
-               $world->install_wp();
-               $world->proc( 'wp core install-network', array( 'title' => 'WP CLI Network' ) )->run_check();
-       }
-);
-
-$steps->Given( '/^a custom wp-content directory$/',
-       function ( $world ) {
-               $wp_config_path = $world->variables['RUN_DIR'] . "/wp-config.php";
-
-               $wp_config_code = file_get_contents( $wp_config_path );
-
-               $world->move_files( 'wp-content', 'my-content' );
-               $world->add_line_to_wp_config( $wp_config_code,
-                       "define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/my-content' );" );
-
-               $world->move_files( 'my-content/plugins', 'my-plugins' );
-               $world->add_line_to_wp_config( $wp_config_code,
-                       "define( 'WP_PLUGIN_DIR', __DIR__ . '/my-plugins' );" );
-
-               file_put_contents( $wp_config_path, $wp_config_code );
-       }
-);
-
-$steps->Given( '/^download:$/',
-       function ( $world, TableNode $table ) {
-               foreach ( $table->getHash() as $row ) {
-                       $path = $world->replace_variables( $row['path'] );
-                       if ( file_exists( $path ) ) {
-                               // assume it's the same file and skip re-download
-                               continue;
-                       }
-
-                       \Process::create( \WP_CLI\Utils\esc_cmd( 'curl -sSL %s > %s', $row['url'], $path ) )->run_check();
-               }
-       }
-);
-
-$steps->When( '/^I (run|try) `([^`]+)`$/',
-       function ( $world, $mode, $cmd ) {
-               $cmd = $world->replace_variables( $cmd );
-               $world->result = invoke_proc( $world->proc( $cmd ), $mode );
-       }
-);
-
-$steps->When( "/^I (run|try) `([^`]+)` from '([^\s]+)'$/",
-       function ( $world, $mode, $cmd, $subdir ) {
-               $cmd = $world->replace_variables( $cmd );
-               $world->result = invoke_proc( $world->proc( $cmd ), $mode, $subdir );
-       }
-);
-
-$steps->When( '/^I (run|try) the previous command again$/',
-       function ( $world, $mode ) {
-               if ( !isset( $world->result ) )
-                       throw new \Exception( 'No previous command.' );
-
-               $proc = Process::create( $world->result->command, $world->result->cwd, $world->result->env );
-               $world->result = invoke_proc( $proc, $mode );
-       }
-);
-
-$steps->Given( '/^save (STDOUT|STDERR) ([\'].+[^\'])?as \{(\w+)\}$/',
-       function ( $world, $stream, $output_filter, $key ) {
-
-               if ( $output_filter ) {
-                       $output_filter = '/' . trim( str_replace( '%s', '(.+[^\b])', $output_filter ), "' " ) . '/';
-                       if ( false !== preg_match( $output_filter, $world->result->$stream, $matches ) )
-                               $output = array_pop( $matches );
-                       else
-                               $output = '';
-               } else {
-                       $output = $world->result->$stream;
-               }
-               $world->variables[ $key ] = trim( $output, "\n" );
-       }
-);
-
 $steps->Then( '/^the return code should be (\d+)$/',
        function ( $world, $return_code ) {
                if ( $return_code != $world->result->return_code ) {
diff --git a/features/steps/when.php b/features/steps/when.php
new file mode 100644 (file)
index 0000000..81b4c9a
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+use Behat\Gherkin\Node\PyStringNode,
+    Behat\Gherkin\Node\TableNode;
+
+function invoke_proc( $proc, $mode, $subdir = null ) {
+       $map = array(
+               'run' => 'run_check',
+               'try' => 'run'
+       );
+       $method = $map[ $mode ];
+
+       return $proc->$method( $subdir );
+}
+
+$steps->When( '/^I (run|try) `([^`]+)`$/',
+       function ( $world, $mode, $cmd ) {
+               $cmd = $world->replace_variables( $cmd );
+               $world->result = invoke_proc( $world->proc( $cmd ), $mode );
+       }
+);
+
+$steps->When( "/^I (run|try) `([^`]+)` from '([^\s]+)'$/",
+       function ( $world, $mode, $cmd, $subdir ) {
+               $cmd = $world->replace_variables( $cmd );
+               $world->result = invoke_proc( $world->proc( $cmd ), $mode, $subdir );
+       }
+);
+
+$steps->When( '/^I (run|try) the previous command again$/',
+       function ( $world, $mode ) {
+               if ( !isset( $world->result ) )
+                       throw new \Exception( 'No previous command.' );
+
+               $proc = Process::create( $world->result->command, $world->result->cwd, $world->result->env );
+               $world->result = invoke_proc( $proc, $mode );
+       }
+);
+